$(document).ready(function(){
	/*
	var mouseY = 0;
	$(document).mousemove(function(e){
	   mouseY = e.pageY;
	});

	// cache the selector
	
	var xp = 0, yp = 0;
	var loop = setInterval(function(){
		// change 12 to alter damping higher is slower
		if(mouseY>400){
			yp += (mouseY - yp) / 12;
			$("#cart_back").css('top',yp+'px');
			$("#cart_right_div").css('margin-top',(yp - 200)+'px');
		}
	}, 30);
*/

	$('#sushi').hide();
	$( ".button" ).button();
	$("#user_check_logged_popup" ).dialog(
		{autoOpen: false, modal: true, draggable: false, resizable: false, width: 600, buttons: { 
			"Εντάξει": function(){
				$(this).dialog("close");
			}
		}}
	);
	$("#user_check_login_popup" ).dialog(
		{autoOpen: false, modal: true, draggable: false, resizable: false, width: 600, buttons: { 
			"Εντάξει": function(){
				$(this).dialog("close");
			}
		}}
	);
	$("#please_wait_popup_div" ).dialog(
		{autoOpen: false, modal: true, draggable: false, resizable: false, width: 600, closeOnEscape: false, open: function(event, ui) { $(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').hide(); }}
	);		
	
	setDims();

	$('a.my_edit_order_link').live('click', function(){
		if($('input#input_is_user_logged').val() == 1){
			return true;
		}else{
			$("#user_check_logged_popup" ).dialog( "open" );
			$('.ui-dialog .ui-state-focus').removeClass('ui-state-focus');
			return false;
		};
	});
	
	user_logout_button_click_handler = function(){
		//alert($(location).attr('href'));
		cur_path = $(location).attr('href').toString();
		first_char = cur_path.indexOf('e-hot.gr/',0);
		new_path = cur_path.substr(0,first_char+9);
		
		$.ajax({
			type: "POST",
			url: APP_DIR + "/user/logout",
			dataType: 'html',
			success: function(data){
				if((cur_path.search('/cart/confirmOrder') != -1) || (cur_path.search('/cart/completeOrder') != -1) || (cur_path.search('/user/create') != -1)){
					window.location.href = new_path;
				}else{
					set_user_login_div();
				};
			}
		});		
		return false;
	};

	function set_user_login_div(){
		$.ajax({
			type: "POST",
			url: APP_DIR + "/user/login/2",
			dataType: 'html',
			success: function(data){
				$('#user_login').html(data);
				setDims();
			}
		});	
	};
	
	$(window).resize(function() {
		setDims();
	});

	function setDims(){
		
		left_bottom = $('#menu').position().top + $('#menu').height() + 50;
		right_bottom = $('#cart_right_div').position().top + $('#cart_right_div').height();
		main_height = 75 + $('#main_content').height();
		if(right_bottom > left_bottom){
			dif_height = right_bottom - main_height;
		}else{
			dif_height = left_bottom - main_height;
		}
		//alert(dif_height);
		if(dif_height > 0){
			new_html = '<div style="display: block; height: '+dif_height+'px;"></div>';
			//$('#main_content').append(new_html);
		}
		
		/*
		if((75 + $('#container').height()) > $('#main_content').height()){
			//$('#main_content').css('padding-bottom',(75 + $('#container').height())+'px');
			$('#main_content').append('<div style="display: block; height: '+(75 + $('#container').height() - $('#main_content').height())+'px;"></div>');
		}
		*/
		if($(window).width()>960){
			
			new_width = ($(window).width() - 960)/2;
			
			$('#submenu_back').css('width',new_width + 'px');
			$('#sushi').css('width',(new_width + 200)+ 'px');
			$('#submenu_back').css('height',$('#menu').height() + 20 + 'px');
			$('#sushi').css('height',$('#menu').height() + 20 + 'px');
			$('#submenu_back').css('top',$('#menu').position().top + 'px');
			$('#submenu_back').css('display','block');
			
			//u_width = $(window).width() - $('#header').position().left - 960;
		
			$('#user_back').css('width',new_width + 'px');
			$('#user_back').css('height',($('#user_login').height() - 33) + 'px');
			$('#user_back').css('top',($('#user_login').position().top -50 + 18) + 'px');
			$('#user_back').css('display','block');
			
			$('#cart_back').css('width',new_width + 'px');
			$('#cart_back').css('height',($('#cart_right_div').height() - 48) + 'px');
			$('#cart_back').css('top',$('#cart_right_div').position().top + 'px');
			$('#cart_back').css('display','block');
		}else{
			$('#submenu_back').css('display','none');
			$('#sushi').css('height',$('#menu').height() + 20 + 'px');
			$('#user_back').css('display','none');
			$('#cart_back').css('display','none');
		}
		$('#sushi').show();
		//alert($('#cart_right_div').height() + ' - ' + $('#cart_back').height());
		//alert($('#cart_right_div').position().top + ' - ' + $('#cart_back').position().top);
	};
			
    $('#addProduct').submit(function(e){
		e.preventDefault();
		
		$("#add_product_popup").dialog("close");
		$("#please_wait_popup_div" ).dialog( "open" );
		var post_list = $('form#addProduct').serialize();
		/*
		var my_radio_val = $('#my_radio_buttons input:radio:checked').val();
		if (my_radio_val) {
			post_list = post_list + '&product[features][' + my_radio_val + ']=' + my_radio_val;
		}
		
		var new_list = $('div#ingedients_popup input').serializeArray();
		//var my_list = '';
		jQuery.each(new_list, function(index,value) {
			post_list = post_list + '&' + value.name + '=' + value.value;
		});
		*/
		$.ajax({
			type: "POST",
			url: APP_DIR + "/catalog/addToCart",
			data: post_list,
			dataType: 'json',
			success: function(msg){
			   if(msg['response']=='ok'){
					showCartItems();
			   } 
			}
		});
   });
   
   	$('a#complete_order').live('click', function(){
		if($('input#input_is_user_logged').val() == 1){
			return true;
		}else{
			$("#user_check_logged_popup" ).dialog( "open" );
			$('.ui-dialog .ui-state-focus').removeClass('ui-state-focus');
			return false;
		};
	});
   
   user_login_form_form_submit_handler = function(event){
   		event.preventDefault();
		$("#please_wait_popup_div" ).dialog( "open" );
		$.ajax({
			type: "POST",
			url: APP_DIR + "/user/login/2",
			data: $('form#user_login_form_form').serialize(),
			dataType: 'html',
			success: function(data){
				$("#please_wait_popup_div" ).dialog( "close" );
				if(data.length<10){
					$("#user_check_login_popup" ).dialog( "open" );
				}else{
					$('#user_login').html(data);
					setDims();
				}
			}
		});
   }
   //$('form#user_login_form_form').submit(user_login_form_form_submit_handler);
   

   showCartItems = function(){
       $.ajax({
            type: "GET",
            url: APP_DIR + "/cart/cartPreview/1",
            dataType: 'html',
            success: function(msg){
               // alert(msg);
               $("#cartPreview").html(msg);
			   setDims();
			   $("#please_wait_popup_div" ).dialog( "close" );
            }
        });
   }

   $("#appendAddress").click(function(){
      // var a = '<div class="addr"><input type="hidden" value="1" name="user[addr][]" /><div><span style="margin-right:2px;">Διεύθυνση</span><input type="text" name="user[address][]" size="49" /></div><div><span style="margin-right:2px;">Τηλέφωνο</span><input type="text" name="user[telephone][]" size="49" /></div><a href="#" onclick="$(this).parent().remove();" class="button_remove">αφαίρεση</a></div>';
	   var a = '<div class="addr"><input type="hidden" value="1" name="user[addr][]" /><table cellspacing="0" cellpadding="0" style="width: 380px; font-size: 8pt; border: 1px solid #9e7d12;"><tr><td rowspan="4" style="padding: 2px; text-align: center; font-size: 14pt; width: 20px; background: #9e7d12; color: white;">' + ($("#cont").children().length + 1) + '</td><td style="padding: 2px 4px; width: 75px;">Διεύθυνση:</td><td style="padding: 2px;"><input type="text" name="user[address][]" value="" style="width: 100%;" /></td><td rowspan="4" style="text-align: right;"><a href="#" onclick="$(this).closest(\'div.addr\').remove();return false;" class="button_remove">αφαίρεση</a></td></tr><tr><td style="padding: 2px;">Τηλέφωνο:</td><td style="padding: 2px;"><input type="text" name="user[phone][]" value="" style="width: 100%;" /></td></tr><tr><td style="padding: 2px;">Όροφος:</td><td style="padding: 2px;"><input type="text" name="user[floor][]" value="" style="width: 100%;" /></td></tr><tr><td style="padding: 2px;">Όνομα (στο κουδούνι):</td><td style="padding: 2px;"><input type="text" name="user[addr_name][]" value="" id="user_addr_name_1" style="width: 100%;" /></td></tr></table></div>';
	  
       $("#cont").append(a);
	   $( "a.button_remove" ).button({
			icons: {primary: "ui-icon-circle-minus"},
			text: false
		});
		return false;
    });

    sendToCart = function(id){
        $("#please_wait_popup_div" ).dialog( "open" );
        $.ajax({
            type: "GET",
            url: APP_DIR + "/catalog/addToCartFromHistory/"+id,
            dataType: 'json',
            success: function(msg){
               if(msg['response']=='ok'){
                    showCartItems();
               }
            }
        });
    }

});



