jQuery.noConflict();
jQuery(document).ready(function(){
	//*****SCROLLING DETECTOR*****
  var subpage = false;
  
  jQuery(".hub-span-5:first").addClass("first-after-nav");
  
  if(!jQuery("#home").length){ subpage = true; jQuery("body").addClass("subpage"); }
  jQuery("#hero li a img").bind("click", function(e){
    if(jQuery("body").hasClass("collapsed")){
      e.preventDefault();
      subpage = false;
      jQuery("body").removeClass("collapsed").addClass("hpmode");
      jQuery(window).scrollTop(0);
      jQuery("#hero h2, #hero .previous, #hero .next, #hero .shading").fadeIn("fast");
      if(jQuery("#floating-nav").length){
        jQuery("#floating-nav").css("top", (parseInt(jQuery("#floating-nav").css("top")) + 245) + "px");
      }
    }
  });
  
	if ( !(jQuery("#home").length)) {
		jQuery("#hero h2, #hero .previous, #hero .next, #hero .shading").fadeOut("fast");
	}
  
	function detectScroll(){
    if(!subpage){
      if ( jQuery.browser.msie ) {
        if( jQuery(window).scrollTop() > 0 ){
          jQuery("#hero .shading").hide();
          jQuery("#hero h2, #hero .previous, #hero .next").fadeOut("fast");
        }else{
          jQuery("#hero .shading").show()
          jQuery("#hero h2, #hero .previous, #hero .next").fadeIn("fast");
        }	
      }
      else {
        if( jQuery(window).scrollTop() > 0 ){
          jQuery("#hero h2, #hero .previous, #hero .next, #hero .shading").fadeOut("fast");
        }
        else {
            jQuery("#hero h2, #hero .previous, #hero .next, #hero .shading").fadeIn("fast");
        }
      }
      if( jQuery(window).scrollTop() > ((!jQuery("#home").length)? 250 : 250)){
        jQuery("body").addClass("collapsed");
      }else{
        jQuery("body").removeClass("collapsed");
      }
    }
	}
	jQuery(window).bind('scroll', detectScroll);
	
	//*****NAVIGATION*****
	if(jQuery.browser.msie){
		jQuery("#navigation li").hoverIntent(
      function(){ jQuery(this).children("ul").css("display","block"); },
      function(){ jQuery(this).children("ul").css("display","none"); }
    );
	}
  else {
		jQuery("#navigation li").hoverIntent(
      function(){ jQuery(this).children("ul").stop(true,true).slideDown("fast"); },
      function(){ jQuery(this).children("ul").stop(true,true).slideUp("fast"); }
    );
	}
  
	//*****SEARCH#*****	
	jQuery("#header-search-input").click(function(){ 
		jQuery("#header-search-input").css("width","180px");
		jQuery("#header-search-input").attr("value","");
	});

  //TODO make grey border on top of mod with an image that's not first or second
	//console.log(">1>" + jQuery("#content .col .mod:not(.first)").length);
  var scope = jQuery("#content .col .mod:not(.first) .link>img, #content .col .mod:not(:first) .link>img");

	//*****CAROUSEL*****
	carousel("hp-slideshow");	
	function carousel(carouselID){
		var slidesNo = jQuery("#"+carouselID+" ul li").size();
		var slideSelected = 1;
    var speed = 500;
		jQuery("#"+carouselID+" .next").click(function(){
      
			jQuery("#"+carouselID+" li:nth-child("+slideSelected+")").fadeOut(speed);
			slideSelected += 1;
			if(slideSelected > slidesNo) slideSelected = 1;
      setTimeout(function(){
        jQuery("#"+carouselID+" li:nth-child("+slideSelected+")").fadeIn(speed);
      }, speed);
			
		});
		jQuery("#"+carouselID+" .previous").click(function(){
			jQuery("#"+carouselID+" li:nth-child("+slideSelected+")").fadeOut(speed);
			slideSelected -= 1;
			if(slideSelected < 1) slideSelected = slidesNo;
      setTimeout(function(){
        jQuery("#"+carouselID+" li:nth-child("+slideSelected+")").fadeIn(speed);
      }, speed);
		});		
	}
	
	 //*****STOCK QUOTE XML******
    stockLocation = window.location.href;
    stockLocationURL = stockLocation.split(".com/");
    stockLocationURLFinal = stockLocationURL[0]+".com/Storage/FordCom/DataExtract/stockquote.xml";
    //stockLocationURLFinal = "xml/chart-quote.xml";
    if(jQuery("#stock-tools").length){
		jQuery.ajax({
			type: "GET",
			dataType: "xml",
			url: stockLocationURLFinal,
			success: function(xml){
				jQuery(xml).find("Stock_Quote[Company='Ford Motor Company']").each(function(){
					var percentage = (jQuery(this).find("Change").text() * 100) / jQuery(this).find("PreviousClose").text();
					if (jQuery(this).find("Change").text() < 0){
						percentage=percentage* (-1);
						jQuery("#stock-quote-change").addClass("negative");
					}
					jQuery("#stock-quote-company").append(jQuery(this).attr("Company"));
					jQuery("#stock-quote-date").append(jQuery(this).find("Date").text());
					jQuery("#stock-quote-trade").append(jQuery(this).find("Trade").text());
					jQuery("#stock-quote-change").append(jQuery(this).find("Change").text()+" ("+percentage.toFixed(4)+"%)");
					jQuery("#stock-quote-volume").append("Volume "+jQuery(this).find("Volume").text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
					if (jQuery("#stock-quote #stock-quote-date").length){
						jQuery("#stock-quote-date").append(" <i> (30 Min. Delay)</i>");
					}
          jQuery("#stock-quote-open").append(jQuery(this).find("Open").text());
          jQuery("#stock-quote-previous").append(jQuery(this).find("PreviousClose").text());
          jQuery("#stock-quote-high").append(jQuery(this).find("High").text());
          jQuery("#stock-quote-low").append(jQuery(this).find("Low").text());
          jQuery("#stock-quote-high52").append(jQuery(this).find("FiftyTwoWeekHigh").text());
          jQuery("#stock-quote-low52").append(jQuery(this).find("FiftyTwoWeekLow").text());
				});
			},
			error: function(xhr, status, error) {
					//alert("xml file not found");
			 } //end error
		});
 	}
	
});




