$(document).ready(function(){

//Dropdown Nav
	// div tag with class='nav_top_dropdown'
	$('.nav_top_dropdown').hover(
		function () {
			$(this).addClass('nav_down');
		},
		function (e) {
			
			//$(this).removeClass('nav_down').animate({'top': '-120px'}, 1000);
			
			var offset = $(this).offset();
			var x = e.pageX - offset.left;
			var y = e.pageY - offset.top + 15;
			var position = $(this).position();
			var left = position.left;
			var top = position.top;
			var right = $(this).width();
			var bottom = $(this).height();
			
			if ( x < left || x > right || y < top || y > bottom ) {
				
				$(this).removeClass('nav_down').animate({'top': '-120px'}, 1000);
			}
		}
	);
	
	//li tag with class='solutions'
	$('.solutions').hover(
		function () {	
			$('.nav_top_dropdown').animate({'top': '+60px'}, 1000).addClass('nav_down');
		},
		function(e){
			var offset = $(this).offsetParent().offset();
			var x = e.pageX - offset.left;
			var y = e.pageY - offset.top + 15;
			var position = $('.nav_top_dropdown').position();
			var left = position.left;
			var top = position.top;
			var right = left + $('.nav_top_dropdown').width();
			var bottom = top + $('.nav_top_dropdown').height();
			
			if ( x < left || x > right || y < top || y > bottom ) {
				
				$('.nav_top_dropdown').removeClass('nav_down').animate({'top': '-120px'}, 1000);
			}
		}
	);

//Modifying text in Twitter feed at the bottom in the footer
	$('#twitter_update_list li a:last').append(' via twitter');
	
//Navigation Hover Functionality
	$(".nav_top").children("li").each(function() {
		var current = "nav_top current-" + ($(this).attr("class"));
		var parentClass = $(".nav_top").attr("class");
		if (parentClass != current) {
			$(this).children("a").css({backgroundImage:"none"});
		}
	});	

	attachnav_topEvents(".nav_top", "why_canopy");
	attachnav_topEvents(".nav_top", "solutions");
	attachnav_topEvents(".nav_top", "contact_us");

	function attachnav_topEvents(parent, myClass) {
		$(parent + " ." + myClass).mouseover(function() {
			$(this).append('<div class="nav_top-' + myClass + '"></div>');
			$("div.nav_top-" + myClass).css({display:"none"}).fadeIn(300);
		}).mouseout(function() {
			$("div.nav_top-" + myClass).fadeOut(300, function() {
				$(this).remove();
			});
		}).mousedown(function() {
			$("div.nav_top-" + myClass).attr("class", "nav_top-" + myClass + "-click");
		}).mouseup(function() {
			$("div.nav_top-" + myClass + "-click").attr("class", "nav_top-" + myClass);
		});
	}
	
//Used for the temporary coming soon message until the video is setup on the homepage
	$(".video").colorbox({width:"540px", height: "360px", inline:true, href:"#temp_message"});
	
//Image Switch Functionality
     $(".img-swap").hover(
          function(){this.src = this.src.replace("_off","_on");},
          function(){this.src = this.src.replace("_on","_off");
     });

	//Preload Images that will be switched     
	$.fn.preload = function() {
		this.each(function(){
			$('<img/>')[0].src = this;
		});
	}
	
	//Preload Images Usage:
	$(['assets/templates/default/images/header/nav_dropdown_hover_1_hover.png',
	   'assets/templates/default/images/header/nav_dropdown_hover_2_hover.png',
	   'assets/templates/default/images/header/nav_dropdown_hover_3_hover.png',
	   'assets/templates/default/images/header/nav_dropdown_hover_4_hover.png']).preload();
	
	//jQuery Code To Create The Image Array
    // Declare the array variable
    var imgSwap = [];
    // Select all images used in the image swap function - in our case class "img-swap"
    $(".img-swap").each(function(){
        // Loop through all images which are used in our image swap function
        // Get the file name of the active images to be loaded by replacing _off with _on
        imgUrl = this.src.replace("_off","_on");
        // Store the file name in our array
        imgSwap.push(imgUrl);
    });
    // Pass the array to our preload function
    $(imgSwap).preload();
    
//Changing text in input fields of forms
	$('input[type="text"]').addClass("idleField");
		$('input[type="text"]').focus(function() {
			$(this).removeClass("idleField").addClass("focusField");
	    if (this.value == this.defaultValue){ 
	    	this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"]').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
	    if ($.trim(this.value) == ''){
	    	this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
//Anything Slider
	$('#anythingslider').anythingSlider({
		startStopped    : false, // If autoPlay is on, this can force it to start stopped
		width           : 902,   // Override the default CSS width
		height          : 370,   // Override the default CSS height
		delay			: 5000,  // Speed in miliseconds
		easing          : 'easeInOutBack',
		autoPlayLocked  : false,  // If true, user changing slides will not stop the slideshow
		//resumeDelay     : 6000,  // Resume slideshow after user interaction, only if autoplayLocked is true.
		stopAtEnd : false,
		onShowStop : function(e, slider){
			setTimeout(function(){
				if (slider.currentPage === slider.pages) { slider.gotoPage(1); }
			}, 5000);
		}
	});
	
//Tabify
	$('#menu').tabify();
	
});
