/* 
* variable assignment  for the following are defined in either /product/default.aspx or /product/productId/default.aspx
* path
* productId
*/
var productId = null;
var section = null; // this is the default state
var d = YAHOO.util.Dom;
var initSection;

  // Instantiate and configure Loader:

    var loader = new YAHOO.util.YUILoader(
    {
      // Identify the components you want to load.  Loader will automatically identify
      // any additional dependencies required for the specified components.
      require: ["animation","connection","container","event","history","json","swfobject"],
      base: '/app/includes/js/yui/',
      loadOptional: false,
      onSuccess: function() {
           initProductPage();
      },
      
      onProgress: function(o) { },
      onFailure: function() { }
    });
  
  
  loader.addModule({
	name: "swfobject",
	type: "js", 
  fullpath: "/app/includes/js/swfobject.js"
  });
  
  
function initProductPage(){
  initSection = YAHOO.util.History.getBookmarkedState("s") || "";
  // register the history object with our callback function and initial state
  
  YAHOO.util.History.register("s", initSection, function (state){       
    //load new section and set tab
    if(state.length>0)
      getProductSectionControl(state,currentPath);
  });
  
  // called on page load
  YAHOO.util.History.onReady(function () {
    var state = YAHOO.util.History.getCurrentState("s");
    // get section data
    if(state.length>0)
      getProductSectionControl(state, currentPath);     
  });
  
  YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
  productTabsInit();
}


/*
* this method/function will loop through each product navigation tab and add the
* event handlers needed to enable ajax
*/
 
 function productTabsInit(){
  var productNavigation = d.get("productNavigation");
  var navElements = d.getElementsBy(function(e){ return (e.nodeName=="A");}, "A", productNavigation);

  for (var i=0; i<navElements.length; i++){
    section = navElements[i].parentNode.id;
    if(section=="overview")
      section="";
      
    if(currentPage=="default.aspx") { //on our default page init tabs to use ajax
      navElements[i].setAttribute("href", "javascript:void(0)");
      YAHOO.util.Event.addListener(navElements[i], 'click', tabOnClick);
    } else {
      // not on default so chang tab links to point to ajaxified page
      navElements[i].setAttribute("href", "default.aspx#s=" + section);
    }
  }
}

// obj should be the link the user has clicked or to make active
function setActiveTab(obj, currentPath, section){
  var unselectTabs = d.getElementsBy(function(e){ return (e.nodeName=="TD");}, "TD", obj.parentNode.parentNode.id); 
  d.replaceClass(unselectTabs, "selected", " ");
  d.addClass(obj.parentNode.id, "selected");
  pageTracker._trackPageview(currentPath + section);
}

var tabOnClick = function(e){  // e is event
  // update tabs states
  YAHOO.util.History.navigate("s", this.parentNode.id);
};

function getProductSectionControl(section, currentPath){
	d.get("productSection").innerHTML = '<img src="/app/assets/images/loading_animation.gif" style="padding: 163px 0 0 435px;" />';
 
	var sUrl = "/app/services/getProductSectionControl.ashx?path=" + currentPath + "&section=" + section;
	var responseHtml;

	var callback = {
		success: function(o) {
		  var responseHtml = o.responseText; //get this from cache
     // var doFadeOut = new YAHOO.util.Anim("productSection", { opacity: { to: 0 } }, 0.2);
      
      //var doFadeIn = function(type, args) {
        d.get("productSection").innerHTML = responseHtml;  
        //var fade = new YAHOO.util.Anim("productSection", { opacity: { to: 1 } }, 0.2);
        //fade.onComplete.subscribe(function(){setActiveTab(d.getFirstChild(d.get(section)), currentPath, section);});
        //fade.animate();
        setActiveTab(d.getFirstChild(d.get(section)), currentPath, section);
      //};
      
      if(d.inDocument("flashScript")){
        eval(d.get("flashScript").text);
      }
      
      addTrackingClickEvents("overlayContent");
      
      if(YAHOO.util.Dom.inDocument("evalJs")){
        YAHOO.log(document.getElementById("evalJs").text);
        eval(document.getElementById("evalJs").text);
       }
            //doFadeOut.onComplete.subscribe(doFadeIn);
      //doFadeOut.animate();
		}, 
		
		failure: function(o) {}
		};

	var transaction = YAHOO.util.Connect.asyncRequest("GET", sUrl, callback, null);	
}

function setoverLayMenuWidth(){
    var menuTbl = d.get("productNavigation");
    if(menuTbl != null){
        var menuTd = d.getChildren(menuTbl);
        var width = Math.round(100/menuTd.length);
        for (var i=0; i<menuTd.length; i++){
           if(i == 0){d.addClass( menuTd[i],"first")} 
           if(i ==(menuTd.length -1)){d.addClass( menuTd[i],"last")} 
           d.setStyle( menuTd[i] , "width" , width+"%" ) 
        }
    }
}

function productPageInit(){
      /*  
      *   This file is shared between two locations. only call this method if we are
      *   this is running in a non overlay mode.
      */
     
      if(!d.inDocument("filterContainer") && currentPage == "default.aspx"){ //filterContainer will only exists in overlay mode
        loader.insert(); //load required js files   
      } 
      setoverLayMenuWidth();
}

var d = YAHOO.util.Dom;
function toggleImg(obj){
    
    theSelectedLi =  d.getElementsByClassName("selected" , "li" , d.getAncestorByTagName( obj ,"ul"));
    if(theSelectedLi.length ==1){
        d.removeClass(theSelectedLi[0], "selected");
    }
    var thumbSrc = d.getFirstChild(obj).src;
    stageSrc = thumbSrc.replace("_thumb", "");
    d.addClass(obj, "selected")
    d.get("stage").src =  stageSrc;
}

YAHOO.util.Event.addListener(window, 'load', productPageInit);