/*
 * Multi-level Drop Down Menu 3.0
 * April 17, 2010
 * Corey Hart @ http://www.codenothing.com
 */ 
(function( $, window, undefined ){
    // Needed for IE Compatibility (Closing menus must be done backwards in IE)
    // Ensure that no complications arise from other libraries modifying the 
    // array functionality (and hope that they store the old reverse function into _reverse)
    var el, a = Array.prototype, Reverse = a._reverse || a.reverse;

    // bgiframe is needed to fix z-index problem for IE6 users.
    // For applications that don't have bgiframe plugin installed, create a useless 
    // function that doesn't break the chain
    function emptyfn(){
        return this;
    }

    // Cache common event functions so they aren't instantiated with each event
    function clearSiblings(){
        $( el = this ).children('a').removeClass( $.data( el.parentNode , 'multi-ddm-classname' ) );
    }
    function oldMenus(){
        $( el = this ).hide().siblings('a').removeClass( $.data( el.parentNode.parentNode , 'multi-ddm-classname' ) );
    }

    
    // Expose the drop down menu
    $.fn.dropDownMenu = function( options ) {
        return this.each(function(){
            // Defaults with metadata support
            var $main = $(this), i = 0, $menu, timeout,
                settings = $.extend({
                    timer: 500,
                    parentMO: undefined,
                    childMO: undefined,
                    bgiframe: undefined,
                    levels: []
                }, options || {}, $.metadata ? $main.metadata() : {}),

                // Check on every initiation, so bgiframe can be loaded after this plugin
                bgiframe = $.fn.bgiframe || $.fn.bgIframe || emptyfn;

            // Loop through each level, attach the bgiframe and store it's classname
            $menu = $main.data( 'multi-ddm-classname', settings.levels[ 0 ] || settings.parentMO || settings.childMO || '' );
            while ( $menu.length > 0 ) {
                $menu = bgiframe.call(
                    $menu.find('> li > ul').data( 'multi-ddm-classname', settings.levels[ ++i ] || settings.childMO || '' ),
                    settings.bgiframe
                );
            }

            // Use event delegation to track mouse movement across the menu
            $main.delegate( 'li', 'mouseenter.multi-ddm', function(){
                var self = $( el = this );

                if ( timeout ) {
                    clearTimeout( timeout );
                }

                // Close old menus and remove hover of non-menus
                Reverse.call( self.siblings('li').find('ul:visible') ).each( oldMenus ).end().each( clearSiblings );

                // Open new menu and remove any lingering hover elements
                self.children('a').addClass( $.data( el.parentNode, 'multi-ddm-classname' ) ).siblings('ul').show()
                    .children('li').each( clearSiblings );
            })
            .bind( 'mouseleave.multi-ddm', function(){
                timeout = setTimeout( closemenu, settings.timer );
            });
    
            // Closes all open menus
            function closemenu(){
                // Clear mouseovers
                $main.find('li').each( clearSiblings );

                // Close Menus backwards for IE Compatibility
                Reverse.call( $main.find('ul:visible') ).hide();

                if ( timeout ) {
                    clearTimeout( timeout );
                }
            }
            
            // Allows user option to close menus by clicking outside the menu on the body
            $( window.document ).bind( 'click.multi-ddm', closemenu );
        });
    };
})( jQuery, window || this );


/****************************** TABBED PANELS *******************************/


(function(jQuery){ 
     jQuery.fn.extend({  
         tabify: function() {
			function getHref(el){
				hash = jQuery(el).find('a').attr('href');
				if(hash)
					return hash.substring(0,hash.length-4);
				else
					return false;
				}
		 	function setActive(el){
				jQuery(el).addClass('active');
				if(getHref(el))
					jQuery(getHref(el)).show();
				else
					return false;
				jQuery(el).siblings('li').each(function(){
					jQuery(this).removeClass('active');
					jQuery(getHref(this)).hide();
				});
			}
			return this.each(function() {
				var self = this;
				
				jQuery(this).find('li>a').each(function(){
					jQuery(this).attr('href',jQuery(this).attr('href') + '-tab');
				});
				
				function handleHash(){
					if(location.hash)
						setActive(jQuery(self).find('a[href=' + location.hash + ']').parent());
				}
				if(location.hash)
					handleHash();
				setInterval(handleHash,100);
				jQuery(this).find('li').each(function(){
					if(jQuery(this).hasClass('active'))
						jQuery(getHref(this)).show();
					else
						jQuery(getHref(this)).hide();
				});
            }); 
        } 
    }); 
})(jQuery);





/******************************* SWAP IT *************************************/
(function($) {

jQuery.fn.canvasSwap = function(options) {
    	
    var options = jQuery.extend({}, jQuery.fn.canvasSwap.defaults, options);
    
    jQuery(this).hover (
    
    	function () {
    		
    		var thesrc = $(this).attr('src');
    	
    		var name = thesrc.substring(0, thesrc.lastIndexOf('.'));
    	
    		var extension = thesrc.substring(thesrc.lastIndexOf('.'));
    		
    		$(this).attr('src', name + options.suffix + extension);
    		
    	}, function () {
    		
    		var thesrc = $(this).attr('src');
    		
    		var name = thesrc.substring(0, thesrc.lastIndexOf('.') - options.suffix.length);
    		
    		var extension = thesrc.substring(thesrc.lastIndexOf('.'));
    		
    		$(this).attr('src', name + extension);
    		
    	}
    	
    );
    
    if(options.ie6_support == true) {
    	
    	if(jQuery.browser.version == "5.5" || jQuery.browser.version == "6.0") {
    	
    		jQuery(this).next().hover (
    		
    			function () {
    			
    				var thefilter = $(this).css('filter').substring(56);
        	
        			var thesrc = thefilter.substring(0, thefilter.length-24);
        	
        			var name = thesrc.substring(0, thesrc.lastIndexOf('.'));
        		
        			var extension = thesrc.substring(thesrc.lastIndexOf('.'));
        		
        			$(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + name + options.suffix + extension + '\', sizingMethod=\'scale\')');
    				
    			}, function () {
    			
    				var thefilter = $(this).css('filter').substring(56);
        	
        			var thesrc = thefilter.substring(0, thefilter.length-24);
        			
        			var name = thesrc.substring(0, thesrc.lastIndexOf('.') - options.suffix.length);
    		
    				var extension = thesrc.substring(thesrc.lastIndexOf('.'));
        	
        			$(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + name + extension + '\', sizingMethod=\'scale\')');
    				
    			}
    			
    		);
    		
    	}
    	
    }
    
};

jQuery.fn.canvasSwap.defaults = {
	
	suffix: '_over',
	
	ie6_support: false
	
}

})(jQuery);

/****************************** DOCUMENT READY *******************************/
jQuery(document).ready(function(){
								
//swap it								
	$.fn.canvasSwap.defaults.suffix = '_hover';
    $.fn.canvasSwap.defaults.ie6_support = true;
    $('img.swapit').canvasSwap();

						   
// pane-list
	jQuery("ul.pane-list li, ol.pane-list li, p.pane-list, div.pane-list, span.pane-list").click(function(){
    	window.location=jQuery(this).find("a").attr("href"); return false;
		});
	
// navigation
	jQuery('#multi-ddm').dropDownMenu({parentMO: 'parent-hover', childMO: 'child-hover1'});

// sliding panels
	jQuery(".menu_list div.menu_head").click(function(){
		jQuery(this).toggleClass('active').next("div.menu_body").slideToggle(300).siblings("div.menu_body").slideUp("slow");
		jQuery(this).siblings("div.menu_head").removeClass('active')
		});

// tabbed panels
	jQuery('#menu').tabify();

// append .navlink to top level navigation
	jQuery("#multi-ddm>li>a").addClass("navlink");

// append .first & .last to top level navigation
	jQuery("#mn-topnav>ul>li:first>a").addClass("first");
		jQuery("#mn-topnav>ul>li:last>a").addClass("last");

// append #content-top
    //ONLOAD CHECK CONDITION AND APPLY CLASS BUT NOT TO FIRST "a"
    jQuery("#mn-topnav li a:not(:first)").each(function(){
		//LETS CHECK EACH HREF
		checklink = this.href;
		//LETS SEE IF WE FIND AN # IN THE HREF
		check = checklink.indexOf('#');
		//IF WE DONT FIND ONE ADD THE CLASS
		if (check <= -1)
			{ jQuery(this).addClass("top-anchor"); }
		});
		//YOURS TRULY : LEYLA BALAREZO
	jQuery("#mn-topnav a.top-anchor").each(function(){
		var newHref = jQuery(this).attr("href") + "#mn-main";
		jQuery(this).attr("href",newHref);
		});
	
	
	jQuery("#mn-topnav li a:not(:first)").each(function(){
             // GET THE HOSTNAME
             url = new RegExp(location.host);

            // =CHECK THE LINK’S HREF ATTRIBUTE TO SEE IF IT CONTAINS THE HOSTNAME
             check2=url.test(jQuery(this).attr('href'))

            //IF THE LINK IS EXTERNAL, ADD THE BLANK ATTRIBUTE                                
            if (!(check2)) 
           { jQuery(this).attr('target', '_BLANK'); }
             });


// anchorlist
	var sublinks = jQuery("#multi-ddm").find("li.current-menu-item").find("ul").html();
		jQuery("ul#sublinks").html(sublinks);

// append .first & .last to bottom lists
	jQuery("#mn-bottom ul:first").addClass("first");
		jQuery("#mn-bottom ul:last").addClass("last");
	jQuery("#mn-footer ul#footernav li a:first").addClass("first");
		jQuery("#mn-footer ul#footernav li a:last").addClass("last");
	jQuery("#mn-footer ul#footerlinks li a:first").addClass("last");
			jQuery("#mn-footer ul#footerlinks li a:last").addClass("first");

// social network bottom icon swap
	var template_directory = jQuery('meta[name="template_directory"]').attr("content");
		jQuery('.socialnets a[href*="facebook"]').html('<img src="' + template_directory + '/images/icons/facebook.png" />');
		jQuery('.socialnets a[href*="twitter"]').html('<img src="' + template_directory + '/images/icons/twitter.png" />');
		jQuery('.socialnets a[href*="linkedin"]').html('<img src="' + template_directory + '/images/icons/linkedin.png" />');
		jQuery('.socialnets a[href*="youtube"]').html('<img src="' + template_directory + '/images/icons/youtube.png" />');
		jQuery('.socialnets a[href*="myspace"]').html('<img src="' + template_directory + '/images/icons/myspace.png" />');

// fancybox classing
//	jQuery("#mn-content a").has("img").addClass("lightboxed");
//		jQuery("a.lightboxed").fancybox();
		

		
		
}); //close doc ready

