var startX=0;
var startY=0;
var started=0;
var currentCarItem=0;
var resizeMode='portrait';



function update_price(e,price)
{
	var input_box=Event.element(e);
	
	var qty=Number(input_box.value);
	if (qty<0||qty>9999) {
		qty=1;
	}
	var total=Math.round((qty * price)*100)/100;

	$(input_box.id + "_total").innerHTML='$' + (total.toFixed(2));

}

//CART
	Event.observe(document, 'keypress', function(event){ 
		if(event.keyCode == Event.KEY_LEFT) {
			previousImage()
		}
		if(event.keyCode == Event.KEY_RIGHT) {
			nextImage()
		}
	});
	
function cart_process()
{
	var elementX=Number($('crop_slicer').style.left.replace('px',''));
	var elementY=Number($('crop_slicer').style.top.replace('px',''));
	/*
		<input type="hidden" name="cart[print_size_id]" id="cart_print_size_id" />
		<input type="hidden" name="cart[qty]" id="cart_qty" />
		<input type="hidden" name="cart[crop][mode]" id="cart_crop_mode" />
		<input type="hidden" name="cart[crop][x]" id="cart_crop_x" />
		<input type="hidden" name="cart[crop][y]" id="cart_crop_y" />
		*/
	var options=[];
	$('photo_details').getElements().each(function(elm){
		if (elm.checked) {
			options[elm.value]=elm.value;
		}
	});
	$('cart_options').value=options.toJSON();

	$('cart_crop_mode').value=$('option_crop_mode0').checked?0:1;
	$('cart_crop_x').value=elementX;
	$('cart_crop_y').value=elementY;
	$('cart_crop_width').value=$('crop_slicer').getWidth();
	$('cart_crop_height').value=$('crop_slicer').getHeight();
	$('photo_form').submit();
}

//non-print add
function cart_nadd(product_id,total) {
	$('cart_print_size_id').value=product_id;
	$('cart_qty').value=total;
	$('photo_form').submit();
}

var print_dimensions;
function cart_add(product_id,total,size_x,size_y) {
	$('header').scrollTo();
	var stats=$('params_'+product_id).innerHTML.evalJSON(true);
	if (typeof(stats)!='object') {
		alert('Invalid options specified.');
		return;
	}
	$('photo-details-outside').show();
	$$('#finishing_options input').each(function(e){
		$(e).checked=false;
	});
	//Jump to landscape mode in special cases.
	if ($('crop_window').getWidth()<=$('crop_window').getHeight()) {
		$('option_crop_mode0').checked=true;
	}else{
		$('option_crop_mode1').checked=true;
	}
	//hide previous options
	$$('#finishing_options dd').each(function(e){
		$(e).hide();
	});
	//show new ones
	var first_check=[];
	stats.sizes.each(function(e){
		if ($('option_group_'+e.group+'0')) {
			if (!first_check[e.group]) {
				$('option_group_'+e.group+'0').checked=true;
				first_check[e.group]=true;
			}
			$('option_group_'+e.group+'0').up().show();
		}
		if ($('option_group_'+e.group+e.finishing_option_id)) {
			if (!first_check[e.group]) {
				$('option_group_'+e.group+e.finishing_option_id).checked=true;
				first_check[e.group]=true;
			}		
			$('option_group_'+e.group+e.finishing_option_id).up().show();
			//label update
			$('option_group_'+e.group+e.finishing_option_id).next().innerHTML=e.title + ' (' + e.formatted_cost + ')';
		}
	});
	print_dimensions=stats.dimensions;

	$('cart_print_size_id').value=product_id;
	$('cart_qty').value=total;
	$('cart_edit').value='-1';

	maxSize=print_dimensions.landscape.width;
	maxSizeY=print_dimensions.landscape.height;
	currentCartItem=product_id;
	
	if (navigator.appName=='Microsoft Internet Explorer') {
		$('html').setStyle({filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='/html/images/transparent.png');"});
	}else{
		$('html').setStyle({backgroundImage:"url(/html/images/transparent.png)"});
	}
	

	$('edit_btn').innerHTML='Add Photo to Cart';
	changeSize();
	updateBackground();
}

function cart_edit(product_id,total,size_x,size_y,cart_index) {
	$('header').scrollTo();
	var stats=$('cparams_'+cart_index).innerHTML.evalJSON(true);
	if (typeof(stats)!='object') {
		alert('Invalid options specified.');
		return;
	}
	$$('#finishing_options input').each(function(e){
		$(e).checked=false;
	});
	if (stats.crop.mode==0) {
		$('option_crop_mode0').checked=true;
	}else{
		$('option_crop_mode1').checked=true;
	}
	//hide previous options
	$$('#finishing_options dd').each(function(e){
		$(e).hide();
	});
	//show new ones
	var first_check=[];
	stats.sizes.each(function(e){
		if ($('option_group_'+e.group+'0')) {
			if (!first_check[e.group]) {
				$('option_group_'+e.group+'0').checked=true;
				first_check[e.group]=true;
			}
			$('option_group_'+e.group+'0').up().show();
		}
		if ($('option_group_'+e.group+e.finishing_option_id)) {
			$('option_group_'+e.group+e.finishing_option_id).up().show();
			stats.options.each(function(se){
				if (e.finishing_option_id==se.finishing_option_id) {
					$('option_group_'+e.group+e.finishing_option_id).checked=true;
				}
			});
		}
	});
	
	
	
	
	print_dimensions=stats.dimensions;

	$('cart_print_size_id').value=product_id;
	$('cart_qty').value=total;
	$('cart_edit').value=cart_index;
	$('edit_btn').innerHTML='Update Photo';

	maxSize=print_dimensions.landscape.width;
	maxSizeY=print_dimensions.landscape.height;
	currentCartItem=product_id;
	$('photo-details-outside').show();
	if (navigator.appName=='Microsoft Internet Explorer') {
		$('html').setStyle({filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='/html/images/transparent.png');"});
	}else{
		$('html').setStyle({backgroundImage:"url(/html/images/transparent.png)"});
	}

	
	changeSize();
	
	$('crop_slicer').style.top=stats.crop.y+"px";
	$('crop_slicer').style.left=stats.crop.x+"px";
	
	updateBackground();
}


function cart_cancel() {
	$('html').setStyle({backgroundImage:"",filter:""});
	$('photo-details-outside').hide();
	
}

//END CART
var paused=false;
var timeoutid=1;
function nextImage(pause_slideshow)
{
	if (pause_slideshow==true) {
		paused=true;
		$('slideshow_play_pause').src=$('slideshow_play_pause').src.replace('pause','play');
	}
	slideshow_current=slideshow_current+1;

	if (slideshow_current*2>slideshow_photos.length) {
		slideshow_current=1;
	}
	if (pause_slideshow) {
		clearTimeout(timeoutid);timeoutid=0;
	}
	showImage();
	
}

function previousImage(pause_slideshow)
{
	if (pause_slideshow==true) {
		paused=true;
		$('slideshow_play_pause').src=$('slideshow_play_pause').src.replace('pause','play');
	}
	slideshow_current=slideshow_current-1;
	if (slideshow_current<=0) {
		slideshow_current=((slideshow_photos.length-1)/2);
	}
	if (pause_slideshow) {
		clearTimeout(timeoutid);timeoutid=0;
	}
	
	showImage();
	
	
}

function showImage()
{
	$('buy_button').href=slideshow_click + slideshow_photos[slideshow_current*2-1];
	$('slideshow_image').src=slideshow_path + slideshow_photos[slideshow_current*2-2];
	//Preloader
	slideshow_next=slideshow_current+1;
	if (slideshow_next*2>slideshow_photos.length) {
		slideshow_next=1;
	}
	$('slideshow_image_preload').src=slideshow_path + slideshow_photos[slideshow_next*2-2];
	$('slideshow_image').onload=function(){
		if (typeof(timeoutid=='number') && timeoutid>0){
			timeoutid=setTimeout(function(){nextImage(false);},3000);
		}
		//$('slideshow_protect').style.height=($('slideshow_image').height+20)+'px';
	};
}

function pauseImage()
{
	if (paused==true) {
		paused=false;
		$('slideshow_play_pause').src=$('slideshow_play_pause').src.replace('play','pause');
		if (typeof(timeoutid=='number')) {
			clearTimeout(timeoutid);
		}
		timeoutid=setTimeout(function(){nextImage();},3000);
	}else{
		paused=true;
		$('slideshow_play_pause').src=$('slideshow_play_pause').src.replace('pause','play');
		if (typeof(timeoutid=='number')) {
			clearTimeout(timeoutid);
		}
	}
}

function changeSize()
{
	if (print_dimensions) {
		if (!$('option_crop_mode0').checked) {
			$('crop_slicer').style.width=print_dimensions.portrait.width+'px';
			$('crop_slicer').style.height=print_dimensions.portrait.height + "px";
	
			
		}else{
			$('crop_slicer').style.width=print_dimensions.landscape.width+'px';
			$('crop_slicer').style.height=print_dimensions.landscape.height + "px";
	
			
			
			
		}
		if ($('crop_slicer').style.width>$('crop_slicer').style.height) {
			
				resizeMode='portrait';
			}else{
				resizeMode='landscape';
			}
		updateBackground();
		$('crop_slicer').style.top="0px";
		$('crop_slicer').style.left="0px";
	}
}

function startSlider(e)
{
	if (started==0) {
		var elementX=$('crop_slicer').style.left.replace('px','');
	    var elementY=$('crop_slicer').style.top.replace('px','');
		startX = Event.pointerX(e) - Number(elementX);
	    startY = Event.pointerY(e) - Number(elementY);
		started=1;
	}

}
//Update the 2 padding spaces to 
function updateBackground()
{
	var elementX=Number($('crop_slicer').style.left.replace('px',''));
	var elementY=Number($('crop_slicer').style.top.replace('px',''));
	
	 var maxX=$('crop_window').getWidth()-$('crop_slicer').getWidth();
	 var maxY=$('crop_window').getHeight()-$('crop_slicer').getHeight();
	 
	 var newLeft=elementX+$('crop_slicer').getWidth();
	 var newTop=elementY+$('crop_slicer').getHeight();
	
	if ($('crop_window').getWidth()>=$('crop_slicer').getWidth()) {
	   	$('crop_slicer_pad1').style.width=elementX + 'px';
	   	$('crop_slicer_pad2').style.left=(newLeft) + 'px';
	   	$('crop_slicer_pad2').style.width=(maxX-elementX) + 'px';
	   	
	   	$('crop_slicer_pad2').style.top='0px';//Reset if switching modes
	   	$('crop_slicer_pad1').style.height='100%';
	   	$('crop_slicer_pad2').style.height='100%';
	   	
	  // 	$('crop_slicer_pad2').style.backgroundPosition='-'+(newLeft) + 'px 0px';	//So the background doesn't look like it's moving
   }else{
   		$('crop_slicer_pad1').style.height=elementY + 'px';

   		
   		
   		$('crop_slicer_pad2').style.top=(newTop) + 'px';
   		
   		$('crop_slicer_pad2').style.height=(maxY-elementY) + 'px';
   		$('crop_slicer_pad1').style.width='100%';	   		
	   	$('crop_slicer_pad2').style.width='100%';
	   	$('crop_slicer_pad2').style.left='0px';//Reset if switching modes
	   	
	   	//$('crop_slicer_pad2').style.backgroundPosition='0px -'+(newTop) + 'px';	//So the background doesn't look like it's moving
   }
}
function moveSlider(e)
{
	//Only update if we are holding a mouse, don't use up cpu or memory!
	if (started==1) {
	    var x = Event.pointerX(e);
	    var y = Event.pointerY(e);

	    // move the target object
	    var newX=Number(x - startX);
	    var newY=Number(y - startY);
	    var maxX=$('crop_window').getWidth()-$('crop_slicer').getWidth();
	    var maxY=$('crop_window').getHeight()-$('crop_slicer').getHeight();
	    if (newX<0) {
	    	newX=0;
	    }
	    if (newY<0) {
	    	newY=0;
	    }
	    if (newX>maxX) {
	    	newX=maxX;
	    }
	    if (newY>maxY) {
	    	newY=maxY;
	    }
	    $('crop_slicer').style.left=(newX) + "px";
	    if (newY<maxY) {
	    	$('crop_slicer').style.top=(newY) + "px";
	    }
	   // $('mode_select').innerHTML=(newX) + "px AND " +  (newY) + "px / " + x + "px AND " +  y + "px"; 
	   
	   updateBackground();
	    
	    Event.stop(e);
	}
}
//If we just want to click
function simpleSliderUpdate(e)
{
	started=1;
	moveSlider(e);
	started=0;
}

function stopSlider(e)
{
	started=0;
}

Event.observe(window,'load',function(){

	if ($('crop_slicer')) {
		$('option_crop_mode0').observe('change',changeSize);
		$('option_crop_mode0').observe('click',changeSize);
		
		$('option_crop_mode1').observe('change',changeSize);
		$('option_crop_mode1').observe('click',changeSize);
		
		$('crop_slicer').onselectstart = function() {
	        return false;
	    };
		
		
	
		
		
		$('crop_slicer').observe('mousedown',startSlider);
		$('crop_window').observe('click',simpleSliderUpdate);
		$('body').observe('mousemove',moveSlider);
		$('body').observe('mouseup',stopSlider);
		
		changeSize();
	}
});