var base = new Object();

function quickviewInit(){
	$$('.prodimg').each(function(el){
		el.addEvent('mouseenter',function(me){
			var popper = el.getLast('div');
			if(popper != null){
				popper.setStyle('display','block');
			}
		});
		el.addEvent('mouseleave',function(ml){
			var popper = el.getLast('div');
			if(popper != null){
				popper.setStyle('display','none');
			}
		});
	});
}

function checkDispose(item,pid){
	$each(base['pid'+pid],function(v,i){
		if(base['pid'+pid][i]['prev'] == item && $('prodvar_'+pid+'_'+i)){
			checkDispose(i,pid);
		}
	});
	$('prodvar_'+pid+'_'+item).dispose();
	
	if(item == 'color' && $('pd_varscolors')){
		$('colhider').setStyle('display','block');
	}
}
function doDropdownInit(pid){
	var selelement;
	var optelement;
	$each(base['pid'+pid],function(value,index){
		if($('prodvar_'+pid+'_'+base['pid'+pid][index]['prev'])){
			$('prodvar_'+pid+'_'+base['pid'+pid][index]['prev']).removeEvents();
			$('prodvar_'+pid+'_'+base['pid'+pid][index]['prev']).addEvent('change',function(event){
				(function(){
					if($('prodvar_'+pid+'_'+index)){
						//$('prodvar_'+index).dispose();
						//$each(base,function(v,i){
						//	if(base[i]['prev'] == index && $('prodvar_'+i)){
						//		$('prodvar_'+i).dispose();
						//	}
						//});
						checkDispose(index,pid);
					}
					if($('prodvar_'+pid+'_'+base['pid'+pid][index]['prev']).value != ''){
						selelement = new Element('select', {name:'prodvar_'+index.toLowerCase(),id:'prodvar_'+pid+'_'+index,'class':'required'});
						if($('pd_varscolors')){
							$('colhider').setStyle('display','none');
							selelement.addClass('inputboxattrib');
							selelement.setStyle('margin','1px 0 0 22px');
							selelement.setStyle('width','104px');
							selelement.inject($('pd_varscolors'),'bottom');
						}
						else{
							selelement.setStyle('width','100px');
							selelement.inject($('secondlevel'),'bottom');
						}
						optelement = new Element('option',{value:'',html:'Select a Variety',selected:'selected'});
						optelement.inject('prodvar_'+pid+'_'+index);
						$each(base['pid'+pid][index]['value'],function(v,i){
							var shortcut = $('prodvar_'+pid+'_'+base['pid'+pid][index]['prev']);
							if(base['pid'+pid][index]['constraints'][i] == ''){
								optelement = new Element('option', {value:base['pid'+pid][index]['optvalue'][i],html:v+' '+base['pid'+pid][index]['price'][i],'myinfo':v});
								optelement.inject('prodvar_'+pid+'_'+index);
							}
							else {
								 var splitter_array = base['pid'+pid][index]['constraints'][i].split('|');
								 var found = false; 
								 //alert(splitter_array);
								 splitter_array.each( function(s){
								 	if (s == shortcut.options[shortcut.selectedIndex].getAttribute('myinfo')){
								 		found = true;
								 	}
								 });
								 if (found == false){
									optelement = new Element('option', {value:base['pid'+pid][index]['optvalue'][i],html:v+' '+base['pid'+pid][index]['price'][i],'myinfo':v});
									optelement.inject('prodvar_'+pid+'_'+index);								 
								 }
								 //|| base['pid'+pid][index]['constraints'][i].indexOf(shortcut.options[shortcut.selectedIndex].getAttribute('myinfo')) == -1){
							}
							//if(base['pid'+pid][index]['constraints'][i] == '' || base['pid'+pid][index]['constraints'][i].indexOf(shortcut.options[shortcut.selectedIndex].getAttribute('myinfo')) == -1){
							//	optelement = new Element('option', {value:base['pid'+pid][index]['optvalue'][i],html:v+' '+base['pid'+pid][index]['price'][i],'myinfo':v});
							//	optelement.inject('prodvar_'+pid+'_'+index);
							//}							
						});
						doDropdownInit(pid);
					}
					//doImageSwap($('prodvar_'+pid+'_'+base['pid'+pid][index]['prev']).value);
					//allSelectsInit();
				}).delay(10);
			});
		}
	});
}

function imageSwitchInit(){
	$$('.pd_image_extras a').each(function(e){
		e.addEvent('click',function(el){
			var num = e.getProperty('id').substring(9) * 1;
			$$('.pd_insideimage').each(function(pd){
				pd.setStyle('display','none');
			});
			if(num != 0){
				$('insideimage'+num).setStyle('display','block');
				$('insideimage'+num).setStyle('height','auto');
			}
			else{
				$('origimage').setStyle('display','block');
			}
			return false;
		});
	});
}

/***** This would be really cool if I could figure a way to get it to work correctly *
*      But, I cant :)
*
function allSelectsInit(){
	//$$('.pd_insideimage').each(function(e){
	//	if(e.getProperty('id') != 'origimage'){
	//		//e.setStyles({
	//	}
	//});
	$$('.pd_varieties select').each(function(e){
		if(!e.hasEvent('change')){
			e.addEvent('change',function(el){
				(function(){
					doImageSwap(e.value);
				}).delay(10);
			});
		}
	});
}

function doImageSwap(prodvarid){
	if($('insideimage'+prodvarid)){
		$('origimage').setStyle('display','none');
		$('insideimage'+prodvarid).setStyle('display','block');
		$('insideimage'+prodvarid).setStyle('height','auto');
	}
	else if($('origimage')){
		if($('origimage').getStyle('display') == 'none'){
			$$('.pd_insideimage').each(function(pd){
				pd.setStyle('display','none');
			});
			$('origimage').setStyle('display','block');
		}
	}
}
*****/
