//var root_uri = 'http://localhost/designertags/';
var root_uri = 'http://www.designertags.com/';

function goHome() {
	/* emulate home link */
	location.href = 'http://www.designertags.com/';
}

function alterItemsPerPage(ob) {
	window.location = "/products/setperpage/" + ob.value;
}

function alterSort(root_url, ob) {
	window.location = root_url + "products/setsort/" + ob.value;
}

function updateQuantity(ob) {
	var name = ob.name.split('_');
	window.location = root_uri + "bag/quantity/" + name[1] + "/" + ob.value;
}

function checkCardType(ob) {
	var sn = $('switchno');
	sn.style.display 			= (ob.value == 'switch') ? 'table-row' : 'none';
	$('order_card_issue').value = (ob.value == 'switch') ? $('order_card_issue').value : '';
}

function submitForm(formid) {
	$(formid).submit();	
}

function changeContactRegarding() {
	var dd = $('contact_regarding');	
	var or = $('related-order');
	
	if(or) {
	
		/* hide/display order drop-down if order values are deselected/selected */
		or.style.display = (dd.value.split('-').shift().toString() == 'order') ? 'table-row' : 'none';
		$('info-panel').style.display = (dd.value.split('-').shift().toString() == 'order') ? 'table-row' : 'none';
	}
	
}

function changeInfoPanel() {
	var ci = $('contact_order');
	if(!$('info-panel')) {
		return;
	}
	$('info-panel').style.display = (ci.value.length > 0) ? 'table-row' : 'none';
	if(parseInt(ci.value.length) > 0) {
		new Ajax.Request('/contact/getorderinfo/' + ci.value,
		{
			onSuccess: function(transport, json){

				$('info-panel-box').innerHTML = '';
				var str = '';

				if(json != null) { 
					for(var ob in json) { 

						str += json + '<br />';
					}

					$('info-panel-box').innerHTML = str;
					new Effect.Highlight('info-panel-box', { duration: 0.5, startcolor: '#00ccff', endcolor: '#000000', restorecolor:'#000000'} );
				}
				else {
					$('info-panel-box').innerHTML = 'Unknown Order Information.';
				}
			}
		});
	}
	else {
		$('info-panel-box').innerHTML = ci.value.length;
	}
}

function addToCart() { 
	
	// clear any error styles. Not really needed but hey...
	if($('product-options') != undefined) $('product-options').setStyle({ backgroundColor: 'black' });
	if($('product-options-error') != undefined) $('product-options-error').remove();

	// sometimes we don't show the options selector so
	// don't validate it if it isn't there
	if($('variant') != undefined) { 
		if($F('variant') > 0) $('f').submit(); 
		else  { 
			new Insertion.Top('product-options', '<p id="product-options-error">Please select a Product Option below:</p>');
		}
	}

	else $('f').submit(); 
}

function checkBobbyWomackability() {
	
	if( $('variant') != undefined ){
		if ( $('product-options-error') != undefined )  { 
			$('product-options-error').setStyle({ display: 'none' });
		}
		if (was[$('variant').value]) $('was_price').innerHTML = was[$('variant').value] + '*';
		if (variants[$('variant').value]) $('now_price').innerHTML = variants[$('variant').value];
	}
	
	$('initrotate').setStyle({ display: 'none' });
	
}


function updateImages(variantID){
	
	new Ajax.Request('/products/variantimages/' + variantID, {
			onSuccess: function(transport, json){
				
				var str = '';
		
				
				if(json != null) {
					
					for(k = 0; k < json.length; k++) {
						if(json[k].image_default==1){
							var defaultImageObjID = k;
						}
					}
					
					
					if(json.length > 0){
						str = '<a id="mainimage-anchor" href="javascript:Popup.open({url:\'/products/image_popup/' + json[defaultImageObjID].image_id + ', width:\'700\',height:\'450\'});"><img id="product_image" src="/img/product/' + json[defaultImageObjID].image_id  + '.jpg" alt="' + json[defaultImageObjID].image_alt +'" /></a>';
					}
					else{
						str = '<img id="product_image" src="/img/no-image-product.gif" alt="No image available" />';
					}
					
					$('pictureframe_main').innerHTML = str;
		
					
					str = '';
					if(json.length >= 1){
						str = '<a id="zoomlink-anchor" href="javascript:Popup.open({url:\'/products/image_popup/' + json[defaultImageObjID].image_id + ', width:\'700\',height:\'450\'});">Click here to zoom in on image</a>';
					}				
					$('pictureframe_zoombuttom').innerHTML = str;
					
					str = '';
					
					var k = 0;
					
					
				
					
					if(json.length > 1) {
						
						if(json.length>3) var limit = 3;
						else var limit = (json.length);
						
						for(k = 0; k < limit ; k++){
						
							str += '<div class="ihold';
							if(k===2){ str += ' finalhold'; }
							str += '" id="slot';
							str += (k+1) + '">';
							str += '<a class="gilink" id="gilink_' + json[k].image_id + '" style="cursor: pointer;" href="javascript:cycleLarge(' + json[k].image_id + ');"><img src="/img/small/' + json[k].image_id + '.jpg" alt="' + json[k].image_alt +'" /></a>';
							str += '</div>';
						}

					
						
						
						if(json.length > 3){
		
							/*str += '<script type="text/javascript">' +
									'var cpos		= 0;' +
									'var imgrob 	= new Array();' +
									
									'var modifier 	= \'\';'; 
							var b = 0; 
								 
							for(k = 0; k < json.length; k++) {
								str += 'imgrob[' + b + '] = \'<a class="gilink" id="gilink_' + json[k].image_id + '" href="javascript:cycleLarge(' + json[k].image_id + ');"><img src="/img/small/'+ json[k].image_id +'.jpg" alt="Alternative image for '+ json[k].image_alt +' " width="'+ json[defaultImageObjID].image_width + '" height="'+ json[defaultImageObjID].image_height +'" /></a>\';';
								b++; 
							}
							
							str += '</script>';
							
							str += 'function cycleLeft() { ' +
			
								'if(modifier == \'r\') { ' +
							'		cpos++; ' +
							'	}' +
								
							'	cpos = (cpos > imgrob.length) ? 1 : cpos; ' +
								
							'	for(i = 1; i <= 3; i++) { ' +
							'		blimp = ((cpos+i) >= imgrob.length) ? (cpos + i) - imgrob.length : cpos + i; ' +
							'		$(\'slot\' + i).innerHTML = imgrob[blimp]; ' +
							'	} ' +
								
							'	cpos++; ' +
								
							'	modifier = \'l\'; ' +
							'} ' +
							
							'function cycleRight() { ' +
								
							'	if(modifier == \'l\' || !modifier) { ' +
							'		cpos = imgrob.length - 1;' +
							'	}' +
							'	cpos--;' +
							'	cpos = (cpos < 0) ? imgrob.length - 1 : cpos;' +
								
							'	for(i = 1; i <= 3; i++) {' +
							'		blimp = ((cpos+i) >= imgrob.length) ? (cpos + i) - imgrob.length : cpos + i;' +
							'		$(\'slot\' + i).innerHTML = imgrob[blimp];' +
							
							'	} ' +
								
							'	modifier = \'r\'; ' +
							'} ' +
							'</script>' +
							*/
							str += '<div class="rotateholder_dyn"> ' +
                            '    <a href="javascript:cycleRight(' + variantID + ');"><img src="/img/arraright.gif" alt="Rotate Images Right" class="fright" /></a> ' +
                            '    <a href="javascript:cycleLeft(' + variantID + ');"><img src="/img/arraleft.gif" alt="Rotate Images Left" /></a> ' +
                            '</div>';
							 
						}
						
						//alert(str);
						$('pictureframe_gallery').innerHTML = str;
					}
					
				}
				else {
					$('image-area').innerHTML = $('image-area').innerHTML;
				}
			},
			onFailure: function() {
				$('image-area').innerHTML = '';
			}
		});			 

}


/*function cpas(){
	
	

		function cycleLeft() {
			
			if(modifier == 'r') {
				cpos++;
			}
			
			cpos = (cpos > imgrob.length) ? 1 : cpos;
			
			for(i = 1; i <= 3; i++) {
				blimp = ((cpos+i) >= imgrob.length) ? (cpos + i) - imgrob.length : cpos + i;
				$('slot' + i).innerHTML = imgrob[blimp];
			}
			
			cpos++;
			
			modifier = 'l';
		}
		
		function cycleRight() {
			
			if(modifier == 'l' || !modifier) {
				cpos = imgrob.length - 1;
			}
			cpos--;
			cpos = (cpos < 0) ? imgrob.length - 1 : cpos;
			
			for(i = 1; i <= 3; i++) {
				blimp = ((cpos+i) >= imgrob.length) ? (cpos + i) - imgrob.length : cpos + i;
				$('slot' + i).innerHTML = imgrob[blimp];
				//alert('setting slot ' + i + ' to image ' + blimp + '. CPOS is ' + cpos);
			}
			
			modifier = 'r';
		}
		
		function cycleLarge(id) {
			$('product_image').src = "/img/product/" + id + ".jpg";
			$('zoomlink-anchor').href = 'javascript:Popup.open({url:"/products/image_popup/' + id + '", width:"700",height:"450"});';					
		}
		</script>
}
*/



// Image switching handling for the products/view controller
// preload product images and allow quick switching between them

var product_images = new Array();

Event.observe(window, 'load', function() {
	if($$('.gilink') != '') { 
		$$('.gilink').each(function(link){ 
			
			// grab the associated product image
			var ids = link.id.split('_');
			product_images[ids[1]] = new Image();
			product_images[ids[1]].src = root_uri + 'img/product/' + ids[1] + '.jpg';
			
			// attach an event to the product image links to 
			// switch the images around 
			Event.observe($('gilink_' + ids[1]), 'click', function() { 
				$('product_image').src		= root_uri + 'img/product/' + ids[1] + '.jpg';
				$('zoomlink-anchor').href	= 'javascript:Popup.open({url:"' + root_uri + 'products/zoom/' + ids[1] + '",width:"700",height:"450"});';
				$('producti-anchor').href	= 'javascript:Popup.open({url:"' + root_uri + 'products/zoom/' + ids[1] + '",width:"700",height:"450"});';
			});
		});
	}
});

function openW(mypage,myname,w,h,features) {
	if(screen.width){
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	}else{winl = 0;wint =0;}
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"no",
      resizable:"no",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width 	= (this.options.width < screen.availWidth) ? this.options.width		: screen.availWidth;
    this.options.height	= (this.options.height < screen.availHeight) ? this.options.height	: screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if(this.options.top != "") {
		openoptions += ",top=" + this.options.top;
	}
	else {
		var top_available = Math.floor((screen.availHeight - this.options.height) / 2);
		openoptions +=",top=" + top_available;
	}
		
    if(this.options.left!="") {
		openoptions +=",left=" + this.options.left;
	}
	else {
		var left_available = Math.floor((screen.availWidth - this.options.width) / 2);
		openoptions +=",left=" + left_available;
	}
	
    window.open(this.options.url, this.options.name,openoptions);
  }
}
