//JQUARY--START--
url="class/";
$(document).ready(function() {
	$(".custom_filled").click(function(event){add_custom_filled($(this));});
	$(".filled_remove").click(function(event){remove_custom_filled($(this));});
	$(".phone").keyup(function () {
		if($(this).val().length == $(this).attr('maxlength')){$(this).next().focus();}
	});
	$(".checkout_form select").change(function () {$(this).next().focus();});
	$("#shop_chart_header").click(function(event){toggle_cart()});
	$("body").click(function(event){cart_up();});
	$(window).scroll(function(event){if(!$('#shop_chart').length){return;}
		if($('#shop_chart').css('display')!="fixed"){
			chart_t=$('#shop_chart').css('top');
			chart_t=chart_t.substr(0,chart_t.indexOf('px'));
			scrool_t=$(this).scrollTop();
			if(chart_t>scrool_t){
				$('#shop_chart').css('top',($(this).scrollTop()+2)+"px");
			}
		}
	});
	$('#shop_chart').click(function(event){ event.stopPropagation(); }); 

	$(".input_qty").keyup(function(event){shop_chart_input_keyup(this);});
	$('.checkbox_service').click(function(event){
		current_total=$("#cart_total").val();
		if($(this).attr("checked")){total=(parseFloat(current_total)+parseFloat($(this).attr("fee")));}
		else{total=(parseFloat(current_total)-parseFloat($(this).attr("fee")));}
		$("#cart_total").val(total);
		$("#span_total").html(total);
		
	});
	$("#cart_ship_service").change(function(event){
		total=(parseFloat($("#cart_subtotal").val())+parseFloat($("#cart_ship_service option[value="+$(this).val()+"]").attr('price'))).toFixed(2);
		if($('.checkbox_service').attr("checked")){total=(parseFloat(total)+parseFloat($('.checkbox_service').attr("fee"))).toFixed(2);}
		$("#span_total").html(total);
		$("#cart_total").val(total);
	});
	
	$(".button_item_remove").bind('click',shop_chart_item_remove);
	
	$("#google_checkout").click(function(event){confirm_checkout(event);});
	$("#paypal_checkout").click(function(event){confirm_checkout(event);});
	function recalculate_ship(o){
		total=(parseFloat($("#cart_subtotal").val())+parseFloat($("#cart_ship_service option[value="+$(this).val()+"]").attr('price'))).toFixed(2);
		$("#span_total").html(total);
		$("#cart_total").val(total);
	}
	var c_toggle_cart=0;
	function toggle_cart(){
		if(c_toggle_cart){cart_up();c_toggle_cart=0;}
		else{cart_down();c_toggle_cart=1;}
	}
	function confirm_checkout(event){
		if($("#input_zip_confirm").val()=='1'){}
		else{event.preventDefault();alert('Please Write a Valid USA Zipcode');}
	}
	function cart_up(){
		$('#shop_chart').css('display',"fixed");
		$('#shop_chart_footer').hide('fast');
		$('#shop_chart_content').hide('fast');
		document.getElementById("toggle_arrow").style.backgroundPosition="right 0px";
		$('#shop_chart').css('top', '2px');
	}
	function cart_down(item_position){
		$('#shop_chart').css('display',"block");
		$('#shop_chart_footer').slideDown('fast');
		$('#shop_chart_content').slideDown('fast');
		document.getElementById("toggle_arrow").style.backgroundPosition="right -8px";
		new_position=$(this).scrollTop() - item_position+50;
		if(parseInt(new_position)<=0 || $(this).scrollTop()<=0){new_position=2;}
		$('#shop_chart').css('top', new_position+'px');
	}
	$('.shop_box').mouseover(function(event){
		if(1)$(this).attr({'src':$(this).attr('src').replace('_box','_open')});
		$("#cell_"+$(this).attr('id')).show();
	});
	$('.shop_box').mouseout(function(event){
		if(1)$(this).attr({'src':$(this).attr('src').replace('_open','_box')});
		$("#cell_"+$(this).attr('id')).hide();
	});
	$('.add_item_button').mouseover(function(event){
		a=$('#'+$(this).attr("cycle_target"));
		a.attr("src",$(this).attr("cycle_img"));
		});
	$('.add_item_button').click(function(event){event.stopPropagation();
		item_code=$(this).attr("id");
		$.ajax({
			type: "POST",
			url: url+"ajax.php",
			data: 	"f_in=add_item&item_code="+item_code,
			success: function(html){
				if($('#shop_chart_content').html().toLowerCase()=='<p>&nbsp;&nbsp;shopping cart is empty</p>'){$('#shop_chart_content').html('');}
				a_return=html.split('');
				if(parseInt(a_return[0])==0){$('#shop_chart_content').append(a_return[4]);}
				else { 
					$('#input_'+item_code).val(a_return[1]); 
				}
				if(0 && a_return[5]==0){$('#stockinfo_'+item_code).html('(Out of Stock)')};
				$('#char_item_num').html(a_return[2]);
				$('#shop_chart_subtotal').html(a_return[3]);
				setTimeout(function(){cart_down($('#div_'+item_code).position().top);},100);
				//FOR USER MANUAL CHANGES
				$("#shop_chart_content input").keyup(function(event){shop_chart_input_keyup(this);});
				$(".button_item_remove").unbind('click',shop_chart_item_remove);
				$(".button_item_remove").bind('click',shop_chart_item_remove);
			}
		});
		
		$('#shop_chart_footer').slideDown('Fast');
		$('#shop_chart_content').slideDown('slow');
	})
});
function add_custom_filled(o){
	v="f_in=add_custom_filled&id="+o.attr('id');p="div_custom_box";
	$("#div_custom_box").show();
	$.ajax({type: "POST",url: url+"ajax.php",data:v,error:function(){alert('problem')},success: function(html){
		$("#"+p).html(html);
		$(".filled_remove").click(function(event){remove_custom_filled($(this));});
	}});
}
function remove_custom_filled(o){
	v="f_in=remove_custom_filled&id="+o.attr('id');p="div_custom_box";
	$.ajax({type: "POST",url: url+"ajax.php",data:v,error:function(){alert('problem')},success: function(html){
		$("#"+p).html(html);
		$(".filled_remove").click(function(event){remove_custom_filled($(this));});
	}});
}

function lable_next(){$("#"+$(this).attr('for')).focus();}
function copy_adress(){
	a=$('#copy_map').val().split(' & ');
	k=a[0].split('|');
	v=a[1].split('|');
	for(i=0;i<k.length;i++){
		if($('input[name='+k[i]+']').length){$('input[name='+k[i]+']').val(v[i]);}
		else{
			$('[name='+k[i]+'] option[value='+v[i]+']').attr("selected","selected");

			}
	}
}
function disable_cart(){
	$("#shop_chart_content input").attr('disabled', 'disabled');
}
function enable_cart(){
	$("#shop_chart_content input").attr('disabled', '');
}
//JQUARY--END--
function shop_chart_input_keyup(o){
	disable_cart();
	$.ajax({
		type: "POST",
		url: url+"ajax.php",
		data: 	"f_in=change_input&item_code="+$(o).attr('id').replace('input_','')+"&item_val="+$(o).val(),
		success: function(html){
			a_return=html.split('');
			if($(o).val()>a_return[0]){$('#'+$(o).attr('id')).val(a_return[0]);}
			$(o).val(a_return[0]);
			$('#char_item_num').html(a_return[1]);
			$('#shop_chart_subtotal').html(a_return[2]);
			enable_cart();$(o).focus();
		}
	});
}
function check_out(service_type){
	$.ajax({
		type: "POST",
		url: url+"ajax.php",
		data: 	"f_in=check_out&service_type="+service_type,
		success: function(html){
			alert(html);
		}
	});
}
var shop_chart_item_remove=function(){
	o=$(this);
	$.ajax({
		type: "POST",
		url: url+"ajax.php",
		data: 	"f_in=remove_item&item_code="+o.attr('id'),
		success: function(html){
			a_return=html.split('');
			$('#char_item_num').html(a_return[1]);
			$('#shop_chart_subtotal').html(a_return[2]);
			$('#div_'+o.attr('id')).remove();
		}
	});
}
function register(){
	$.ajax({
		type: "POST",
		url: "submit_data.php",
		data: 	"username=" + document.getElementById("username").value + 
				"&email=" + document.getElementById("email").value,
		success: function(html){
			$("#response").html(html);
		}
	});
	
	}

//add_chocolate
var p_num='';
function add_chocolate(size,tot,id){
	if(p_num==''){p_num=tot;}
	if(size-1>=p_num){p_num++;xml("data","f_in=add_filled&id="+id,"box_area");}
	else{alert('You can not add more');}
}
//remove_chocolate
function remove_chocolate(size,tot,id){
	if(p_num==''){p_num=tot;}
	if(p_num>0){p_num--;xml("data","f_in=remove_filled&id="+id,"box_area");}
	else{alert('You can not remove more');}
}
//f_href
function f_href(t){window.location.href=t;}
//OPEN CLOSE
function o(id){document.getElementById(id).style.display='block';}
function c(id){document.getElementById(id).style.display='none';}
function o_bar(id){
	document.getElementById('cell_info_'+id).style.display='block';
	document.getElementById('cell_info_'+id).innerHTML=document.getElementById('cell_'+id).innerHTML;
}
function c_bar(id){
	document.getElementById('cell_info_'+id).innerHTML='';
	document.getElementById('cell_info_'+id).style.display='none';
}
// ALT MENU HANDLER
var c_alt_link_array=new Array();
var timer=new Array();
function o_alt(id){
	if(c){c_alt_link_array[id]='on';}
	if(timer[id]==undefined){document.getElementById(id).style.display='block';}
	else{clearTimeout(timer[id]);}
}
function c_alt(id,c){
	timer[id]=setTimeout(function(){document.getElementById(id).style.display='none';delete c_alt_link_array[id];delete timer[id];},c);
}
function on_img(id,img){document.getElementById(id).src=img;}
function off_img(id,img){document.getElementById(id).src=img;}
//IMAGE ONLOADER
function onload_images() {
	document.img_cell=new Array();
	a=new Array('menu_contact_us_over.gif','menu_designer_bars_over.gif','menu_filled_chocolates_over.gif','menu_home_page_over.jpg','menu_location_over.jpg','menu_michael_mischer_over.jpg','menu_our_chocolates_over.jpg','menu_press_over.jpg','menu_shop_online_over.jpg','menu_the_collection_over.jpg');
	for(i=0; i<a.length; i++){document.img_cell[i]=new Image; document.img_cell[i].src='images/'+a[i];}
}
//COLLECTION
document.onmousemove=f_mouseMove;
var xmouse,ymouse;
function f_mouseMove(e){
	e = (window.event) ? window.event : e;
	xmouse=e.clientX;
	ymouse=e.clientY;
	if(c_drag_obj)
	{
		document.getElementById('ad').style.left=xmouse+document.body.scrollLeft+40+'px';
		document.getElementById('ad').style.top=ymouse+document.body.scrollTop-210+'px';
	}
	
}
//OPEN DETAILER
function o_det(id,img){
	document.getElementById(id).innerHTML='<img src='+img+'>'+document.getElementById(id).innerHTML;
	document.getElementById(id).style.display='block';
}
function c_det(id){
	document.getElementById(id).innerHTML='<img src='+img+'>'+document.getElementById(id).innerHTML;
	document.getElementById(id).style.display='none';
}
//ACTIVE CELL OPENER
var c_drag_obj;
var v_left=1;
function activate_cell(topic,body,img,e){
	
	c_drag_obj=true;
	e = (window.event) ? window.event : e;
	xmouse=e.clientX;
	ymouse=e.clientY;
	if(c_drag_obj)
	{
		document.getElementById('ad').style.left=xmouse+document.body.scrollLeft+40+'px';
		document.getElementById('ad').style.top=ymouse+document.body.scrollTop-210+'px';
	}
	document.getElementById('ad').style.display='block';
	document.getElementById('ad_topic').innerHTML='<b>'+topic+'</b>';
	document.getElementById('ad_img').innerHTML='<img src="'+img+'" width=120 style="border:#CCC 1px solid;">';
	document.getElementById('ad_body').innerHTML=body;
}
function deactivate_cell(){
	c_drag_obj=false;
	document.getElementById('ad').style.display='none';
	document.getElementById('ad_topic').innerHTML='';
	document.getElementById('ad_img').innerHTML='';
	document.getElementById('ad_body').innerHTML='';
}

