// JavaScript Document

function gp(){
	alert("gp lib loaded");
}

function calcHeight(){
	frameheight = $(window.frames[0].document.getElementById('wrapper')).height();
	$("#blogframe").css("height",String(frameheight+100)+"px");

	
}
var otfmod =0;
function computeprice(){
		


	myprice=0;

taglist = $("input");

	$.each(taglist, function(index, object){
		if($(object).attr("checked") && !$(object).attr("disabled")){
			// actions for promo package selection - should be better on a different function
			if($(object).attr("name").match("package")){
				$(".package_selected").removeClass("package_selected");
				$(object).parent("h1").addClass("package_selected");
				if($(object).attr("value").indexOf("--")<0)$(".package-items").hide();
				$("."+$(object).attr("value")).show();
				$("input[name='package-items']").attr("value",$("."+$(object).attr("value")).html());
			}

			value = $(object).attr("price");		
			
			
			
				if(value){
					myprice+=parseInt(value);
				}	
				priceframes = $(object).attr("priceframes");
				if(priceframes){
					//$("input[name='frames']").attr("price",priceframes);
					$(".priceframes").html(formatprice(String(priceframes)));
				}			
		}
						   
	});	
	taglist = $("option");
	$.each(taglist, function(index, object){
		if($(object).attr("selected") && !$(object).attr("disabled") ){
			value = $(object).attr("price");		
			otfmodnew= $(object).attr("otfmod");
			if(otfmodnew){
				otfmod += parseFloat(otfmodnew);
				
			}
				if(value){
					if($(object).parent().attr("name").match("event_region")){
						//alert(value+":"+computeprice.otfmod);
					}
					myprice+=parseInt(value);
				}	
				priceframes = $(object).attr("priceframes");
				if(priceframes){
					$("input[name='frames']").attr("price",priceframes);
					$(".priceframes").html(formatprice(String(priceframes)));
				}			
		}
						   
	});	
	formatedprice = formatprice(String(myprice));
	$("input[name='totalprice']").attr("value",formatedprice);
	$(".totalprice").css("color","red").html(formatedprice);
	setTimeout("$('.totalprice').html(formatedprice).css('color','black')",500);	
}


function formatprice(pricestr){	
	strlength = pricestr.length;	
	if(strlength>3){			
		return formatprice(pricestr.substring(0,strlength-3))+","+pricestr.substring(pricestr.length-3);			
	}else{		
		return "P"+pricestr;
	}
	
}

function disable(target,checked){	
	if(checked !=null) $(target+" input").attr("disabled","disabled").css("background-color","#CCCCCC").attr("checked",checked);
	else  $(target+" input").attr("disabled","disabled").css("background-color","#CCCCCC");
	$(target).css("color","#CCCCCC");
	$(target+" select").attr("disabled","disabled").css("background-color","#CCCCCC");			

}
		
function enable(target,checked){
	if(checked!=null) $(target+" input").attr("disabled",null).attr("checked",checked);
	else $(target+" input").attr("disabled",null);
	$(target).css("color","#000000");
	$(target+" select").attr("disabled",null).css("background-color","#000000");			
}
	
function initprice(){	

	updateform = function(object){		

		if($(object).attr("value")!=null) myObject = object; else myObject = this;				
	   target = "."+$(myObject).attr("name");
	   if(myObject.length!=null && myObject.length>0){
		   $.each(myObject,function(){if($(this).attr("checked")){myObject=this;}});
	   }
	 
	   toggle = ($(myObject).attr("value").indexOf("--")>-1);	
	   if($(myObject).attr("disabled")){toggle=true;}
	 
	   
		try{
			
		whenvalue = $(target).attr("whenvalue");
		hasvalue = whenvalue.indexOf($(myObject).attr("value"));
		if(whenvalue.indexOf($(myObject).attr("value"))>-1)toggle = true;
		
		
		}catch(e){}
		 if($(target).hasClass("reverse")) toggle = !toggle;		
		 if(toggle){			
		// alert("disable "+target);
				$(target+" input").attr("disabled","disabled").css("background-color","#CCCCCC");
				$(target+" tr").css("color","#CCCCCC");
				$(target+" select").attr("disabled","disabled").css("background-color","#CCCCCC");
				$(target+" option").attr("disabled","disabled").css("background-color","#CCCCCC");	
				return false;
			}else{
				 //alert("enable "+target);
				$(target+" input").attr("disabled",null);
				$(target+" tr").css("color","#000000");
				$(target+" select").attr("disabled",null).css("background-color","#ffffff");			
				$(target+" option").attr("disabled",null).css("background-color","#ffffff");	
				return true;
			}
	
	   
	}

	


	//reset form
	document.forms[0].reset();
	$("input").attr("disabled",null);
	$("select").attr("disabled",null);
	
	//set event listeners
	$("input[name='package']").click(function(){				
											  
			if(updateform(this)){				
				try{
				updateform($("select[name='photographers']"));
				updateform($("select[name='videographers']"));
				updateform($("input[name='album_type']"));
				}catch(e){}
			}
			
		});	

	$("select[name='videographers']").click(function(){
												updateform(this);
												switch($(this).attr("value")){
													case 'One': disable('.OneDisable',false);
												}											
											});	
	$("select[name='photographers']").click(function(){
													  updateform(this);																				
													  updateform($("input[name='album_type']"));
													  });
	$("input[name='album_type']").click(function(){updateform(this);});

	
	$("input[type='radio']").click(function(){computeprice()});			
	$("input[type='checkbox']").click(function(){computeprice()});		
	$("select").change(function(){computeprice()});		
	initbackdrop();	
	//hide package items
	$(".package-items").hide();
	//trigger package
	$("input[name='package'][value='-----']").click();
	computeprice();
	//$(".bookingform").hide();	
	
}

function initbackdrop(){
	
$(".backdroplist img").toggle(
			function(){
				$(this).css("width","150px").css("height","150px");
			},
			function(){
				$(this).css("width","50px").css("height","50px");
			}
			);

}

function togglebooking(target){
	if ($(target).is(":hidden")) {		  
        $(target).show();
      } else {
       $(target).hide();
      }
}


function initcontract(){
	avalue = "";
	terms = $(".terms");
	$.each(terms, function(i,object){avalue += $(object).html();});
	$("#contract-terms").attr("value",avalue);
}