
var postageRates = new Array(
			new PostageRate( "Rarotonga, CI", 0),
			new PostageRate( "Aitutaki, CI", 6),
			new PostageRate( "Southern Group, CI", 7.5),
			new PostageRate( "Nothern Group, CI", 10),
			new PostageRate( "Australia", 9.5),
			new PostageRate( "Fiji", 10),
			new PostageRate( "New Zealand", 7.5),
			new PostageRate( "Samoa", 10),
			new PostageRate( "United Kingdom", 12.5),
			new PostageRate( "USA", 12.5)
	)

var photoSizes = new Array(
			new PhotoSize('6"x4"',7),
			new PhotoSize('8"x10"',15),
			new PhotoSize('10"x13"',20)
	)

var postageCost = 0;
var orderPh_defaultText = "<p>Please order photos by clicking the &ldquo;order prints&rdquo; link beneath the photos you desire. </p>";

var photoOrder =  new Array();
var lastItem = "";

function PostageRate( place_, cost_ ){
	this.country = place_;
	this.cost = cost_;
	
}


function PhotoSize( size_, cost_ ){
	this.size = size_;
	this.cost = cost_;
	
}

/*
*	photoItemobject
*/

function photoItem(id_, quantity_, size_, imageLink_ ){
	this.id = id_;
	this.quantity = quantity_;
	this.photoSize = size_;
	this.imageLink = imageLink_;
	
	this.displayItem = oph_displayItem;

}

// displayItem function 
function oph_displayItem(){
		var HTMLTxt ='<p class="unselected"><input class="oph_id" type="input" size="12" readonly name="item'+this.id+'ID" value="'+ this.id +'">';
		HTMLTxt +='<input class="oph_quantity" type="input" size="8" onChange="pho_changeQuantity('+this.id+',this.value)" name="item'+this.id+'Q" value="'+this.quantity+'">';
        HTMLTxt +='<select class="oph_size" name="item'+this.id+'Size" onChange="pho_changeSize('+this.id+',this.selectedIndex)">';
        for(k=0; k<photoSizes.length; k++){
			tmp="";
			if(photoSizes[k]==this.photoSize)
				tmp="selected";
			HTMLTxt +=' <option value="'+photoSizes[k].cost+'" '+tmp+'>'+photoSizes[k].size+'</option>';
		}
        HTMLTxt +='</select>';
        HTMLTxt +=' <strong class="oph_cost" >'+ formatPrice(this.photoSize.cost*this.quantity) +'</strong>'; 
		HTMLTxt +='<span class="oph_tools" ><a href="'+this.imageLink+'" class="enlarge">Preview</a> <a href="#order" name="'+ this.id +'" class="remove">Remove</a></span><br clear="all"/></p>';
		return HTMLTxt;
}

function oph_changePostage(newValue){
	if(newValue=="Pick up"){
		postageCost=0;
	}else{
		for(var c=0; c<postageRates.length; c++){
			if(postageRates[c].country== newValue)
				postageCost= parseInt(postageRates[c].cost);
		}
	}
	
	displayOrder();
}

function oph_getOrderText(){
		theStyle="display:block; float:left; margin: 6px; width:80px";
		var HTMLTxt ='<span style="'+theStyle+'">Photo ID</span>';
			HTMLTxt +='<span style="'+theStyle+'">Quantity</span>';
			HTMLTxt +='<span style="'+theStyle+'">Size</span>';
			HTMLTxt +='<strong style="'+theStyle+'">Cost</strong><br clear="both">'; 
		var sum=0;
		for(var i=0; i<photoOrder.length; i++){
			HTMLTxt +='<span style="'+theStyle+'">'+ photoOrder[i].id +'</span>';
			HTMLTxt +='<span style="'+theStyle+'">'+ photoOrder[i].quantity +'</span>';
			HTMLTxt +='<span style="'+theStyle+'">'+ photoOrder[i].photoSize.size +'</span>';
			HTMLTxt +='<strong style="'+theStyle+'">'+ formatPrice(photoOrder[i].photoSize.cost*photoOrder[i].quantity) +'</strong><br clear="both">'; 
			sum+=photoOrder[i].photoSize.cost*photoOrder[i].quantity;
			
		}
		sum+=postageCost;
		HTMLTxt +='<span style="'+theStyle+'">&nbsp;</span>';
		HTMLTxt +='<span style="'+theStyle+'">&nbsp;</span>';
		HTMLTxt +='<span style="'+theStyle+'">Postage</span>';
		HTMLTxt +='<span style="'+theStyle+'">'+formatPrice(postageCost)+'</span><br clear="both">'; 
		HTMLTxt +='<span style="'+theStyle+'">&nbsp;</span>';
		HTMLTxt +='<span style="'+theStyle+'">&nbsp;</span>';
		HTMLTxt +='<strong style="'+theStyle+'">Total</strong>';
		HTMLTxt +='<strong style="'+theStyle+'">'+formatPrice(sum)+'</strong><br clear="both">'; 
		return HTMLTxt;
}

function oph_changePostMethod( postMethod){
	if( postMethod=='pick up'){
		$("#shipping").hide();
		oph_changePostage("Pick up");
	}else if( postMethod=='post'){
		$("#shipping").show();
		oph_changePostage( parseInt( $("#country").attr("value") ) );
	}
}

function displayOrder( newOrder ){
	$("#photoOrder").empty();
	var sum=0;
	for(var i=0; i<photoOrder.length; i++){
		$("#photoOrder").append(photoOrder[i].displayItem());
		sum+=photoOrder[i].photoSize.cost*photoOrder[i].quantity;
	}
	sum+=postageCost;
	if(photoOrder.length>0){
	// append the postage costs (if any)
	$("#photoOrder").append('<p><span class="oph_total">Postage:</span><span class="oph_cost">'+ formatPrice(postageCost)+'</span><br class="oph_clear"\></p>');
	$("#photoOrder").append('<p><strong class="oph_total">Total:</strong><strong class="oph_cost">'+ formatPrice(sum)+'</strong><br class="oph_clear"\></p>');
	
	}
	if(photoOrder.length==0)
		$("#photoOrder").append(orderPh_defaultText);
	//add the total cost 
	
	
	// add the onClick function for the reomve links
	$("a.remove").click(function() {
		for(var j=0; j<photoOrder.length; j++){
			if(this.name == photoOrder[j].id)
				photoOrder.splice(j,1);
		}
		displayOrder();
	});
	// add the onClick function for the enlarge links (fancybox)
	$("a.enlarge").fancybox({ 'hideOnContentClick': true, 'overlayOpacity': 0.6 ,  'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true, 'easingChange': "swing" }); 
	//focus on the selected input.
	$("input[name='item" +lastItem+ "Q']").focus();
	$("input[name='item" +lastItem+ "Q']").select() ;
	$("input[name='item" +lastItem+ "Q']").parent().removeClass("unselected") ;
	$("input[name='item" +lastItem+ "Q']").parent().addClass("selected") ;
}



$(document).ready(function(){
	displayOrder();
	$("a.order").click(function() {
		photoOrder[photoOrder.length]=new photoItem(this.name, 1, photoSizes[0], $(this).next().attr("href") );
		$("a.order").attr("href","javascript:void(0)");
		var target = $("div#photoOrder");
		var targetOffset = target.offset().top - 100;
 		$('html,body').animate({scrollTop: targetOffset}, 1000, "swing");					   
 		// focus on the field and select all text
		
		lastItem = this.name;
		$("a#oph_gallerylink").attr("href","javascript:void(0)");
		$("a#oph_gallerylink").unbind("click");
		$("a#oph_gallerylink").click(function() {
			var target = $("a#"+lastItem);
			var targetOffset = target.offset().top - 300;
 			$('html,body').animate({scrollTop: targetOffset}, 1000);					   
 		});
		displayOrder()
	});
	// Add the current 
	var oph_postage='<select name="country" id="country" onChange=" oph_changePostage(this.value) ">';
	for(var o=0; o<postageRates.length; o++){
		oph_postage+='<option value="'+postageRates[o].country+'">'+ postageRates[o].country +'</option>';
	}
	oph_postage +='</select>';
	$("#country").replaceWith(oph_postage);
	
});

function pho_changeQuantity(id_,newValue_){
		for(var j=0; j<photoOrder.length; j++){
			if(id_ == photoOrder[j].id){
				photoOrder[j].quantity = newValue_;
			}
		}

		displayOrder();
}

function pho_changeSize(id_, selected_){
		for(var j=0; j<photoOrder.length; j++){
			if(id_ == photoOrder[j].id){
				photoOrder[j].photoSize = photoSizes[selected_];
			}
		}
		displayOrder();
}




//this function formats prices to $x.xx in text.  Doesn't matter what format the nmber comes in as.
function formatPrice(price){
	 price=price*100;
	 price=Math.round(price);
	 price=price/100; //Roundto Nearest Cent
	 var textPrice= "$"+price;
	 
	 if(textPrice.indexOf('.')==-1){
		textPrice+=".00";
	 } else if(textPrice.indexOf('.')==textPrice.length-2) {
		textPrice+="0";
	 }
	 return textPrice;
}
