$j = jQuery.noConflict();
$j(function() {
    $j('#region_picker .regioninfo .videobox a').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        $j('#lightbox').show();
        $j('#voverlay').show();
        $j('#vcontainer').html('<embed width="640" height="480" type="application/x-shockwave-flash" src="' + $j(this).attr('href') + '" pluginspage="http://www.adobe.com/go/getflashplayer" flashvars=""/>');
        $j("#voverlay").css('background', 'url("http://www.effoundation.org/Foundation-US/images/lightbox/back.png")');
        //attr('src',$j(this).attr('href'));

        $j('#voverlay .close').click(function(e) {
            e.stopPropagation();
            $j('#vcontainer embed').remove();
            //$j('#vcontainer embed').attr('src', '');
            // swfobject.removeSWF('video_overlay');
            $j('#voverlay').hide();
            $j('#lightbox').hide();
        });
    });


    // iSendBox fix
    if ($j.browser.msie) {
        $j('#iSendBox').appendTo('body');
    }
    //Print profile
    $j('.printLink').click(function(e) {
        e.preventDefault();
        print();
    });
    //Active Links			
    $j('.active, #region_picker a').click(function(e) {
        e.preventDefault();
    });
    //Rounded Corners
    /*
    $j('.prominent, .locale').append('<div class="cap_lt"></div><div class="cap_rt">');
    if ($j.browser.msie && parseInt($j.browser.version) < 9) {
    $j('.rounded').append('<div class="cap_lt"></div><div class="cap_rt"></div><div class="cap_lb"></div><div class="cap_rb"></div>	');
    }
    */
    $j('.rounded').prepend('<div class="top"></div>');
    //Maps
    $j('.map').wrap('<div class="map_overlay"></div>');
    //Slideshow
    $j('.scroll_l, .scroll_r').css('display', 'none');
    var countImages = $j('#stage .slideshow ul li').length;
    if (countImages > 5) {
        $j('.scroll_r').css('display', 'block');
    }
    var currentImage = 0;
    $j('.scroll_r').click(function() {
        currentImage++;
        if (countImages - currentImage == 5) {
            $j('.scroll_r').css('display', 'none');
        }
        $j('.slideshow ul').animate({
            left: '-=70'
        });
        $j('.scroll_l').css('display', 'block');
    });
    $j('.scroll_l').click(function() {
        currentImage--;
        if (currentImage == 0) {
            $j('.scroll_l').css('display', 'none');
        }
        $j('.slideshow ul').animate({
            left: '+=70'
        });
        $j('.scroll_r').css('display', 'block');
    });
    var currentText;
    $j('#slidebox').css('display', 'none');
    $j('#stage .slideshow .imagelist a').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        var clickedItem = $j('#stage .slideshow a').index(this);
        $j('#bg_country').css('display', 'none');
        $j('#lightbox').css('display', 'none');
        $j('#slidebox .slideshow .imagelist img.active').removeClass('active');
        $j('#slidebox .slideshow .imagelist img:eq(' + clickedItem + ')').addClass('active');
        $j('#slidebox .contain *').remove();
        currentText = $j(this).next('div').html();
        $j('#slidebox .contain').html('<img src="' + $j(this).children('img').attr('src') + '" style="padding: 0;" />');
        $j('#slidebox .w_width').css({
            width: 'auto',
            left: 0
        });
        if ($j.browser.msie) {
            $j('#slidebox img').css('width', '400px');
            $j('#slidebox .contain').append(currentText);
            $j('#slidebox .w_width').css('width', '99999px');
            $j('#slidebox .contain').css({
                width: '400px',
                marginRight: '20px'
            });
            $j('#slidebox').css({
                'display': 'none',
                'width': 402,
                'height': 400,
                'top': 110,
                'left': ($j(window).width() / 2) - 200,
                'padding': 15
            });
            $j('#lightbox').css('display', 'block').animate({
                opacity: 0.8
            });
            $j('#slidebox').css('display', 'block');
        }
        if (!$j.browser.msie) {
            $j('#slidebox img').css('width', '50px');
            $j('#slidebox .slider').css('display', 'none');
            $j('#slidebox .w_width').css({
                width: 'auto',
                left: 0
            });
            $j('#slidebox .contain').css({
                width: 'auto',
                marginRight: 0
            });
            $j('#slidebox').css({
                display: 'block',
                width: 'auto',
                height: 'auto',
                padding: '3px',
                paddingBottom: '0',
                top: 300 - $j(window).scrollTop(),
                left: $j(this).offset().left
            });
            $j(' #slidebox .slider').css({
                display: 'none',
                opacity: 0
            });
            $j('#lightbox').css({
                display: 'block',
                opacity: '0'
            }).animate({
                opacity: 0.9
            }, function() {
                $j('#slidebox img').animate({
                    width: 400
                });
                $j('#slidebox').animate({
                    left: ($j(window).width() / 2) - 200,
                    top: 110,
                    padding: 15
                }, function() {
                    $j('#slidebox').css('width', '402px');
                    $j('#slidebox .w_width').css('width', '99999px');
                    $j('#slidebox .contain').css({
                        width: '400px',
                        marginRight: '20px'
                    });
                    $j('#slidebox .contain').append(currentText);
                    $j('#slidebox').animate({
                        height: 400
                    }, function() {
                        $j(' #slidebox .slider').css({
                            display: 'block',
                            opacity: 0
                        });
                        $j('#slidebox .slider').animate({
                            opacity: 0.8
                        }
                        /*, function () {
                        $j('#bg_country').css('display', 'block').animate({
                        opacity: 0.7
                        }, 1500);
                        }*/
                        );
                    });
                });
            });
        }
        if (!$j.browser.msie) {
            $j('#slidebox .slider').hover(function() {
                $j(this).css('opacity', '1.0');
            }, function() {
                $j(this).css('opacity', '0.8');
            });
        }
    });
    $j('#slidebox .slideshow .imagelist a').click(function(e) {
        var clickedItem = $j('#slidebox a').index(this);
        $j('#slidebox .slideshow .imagelist a img.active').removeClass('active');
        $j(this).children('img').addClass('active');
        $j('#slidebox .w_width').append('<div class="contain" style="width:400px; margin-right:20px;"><img src="' + $j('#stage .slideshow .imagelist li:eq(' + clickedItem + ') img').attr('src') + '" style="padding: 0;" />' + $j('#stage .slideshow .imagelist li:eq(' + clickedItem + ') div').html() + '</div>');
        $j('#slidebox .w_width').animate({
            left: '-=420'
        }, 150);
    });
    $j('#slidebox').click(function(e) {
        e.stopPropagation();
    });
    $j('html').click(function(e) {
    $j('#slidebox .contain').remove();    
        if ($j('.homepage').size() == 0){
            $j('#lightbox').css('display', 'none');
        }
        $j('#bg_country').css({
            display: 'none',
            opacity: 0
        });
        $j('#slidebox').css('display', 'none');
        $j('#slidebox .w_width').html('<div class="contain"></div>');
    });
    //Video		
    $j('.video').wrap('<div class="videowrap"></div>');
    $j('.video').after('<a class="video_play"></a>');
    $j(window).load(function() {
        $j('.video').each(function() {
            var comLeft = (($j(this).width() / 2 - 30 + 4) < 0) ? 26 : $j(this).width() / 2 - 30 + 4;
            var comTop = (($j(this).height() / 2 - 30 + 4) < 0) ? 5 : $j(this).height() / 2 - 30 + 4; ;
            $j(this).next('.video_play').css({
                left: comLeft,
                top: comTop
            });
        });
    });
    $j('.videowrap').click(function(e) {
        $j('#stage').stop();
    });
    //Region Picker
    var repeatSlide = function() { ; };
    var flagRegClicked = false;
    $j('#stage .banner img:eq(0)').css('display', 'block');
    if ($j('.homepage').length == 1) {
        $j(window).load(function() {
            //$j('#region_picker').css('opacity', 0.80);
            //$j('#region_picker .regionlist').css('opacity', 0.90);
            var activeItem = $j('#region_picker .regionlist a.active').attr('href');
            $j('#stage .banner img:eq(' + activeItem + ')').css('z-index', '99');
            $j('#region_picker .hover_mover').css('top', activeItem * 40 - activeItem);
            $j('#region_picker h3, #region_picker .select_region, #region_picker .regionlist').hover(function() {
                $j('#region_picker .regionlist').animate({
                    height: 233
                }, {
                    queue: false
                });
            }, function() {
                $j('#region_picker .regionlist').animate({
                    height: 0
                }, {
                    queue: false
                });
            });
            $j('#region_picker .regionlist a').mouseover(function(e) {
                e.preventDefault();
                var hoveredItem = $j(this).attr('href');
                $j('#stage #region_picker .hover_mover').animate({
                    top: hoveredItem * 40 - hoveredItem
                }, {
                    duration: 250,
                    queue: false
                });
            });
            $j('#region_picker .regionlist').mouseout(function(e) {
                var activeItem = $j('#region_picker .regionlist a.active').attr('href');
                $j('#region_picker .hover_mover').animate({
                    top: activeItem * 40 - activeItem
                }, {
                    duration: 250,
                    queue: false
                });
            });
            $j('#stage .banner img').css({
                'z-index': 9,
                'opacity': 0,
                'display': 'block'
            });
            $j('#stage .banner img:eq(0)').css({
                'z-index': 99,
                'opacity': 1
            });

            //AutoSlide & RegionClick
            var now_active = 0;
            //AutoSlide
            var count = $j('.regionlist ul li').size();

            repeatSlide = function() {
                now_active++;
                if (now_active == count) now_active = 0;
                regionClickHandler();
                $j('#stage').animate({ height: 740 }, 5000, function() { repeatSlide(); });
            };
            $j('#stage').animate({ height: 740 }, 5000, function() { repeatSlide(); });
            //RegionClick            
            $j('#region_picker .regionlist a').click(function(e) {
                flagRegClicked = true;
                e.preventDefault();
                now_active = $j(this).attr('href');
                regionClickHandler();
                $j('#stage').stop(); //.animate({height: 740}, 10000, function(){repeatSlide();});
            });

            var regionClickHandler = function() {
                //e.preventDefault();
                //var now_active = 3;
                activeItem = $j('#stage .regionlist a.active').attr('href');
                $j('#stage .banner img:eq(' + activeItem + ')').css('z-index', '9');
                $j('#stage .banner img:eq(' + now_active + ')').css({
                    'z-index': 99,
                    'opacity': 0
                }).animate({
                    'opacity': 1
                }, 250, function() {
                    $j('#stage .banner img:gt(' + now_active + ')').css({
                        'z-index': 9,
                        'opacity': 0
                    });
                    $j('#stage .banner img:lt(' + now_active + ')').css({
                        'z-index': 9,
                        'opacity': 0
                    });
                });
                $j('#stage .regionlist a.active').removeClass('active');
                //$j(this).addClass('active');
                $j('#stage .regionlist a:eq(' + now_active + ')').addClass('active');
                //var thisItem = $j(this).attr('href');
                var distance = Math.abs(now_active - activeItem);
                $j('#region_picker .regioninfo').animate({
                    left: now_active * 310 * -1
                }, 250, function() { $j('#region_picker h3').html($j('#stage .banner img:eq(' + now_active + ')').attr('alt')); });

                //var activeItem = $j('#region_picker .regionlist a.active').attr('href');
                $j('#region_picker .hover_mover').animate({
                    top: now_active * 40 - now_active
                }, {
                    duration: 250,
                    queue: false
                });
                $j('#region_picker .regionlist').css('height', 0);
            };
        });
    }
    //Student Country List
    $j('#student_country  *').click(function(e) {
        e.stopPropagation();
        //e.preventDefault();
    });
    $j('#student_country > a').click(function(e) {
        //e.stopPropagation();
        e.preventDefault();
        var items = $j('#student_country  ul li').size();
        var ht = Math.ceil(items / 2) * 24 + 20;
        $j('#student_country div').css('padding-top', '2px').animate({
            height: ht
        });
    });
    $j('html, #student_country .slide_up').click(function() {
        $j('#student_country div').animate({
            height: 0
        }, function() {
            $j('#student_country div').css('padding', '0')
        });
    });
    //MoS Pages Filter Bar
    $j('.no_pref').not('#interestsBox a.no_pref').click(function(e) {
        e.preventDefault();
        if ($j(this).hasClass('selected')) {
            $j(this).removeClass('selected');
        } else {
            $j(this).parents('ul').find('a').removeClass('selected');
            $j(this).addClass('selected');
        }
    });
    $j('#filterBar a').not('.no_pref, #interestsBox a').click(function(e) {
        e.preventDefault();
        if ($j(this).hasClass('selected')) {
            $j(this).removeClass('selected');
        } else {
            $j(this).parents('ul').find('a.no_pref').removeClass('selected');
            $j(this).addClass('selected');
        }
        //alert($j(this).parents('ul').find('a.selected').size()); 
        if ($j(this).parents('ul').find('a.selected').size() == 0) {
            $j(this).parents('ul').find('a.no_pref').addClass('selected');
        }
    });

    //interestsBox
    $j('#interestsBox a').not('a.no_pref').click(function(e) {
        e.preventDefault();
        if ($j(this).parent('li').hasClass('selected')) {
            $j(this).parent('li').removeClass('selected');
        } else {
            $j(this).parents('ul').find('a.no_pref').removeClass('selected');
            $j(this).parent('li').addClass('selected');
        }
        if ($j('#interestsBox li.selected').size() == 0) {
            $j('#interestsBox a.no_pref').addClass('selected');
        }
    });
    $j('#interestsBox a.no_pref').click(function(e) {
        e.preventDefault();
        if ($j(this).hasClass('selected')) {
            $j(this).removeClass('selected');
        } else {
            $j(this).parents('ul').find('li').removeClass('selected');
            $j(this).addClass('selected');
        }
    });
    //interestsBox Tooltip
    $j('.interestList li a span').css('display', 'none');
    //IE Fix for word wrap   
    if ($j.browser.msie) {
        $j('.interestList a span b').each(function() {
            $j(this).html($j(this).text().replace(' ', '\&nbsp\;'));
        });
        //alert($j('#favList').css('height', Math.ceil($j('#favList .interestList li').size() / 3) * 42));
        $j('#favList').css('height', Math.ceil($j('#favList .interestList li').size() / 3) * 42);
    }
    //var tempObj;
    $j('.interestList li a').not('a.no_pref').mouseover(function(e) {
        e.stopPropagation();
        e.preventDefault();
        //$j('.interestList li a span').css('display', 'none');
        // $j(this).find('a span').css('display', 'inline');
        $j('#tooltip span').html($j(this).text());
        $j('#tooltip').css({
            'left': $j(this).offset().left,
            'top': $j(this).offset().top - 35
        });
    });
    if ($j.browser.msie || $j.browser.webkit || $j.browser.mozilla || $j.browser.opera) {
        $j('html').mouseover(function() {
            //$j('.interestList li a span').css('display', 'none');
            $j('#tooltip').css({
                'left': -400,
                'top': -4000
            });
        });
    }


    //countryBox
    $j('#countryBox h4').click(function(e) {
        //alert($j('#countryBox .countryList').css('height'));
        if ($j('#countryBox .countryList').css('height') == '38px') {
            $j('#countryBox .countryList').animate({
                height: 570
            });
        }
        else {
            $j('#countryBox .countryList').animate({
                height: 38
            });
        }
    });
    $j('#countryBox .countryList').click(function(e) {
        e.stopPropagation();
    });
    $j('html').click(function() {
        $j('#countryBox .countryList').animate({
            height: 38
        });
        if ($j('.homepage').length == 1 && !flagRegClicked) {
            $j('#stage').animate({ height: 740 }, 5000, function() { repeatSlide(); });
        }
    });

    //iFrame Send Email
    $j('#whitebox').css('opacity', '0.8');
    $j('#sendEmail').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        $j('#iSendBox').css({
            'width': 16,
            'height': 11,
            'left': $j(this).offset().left,
            'top': $j(this).offset().top - $j(window).scrollTop()
        });
        $j('#lightbox').css('display', 'block');
        $j('#iSendBox').css('display', 'block').animate({
            'width': 350,
            'height': 350,
            'left': ($j(window).width() / 2) - 115,
            'top': 150
        });
    });
    $j('#iSendBox').click(function(e) {
        e.stopPropagation();
    });
    $j('#emailBox h3 span').click(function() {
        //$j('#lightbox', window.parent.document).css('display', 'none');
        $j('#iSendBox', window.parent.document).animate({
            'width': 16,
            'height': 11,
            'left': $j('#sendEmail', window.parent.document).offset().left,
            'top': $j('#sendEmail', window.parent.document).offset().top - $j(window, window.parent.document).scrollTop()
        }, function() {
            $j('#iSendBox', window.parent.document).css('display', 'none');
            $j('#lightbox', window.parent.document).css('display', 'none');
        }); //.css('display', 'none');
    });

});

