/* productmenu.js
 * 
 * desc: contains the logic for swapping tabbed content on a product page.
 */

var sku = "";
var current_prefix = "itfm";

// will switch all the page areas to the appropriate
// parts.  expects a sku.
function switch_to_product(product_id,level) {
	var header_id = 'head-' + product_id
        sku = product_id
	shuffle('head-' + product_id);
	shuffle('box-' + product_id);
	if ($('guaranteed-' + product_id) != null) {
		shuffle('guaranteed-' + product_id);
	}
        
	activate('tab-' + product_id);

        //Eschool faq tabs (if homeschool level=homeschool)
        if(level != "homeschool"){
          if(level.indexOf("eschool") != -1){
            document.getElementById('faqs').style.display = 'block';
          }else{
            document.getElementById('faqs').style.display = 'none';
          }
        }

	if ($("sp-audio_companion_cd-" + product_id)) {
		$('audio_companion_cd').show();
	} else {
		if ($('audio_companion_cd') != null) {
			$('audio_companion_cd').hide();
			if ($('audio_companion_cd').className == 'active') {
				current_prefix = "itfm";
				activate('is_this_for_me')
			}
		}
	}
        
	if (current_prefix) {
		shuffle(current_prefix + "-" + product_id);
	}
}

function tabless_switch_to_product(product_id) {

        var header_id = 'head-' + product_id
        sku = product_id

        shuffle('head-' + product_id);
        shuffle('box-' + product_id);
        if ($('guaranteed-' + product_id) != null) {
                shuffle('guaranteed-' + product_id);
        }



        shuffle("sp-audio_companion_cd");
}

// hides all siblings of an element and shows
// that element.  taking the card you want to
// the top of the deck.
function shuffle(element_id) {
        var siblings = $(element_id).ancestors()[0].immediateDescendants();
	for (var i = 0; i < siblings.length; i++) {
		if (siblings[i].id != element_id) {
			//alert("hiding: " + headers[i].id)
			siblings[i].hide();
		}
	}
	$(element_id).show();
}

function select_tab(level) {
   for( var i = 0; i < $('main-tab').childNodes.length; i++){
       if( $('main-tab').childNodes[i].className == 'active' ){
           $('main-tab').childNodes[i].className = "";
       }
   }
  $(level).className = "active";
}

// 'sub-left', and 'main-tab' are the likely targets
// here .. 
// switches the active flag on for a submenu element.
function activate(obj_id) {
	var parent = $(obj_id).ancestors()[0];
	for( var i = 0; i < parent.childNodes.length; i++){
       if( parent.childNodes[i].className == 'active' )
           parent.childNodes[i].className = "";
	}
	$(obj_id).className = "active";
   
}

// shows a subpanel, given the prefix. 
function show_subpage(prefix) {
    current_prefix = prefix;
    shuffle (prefix + "-" + sku);
}

// LEGACY

function clicktab(silo, level, sku){
  if (sku.match("OSUB")){
    new Ajax.Updater('content-area-right', '/render_part/product_boxshot?sku=OSUB');
  } else {
    new Ajax.Updater('content-area-right', '/render_part/product_boxshot?sku=' + sku);
  }
  new Ajax.Updater('content-style-sub-left','/render_part/part_listing?page=/' + silo + '/levels/' + level + '&sku=' + sku);
  if (silo == "personal") {
     new Ajax.Updater('content-style-sub-right','/render_part/is_this_for_me?page=/' + silo + '/levels/' + level + '&sku=' + sku); 
  } else {
     new Ajax.Updater('content-style-sub-right','/render_part/what_do_i_get?page=/' + silo + '/levels/' + level + '&sku=' + sku);
  }
  new Ajax.Updater('content-style-sub-header','/render_part/product_description_and_buy?sku=' + sku);

  select_tab(level);
}

function part_click(part, url, sku){

   new Ajax.Updater('content-style-sub-right','/render_part/' + part + '?page=' + url + '&sku=' + sku);

   for( var i = 0; i < $('sub-left').childNodes.length; i++){
       if( $('sub-left').childNodes[i].className == 'active' )
           $('sub-left').childNodes[i].className = "";
   }
      

  $(part).className = "active";

}

function get_level(){

   var loc = document.location.href;

   var ind = loc.indexOf('=');

   var level = loc.substring(ind+1, loc.length);

   return level;
}


  


