var obj, obj2;
var dimages=new Array();
//var numImages=(document.getElementById('cantImages').value);
var curImage=-1;

function toggleYear(elementId) {
	if (document.getElementById){

		monthsElmt = document.getElementById(elementId);
		window.alert(monthsElmt.className);
		if (monthsElmt.className == "selected"){
			monthsElmt.className = "";
		} else {
			monthsElmt.className = "selected";
		}
	}
}

//change mouse over image for the cabezote buttons
function swapMenuBtn(btn) {
	if( !$(btn).src.endsWith('sel.gif') ) 
	{
		if ( $(btn).src.endsWith('rr.gif') ) 
			btn.src = $(btn).src.sub('rr.gif', '.gif');
		else
			btn.src = $(btn).src.sub('.gif', 'rr.gif');
	}
}

function preLoadGalleryImages() {
	if (document.images)
	{
		pic1= new Image(100,25);
			pic1.src="http://www.aunotribeca.com/Site_Images/button_cuadrado.gif";
				
	}
}
// Preload images to avoid slow roll-over efect
function preLoadImages(){

	if (document.images)
	{
			pic1= new Image(100,25);
			pic1.src="http://www.aunotribeca.com/Site_Images/cabezote/homerr.gif";
			
			pic2= new Image(100,25);
			pic2.src="http://www.aunotribeca.com/Site_Images/cabezote/aunoonlinerr.gif";
			
			pic3= new Image(100,25);
			pic3.src="http://www.aunotribeca.com/Site_Images/cabezote/aboutusrr.gif";
			
			pic4= new Image(100,25);
			pic4.src="http://www.aunotribeca.com/Site_Images/cabezote/contactusrr.gif";
			
			pic5= new Image(100,25);
			pic5.src="http://www.aunotribeca.com/Site_Images/cabezote/locationsrr.gif";
			
			pic6= new Image(100,25);
			pic6.src="http://www.aunotribeca.com/Site_Images/cabezote/foryourr.gif";
			
			pic7= new Image(100,25);
			pic7.src="http://www.aunotribeca.com/Site_Images/cabezote/viewcartrr.gif";
			
			pic8= new Image(100,25);
			pic8.src="http://www.aunotribeca.com/Site_Images/cabezote/signinrr.gif";
			
			pic9= new Image(100,25);
			pic9.src="http://www.aunotribeca.com/Site_Images/button_cuadrado.gif";
	
	}
}

function onStart(){
	// JSon stuff	
	document.getElementById('cmbColors').onchange = updateCombo;	
	document.getElementById('cmbSizes').onchange = update3Combo;
	
	json=document.getElementById("json_text").value;
	json2=document.getElementById("json_text2").value;
	 
	
	obj=eval('('+json+')');	
	obj2=eval('('+json2+')');
	
	
	document.createElement("ul");
	for(val in obj){	    
	    load_combo ("cmbColors", obj[val]);		
	}		
	
	// Forse first update
	updateCombo();
	
	// Automatic Gallery stuff

	/*for (i=0; i<numImages; i++)
	{
	  dimages[i]=new Image();    
	  dimages[i].src=document.getElementById('image'+i).value;
	  
		
	}
	
	swapPicture();
	setTimeout("swapPicture()", 5000);*/
	
	
}
// Sacado de = http://java-programming.suite101.com/article.cfm/creating_dynamic_comboboxes_with_javascript
function add_option (select_id, text) {
	var select = document.getElementById(select_id);
	select.options[select.options.length] = new Option(text);
}

function clear_combo (select_id) {
	var select = document.getElementById(select_id);
	select.options.length = 0;
}

function load_combo_condition (select_id, option_array, condition) {

	
	for (var i = 0; i < option_array.length; i++) {			
		if (trim(option_array[i])==condition){				
				add_option (select_id, option_array[i+1]);
				
			}			
	}
}


function load_3combo_condition (select_id, option_array, condition1, condition2) {
	
	for (var i = 0; i < option_array.length; i++) {		
		if (trim(option_array[i])==condition1){
			if (trim(option_array[i+1])==condition2){				
				addNumbers(select_id, option_array, i);				
			}							
		}			
	}
}

function addNumbers(select_id, option_array, index)
{
	for(var i=1; i<=option_array[index+2]; i++){
		add_option (select_id, i);		
		document.getElementById('skuID').value = option_array[index+3];
	}
}

function load_combo (select_id, option_array) {				
			
	for (var i = 0; i < option_array.length; i++) {
		if (typeof(option_array[i]) != "undefined")			
			add_option (select_id, option_array[i]);
	}
}

function updateCombo () {
	 
	clear_combo ("cmbSizes");
	
	for(val2 in obj2){  

		var combo = document.getElementById("cmbColors");	
		var indice = combo.selectedIndex;
	    var valor = combo.options[combo.selectedIndex].text;			
		load_combo_condition("cmbSizes", obj2[val2],valor);				
	}		

	// Update the last combo as well
	update3Combo();		
}

function update3Combo () {	 
	
	clear_combo ("cmbQuantity");	
	
	for(val2 in obj2){	   

		var combo = document.getElementById("cmbColors");	
		var indice = combo.selectedIndex;
	    var valor = combo.options[combo.selectedIndex].text;

	    var combo2 = document.getElementById("cmbSizes");	
		var indice2 = combo2.selectedIndex;
	    var valor2 = combo2.options[combo2.selectedIndex].text;		
	    		
		load_3combo_condition("cmbQuantity", obj2[val2],valor,valor2 );				
	}			
}



function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}


function roll(img_name, img_src){
   document[img_name].src = img_src;
}

var hide= true;

var isHidden=new Array();
isHidden[0] = false;
isHidden[1] = false;
isHidden[2] = false;
isHidden[3] = false;

function toggleRows(start, end, tag, who){

	who= 0;
	tableId= "myTable";
	tbl = document.getElementById(tableId);

	var len = tbl.rows.length;
	var vStyle = (isHidden[who])? "none":"";

	for(i=start ; i<end; i++){
		tbl.rows[i].style.display = vStyle;
	}


	if(hide){		
		isHidden[who]= !isHidden[who];
	}else{
		
		isHidden[who]= !isHidden[who];
		
	}

}


function load(){
	toggleRows(5,10, this);

}

function toggleMenu(){
	toggleRows(1,21, this);
}

// Gallery stuff

function swapPicture()
{
	
  if (document.images){
    var nextImage=curImage+1;
    if (nextImage>=numImages)
      nextImage=0;
    if (dimages[nextImage] && dimages[nextImage].complete){
      var target=0;
      if (document.images.myImage)
        target=document.images.myImage;
      if (document.all && document.getElementById("myImage")){    	  
	    	target=document.getElementById("myImage");	    		        
      }
  
      // make sure target is valid.  It might not be valid
      //   if the page has not finished loading
      if (target){
        target.src=dimages[nextImage].src;        
        curImage=nextImage;        
      }

      setTimeout("swapPicture()", 5000);

    }
    else
    {
      setTimeout("swapPicture()", 5000);
    }
  }
}


function manageMenu(element){
	var ulName = element.id + 'Items';
	
	if($(ulName).visible()){	
		$(ulName).hide();		
	} 
	else {
		$(ulName).show();
	}	
}
