﻿
function startLoading () {
    centerDiv("#loading");
    $("#loading").show();
};

function endLoading () {
    $('.ui-widget-overlay').remove();
    $("#loading").hide();
};

$(function() {
	$.ajax({ url: "http://www.novaepoha.com/actions.php?action=getBasket",
		type: "POST",
		cache: false,
		dataType: "text",
		success: function(responce,textStatus) {  
			$(".basket_info").html(responce);
		}
		});	
	var page = $("body").attr("id");
	if (page == 'page_basket') {
		$(".basket_page").show();
		$(".def_page").hide();
	}
	else if (page == 'page_seminar') {
		$(".basket_page").hide();
		$(".def_page").show();
	}	
	else if (page == 'page_products') {
		$(".basket_page").hide();
		$(".def_page").show();	
	}
	else if (page == 'page_home') {
		$(".basket_page").show();
		$(".def_page").show();	
	}	
	else {
		$(".basket_page").hide();
		$(".def_page").hide();		
	}
});

function changeBasket (action) {
	if (action == 'step_1') {
		$(".basket_step_1").show();
		$(".basket_step_2").hide();	
		$(".basket_step_3").hide();
	}
	if (action == 'step_2') {
		$(".basket_step_1").hide();
		$(".basket_step_2").show();
		$(".basket_step_3").hide();
	}
	if (action == 'step_3') {
		$('.form_step2').submit();
	}	
};

function centerDiv(divName, pos){  
    //request data for centering  
    var windowWidth = document.body.clientWidth;  
    var windowHeight = document.body.clientHeight;  
    var popupHeight = $(divName).height();  
    var popupWidth = $(divName).width();  
    //centering  
    var top = windowHeight/2-popupHeight/2;
    if (pos && $.browser.msie) {
        top = pos;
    }
    $(divName).css({  
        "z-index": '2000',  
        "top": top,  
        "left": windowWidth/2-popupWidth/2
    });
};

function notifyAvailable (id) {
	if ($("#name_family").val() != "" && $("#email").val() != "" && $("#phone").val() != "") {
	$.ajax({ url: "http://www.novaepoha.com/actions.php?action=notifyAvailable",
		data: ({id: id, name: $("#name_family").val(), email: $("#email").val(), phone: $("#phone").val()}),
		type: "POST",
		cache: false,
		dataType: "text",
		beforeSend: function () {
			startLoading ();
		},
		success: function(responce,textStatus) {  
			$("#available_form").hide();
			endLoading ();                
		},
    error:
    	function(xhr,error){
    		alert ("HTTP status:" + xhr.status + " \n  "+ "Response Text: "+xhr.responseText );
			}
		});	
	}
	else {
		alert ('Полето е задължително.');
	}
};

function addToCard (id) {
	$.ajax({ url: "http://www.novaepoha.com/actions.php?action=addToCard",
		data: ({id: id}),
		type: "POST",
		cache: false,
		dataType: "text",
		beforeSend: function () {
			startLoading ();
		},
		success: function(responce,textStatus) {  
			$(".basket_info").html(responce);
			endLoading ();                
		},
    error:
    	function(xhr,error){
    		alert ("HTTP status:" + xhr.status + " \n  "+ "Response Text: "+xhr.responseText );
			}
		});	
};


function search (action, page, ord) {
	if (action == 'step_1') {
		$(".search_step_1").show();
		$(".search_step_2").hide();
		$(".search_step_3").hide();
	}
	else if (action == 'step_2') {
		$("#start").attr('value', page);
		var order_by = $("#ord_by option:selected").val();
		if (ord != '') {
			$("#order_by").attr('value', order_by);
		}			
		$('.form_step2').submit();
	}	
};

function changeSeminarInfo (action) {
	if (action == 'step_1') {
		$(".step_1").show();
		$(".step_2").hide();
	}
	else if (action == 'step_2') {
		$('.form_step2').submit();
	}	
};

function changeSeminar (action) {
	if (action == 'step_1') {
		$(".step_1").show();
		$(".step_2").hide();
		$(".step_3").hide();
	}
	else if (action == 'step_3') {
		$('.form_step2').submit();
	}	
};

function registerSeminar (id, price) {
	$(".step_1").hide();
	$(".step_2").show();
	$("#seminar_id").attr("value", id);
	$(".total_price").html(price);
}

function updateBasket (action, id) {
	$.ajax({ url: "http://www.novaepoha.com/actions.php?action="+action,
		data: ({id: id}),
		type: "POST",
		cache: false,
		dataType: "text",
		beforeSend: function () {
			startLoading ();
		},
		success: function(responce,textStatus) {  
			$(".basket_info").html(responce).after(function () {
					$(".total_price").html($(".num_price").html());
				});
			endLoading ();  
			if (action == 'order_books') {
					$(".basket_step_1").hide();
					$(".basket_step_2").hide();	
					$(".basket_step_3").show();
			}
			if (action == 'removeAllItem') {
				$("#prod_"+id).remove();
			}
			if (action == 'removeItem') {
				var quantity = $("#prod_"+id).find(".quantity").html();
				$("#prod_"+id).find(".quantity").html((parseInt(quantity)-1));
				if (quantity == 1) {
					$("#prod_"+id).remove();
				}
			}
			if (action == 'addItem') {
				var quantity = $("#prod_"+id).find(".quantity").html();
				$("#prod_"+id).find(".quantity").html((parseInt(quantity)+1));
			}						
		},
    error:
    	function(xhr,error){
    		alert ("HTTP status:" + xhr.status + " \n  "+ "Response Text: "+xhr.responseText );
			}
		});
};

var foto1;
function CaricaFoto(img) {
	foto1 = new Image();
	foto1.src = (img);
	Controlla(img);
}

function Controlla(img) {
	if ( (foto1.width != 0) && (foto1.height != 0) ) {
		viewFoto(img);
//		alert(foto1.width +"::"+ foto1.height)
	}
	else{
		funzione = "Controlla('" + img + "')";
		intervallo = setTimeout(funzione, 500);
	}
}

function viewFoto(img) {
	largh = foto1.width+20;
	altez = foto1.height+30;
	stringa = "width=" + largh + ",height=" + altez + ",status=0";
//	alert (stringa);
	finestra=window.open(img, "photo_wnd", stringa);
	if (finestra != null)
	{
		finestra.focus();
	}
	else
	{
		alert('Pop up blocker activated!\nTo view large image, please use \'ctl\' + click.');
	}	
		
	return false;
}

function change_theimg(picture) {
	document.theimg.src = picture
}
function change_theimg2(picture) {
	document.theimg2.src = picture
}

var ie5=document.all && !window.opera
var ns6=document.getElementById

if (ie5||ns6)
	document.write('<div id="popitmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>')

function iecompattest(){
	return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}

function findPos() {
	var p = $("#methods_link");
	var position = p.position();
	return [position.left, position.top];	
}

function showmenu(e, which, optWidth){
	if (!document.all&&!document.getElementById)
		return
	clearhidemenu()
	var coors = findPos();
	$("#popitmenu").css({
	  width: '210px', 
	  contentheight: '420px',
	  top: coors[1]+9+"px",
	  left: coors[0]+41+"px"
  }).html(which);	
	$("#popitmenu").show();
	return false
}

function contains_ns6(a, b) {
	if ((b = b.parentNode) == a)
		return true;
	return false;
}

function hidemenu(){
	$("#popitmenu").hide();
}

function dynamichide(e){
	if (ie5&&!menuobj.contains(e.toElement))
		hidemenu()
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		hidemenu()
}

function delayhidemenu(){
	delayhide=setTimeout("hidemenu()",500)
}

function clearhidemenu(){
	if (window.delayhide)
		clearTimeout(delayhide)
}

if (ie5||ns6)
	document.onclick=hidemenu

