var mainnav = {
    closetimer: '',
    
    init: function() {
        jQuery('#main_navigation > ul > li.children').hover(
            function() {
                clearTimeout(mainnav.closetimer);
                var self = jQuery(this);
                var dropdown = self.children('div');
                
                var dropdown_left = self.parent().width() - dropdown.width() - self.position().left;
                if( dropdown_left < 0 ) {
                    dropdown.css("right", 0);
                } else {
                    dropdown.css("right", dropdown_left);
                }
                // IE6
                //jQuery(this).attr("class", "children dropdown").siblings().attr("class", "children");
                jQuery(this).addClass('dropdown').siblings().removeClass('dropdown');
            },
            function() { //OUT
                mainnav.closetimer = setTimeout(function() {
                    jQuery('#main_navigation > ul > li.children').removeClass('dropdown');
                }, 350);
            }
        );
        jQuery('#main_navigation > ul > li.children > div > div > div').hover(
            function() { jQuery(this).addClass('hover'); },
            function() { jQuery(this).removeClass('hover'); }
        )
    }
};

(function($){
    $(document).ready(function() {
        mainnav.init();
        
        // Hide the side social navigation bar if 1024 or smaller
        if( $('body').width() < 1000 ) $('#social_links').hide();
        
        // Cufon text replacement
        //.cuf_goth-light = gotham-light
        //.cuf_goth-bold  = gotham-bold
        //.cuf_goth-book  = gotham-book
        //.cuf_goth-med  = gotham-medium
        //.cuf_ro  = hover state generated too
        
        Cufon.now();
        
        // Start: Site Switcher Popup
        $('#siteswitcher a.btn.ignore').click(function() {
        	$(this).parent().fadeOut(150);
        	$.ajax({
				url: 'home/ignoreipsniffer'
			});
			return false;
        });
        // End: Site Switcher Popup
        
        if( !$.browser.msie || $.browser.version.substr(0,1) >= 8 ) {
            Cufon.replace( '.cuf_goth-light:not(.cuf_ro)', {hover:false, fontFamily:'Gotham-Light'} );
            Cufon.replace( '.cuf_goth-bold:not(.cuf_ro)', {hover:false, fontFamily:'Gotham-Bold'} );
            Cufon.replace( '.cuf_goth-book:not(.cuf_ro)', {hover:false, fontFamily:'Gotham-Book'} );
            Cufon.replace( '.cuf_goth-med:not(.cuf_ro)', {hover:false, fontFamily:'Gotham-Medium'} );
            Cufon.replace( '.cuf_goth-light.cuf_ro', {hover:true, fontFamily:'Gotham-Light'} );
            Cufon.replace( '.cuf_goth-bold.cuf_ro', {hover:true, fontFamily:'Gotham-Bold'} );
            Cufon.replace( '.cuf_goth-book.cuf_ro', {hover:true, fontFamily:'Gotham-Book'} );
            Cufon.replace( '.cuf_goth-med.cuf_ro', {hover:true, fontFamily:'Gotham-Medium'} );
            
            var _body = $('body');
            if(_body.hasClass('AccountsettingsPage') 
				|| _body.hasClass('CallbackPage')
				|| _body.hasClass('CourseReminderPage')
				|| _body.hasClass('JobUpdatesPage')
				|| _body.hasClass('NewsletterPage')
				|| _body.hasClass('PopupForm')
				|| _body.hasClass('PopupPage')
				|| _body.hasClass('UniversityToursPage')
				|| _body.hasClass('RegisterPage')
				|| _body.hasClass('SendtofriendPage')
				|| _body.hasClass('Page')) {
			} else {
	            $('a.popupwinlink').esc_popupwin();
			}

    		// Replace form fields, but not Magento ones
            var $replacable_forms = $('form').filter(function(){
				return $(this).parents('.main-container').length < 1;
			});
			$('select',$replacable_forms).selectbox();
			$('input[type=checkbox]',$replacable_forms).cd_checkbox();
        }

        // Create any of the basic Slideshows
        $('.std-slideshow').esc_stdslideshow();
        
		// Watermark form fields, but not Magento ones
		var $watermarkable_forms = $('form').filter(function(){
			return $(this).parents('.main-container').length < 1;
		});
        $('input[type=password],input[type=text]:not(.selectbox),textarea', $watermarkable_forms).watermark();
        
        // Creating custom :external selector
        $.expr[':'].external = function(obj){

			// PDF links should open in a new window
			if(obj.href.match(/\.pdf$/)) {
				return true;
			}

            return !obj.href.match(/^mailto\:/)
                    && (obj.hostname != location.hostname);
        };
        
        // Add 'external' CSS class to all external links
        $('a:external').attr('target', '_blank');
        
        // START: IE6 Only
        if( $.browser.msie && $.browser.version.substr(0,1) < 7 ) {
            // Start: Account dropdown
                var accountdropdown_active = false;
                $('#top_bar .account .loggedin > li > ul').width( $('#top_bar .account .loggedin > li').width() + 25);
                $('#top_bar .account .loggedin > li').hover(function() {
                    accountdropdown_active = true;
                    $(this).children('ul')
                        .show()
                        .hover(function() {}, function() {
                            accountdropdown_active = false;
                            $(this).hide();
                        });
                    
                }, function() {
                    if( accountdropdown_active == false ) {
                        $(this).children('ul').hide();
                    }
                });
            // End: Account dropdown
            
            // Start: Country Sites Dropdown
            $('#top_bar #navigation #countrysites > li').hover(function() {
            	$(this).children('ul').show();
            }, function() {
            	$(this).children('ul').hide();
            });
            // End: Country Sites Dropdown
        }
        // END: IE6 Only
        
    }); 
})(jQuery);

