// openFlag Flag
var openFlag = false;
var base_url = "http://www.godzila.bg/index.php/bg/";
var base_url_images = "http://www.godzila.bg/";

var which_group_to_open;

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function charlim(object, limit, limid)
{
	var left = (limit - object.value.length);
	
	if (left <= 0)
	{
		object.value = object.value.substring(0, limit);
	}
	
	left = (limit - object.value.length);
	
	document.getElementById(limid).innerHTML = left;
}




// ---------------------------------------------------------
// BODY READY


$(document).ready(function(){
   
	// prepare left menu
	prepare_left_menu();

});
// ---------------------------------------------------------

function prepare_left_menu() {
		
	 // Autoclose all sublinks	
	$("#left_menu_listing .menu_links").next().hide();
	
	// rollover the main links
	$("#left_menu_listing .menu_links").hover(
      function () {
        $(this).removeClass();
	  	$(this).addClass("menu_on");
      }, 
      function () {
        $(this).removeClass();
	  	$(this).addClass("menu_links");
      }
    );
		
		

	
	 $("#left_menu_listing .menu_links").click(function(){
      
        $(this).next().slideToggle();
      

	  /*
	  $("#block").animate({ 
        width: "70%",
        opacity: 0.4,
        marginLeft: "0.6in",
        fontSize: "3em", 
        borderWidth: "10px"
      }, 1500 );
      */

      
    });
	
	// dali triabwa da otworim neshto
	if (which_group_to_open!=undefined) {
		//alert("which_group_to_open="+which_group_to_open);
		$("#left_menu_group_"+which_group_to_open).next().slideToggle();
		$("#left_menu_group_"+which_group_to_open).removeClass();
	  	$("#left_menu_group_"+which_group_to_open).addClass("menu_on");	
	  	which_group_to_open=undefined;
	}
	
	
	
}




function extras_update(day, item_id, extra_id, extra_value) {
	
	//alert("extras_update("+day+", "+item_id+", "+extra_id+", "+extra_value+")");

        $.getJSON(base_url+"actions/order_setExtra/"+day+"/"+item_id+"/"+extra_id+"/"+extra_value,
        function(data){

                 $('#extras_'+item_id+'_text').html(data.text);
                 $('#extras_'+item_id+'_sum').html(data.sum);

        });

}

function updateOrder(day,what_after)
{
	// rollover all the items notes
	curNotes = new Array();
	curBroi = new Array();
	vCode = $("input[id=voucher_code]").val();
	
	$("input[id^=utochnenia_]").each(function (i)
	{
		curNotes.push($(this).val());
	});
	
	$("input[id^=broi_]").each(function (i)
	{
		curBroi.push($(this).val());
	});
	
	var post_object =
	{
		notes: curNotes.join("^"),
		broi: curBroi.join("^"),
		voucher_code: vCode,
		pribori_broi: $('#pribori_broi').val()
	};
	
	
	//alert("post_object.notes = " + post_object.notes +"\n"+ "post_object.broi = " + post_object.broi +"\n"+"post_object.pribori_broi = " + post_object.pribori_broi +"\n");
	
	$.post(base_url + "actions/order_UpdateNotes/" + day, post_object, function(data)
	{
		titlesArray=data.split("\^");
		
		if (what_after=="confirm")
		{
			document.location = base_url+'order/confirm/'+day;
		}
		else
		{
			// display the popup
			//OpenPopup(titlesArray[0],titlesArray[1]);	
		}
	});
}


function final_updateOrder(day) {

	var post_object = {
            chas_dostavka: $('#chas_dostavka').val(),
	    nachin_plashtane: $("input[name='nachin_plashtane']:checked").val()
        };

	//alert("post_object.chas_dostavka = " + post_object.chas_dostavka +"\n"+"post_object.nachin_plashtane = " + post_object.nachin_plashtane +"\n");

        $.post(base_url+"actions/order_UpdateNotes/"+day, post_object,

          function(data){

             titlesArray=data.split("\^");
             
             // display the popup
             //OpenPopup(titlesArray[0],titlesArray[1]);
			 
			 document.location = base_url+'order/final_confirm/'+day;
			 
			 
          });




}


function confirmOrder(day) {

    // updating the order
	updateOrder(day,'confirm');

    
}

function finalConfirmOrder(day) {

    // updating the order
    final_updateOrder(day);


}

// ---------------------------------------------------------------------

  
function town_change(_dropdown_value) {
	

	url=base_url+"actions/getJSON_Raioni_za_Town/"+_dropdown_value+"/";
	//alert("url = "+url);

	$.getJSON(url,
	function(data){
		
		
		//alert('count = '+data.items.length);

		// clearing the dropdown but the first item
		$("#raion :lt(500)").remove();
		
	
		$.each(data.items, function(i,item){
		
			//alert("item.id,item.name = "+item.id+","+item.name);	
			$("#raion").append(new Option(item.name, item.id));
			
			
		});
		
		$("#raion :eq(0)").attr("selected","selected"); // selecting the first item again
		
		// highlight the combobox
		if (data.items.length>0) {
			$('#raion').highlightFade('#aaff00');
		} else {
			$('#raion').highlightFade('red');
		}
	

	});	

}

function checkUsername(username) {

	//alert("checkUsername("+username+")");


	$.getJSON(base_url+"actions/checkUsername/"+username+"/",
	function(data){
		
		
		//alert('data.status = '+data.status);

		if (data.status==1) {
			$('#username_check').attr("src",base_url_images+'images/zaeto_bg.gif');
			$('#username').css({'background-color' : '#fccdb4','border' : '1px solid #D1D1D1'});
		} else {
			$('#username_check').attr("src",base_url_images+'images/svobodno_bg.gif');
			$('#username').css({'background-color' : '#d4fcb4','border' : '1px solid #D1D1D1'});
		}
		/*
		// highlight the combobox
		if (data.items.length>0) {
			$('#raion').highlightFade('#aaff00');
		} else {
			$('#raion').highlightFade('red');
		}
		*/

	});	


}



function confirmDelete(message) {

    return confirm(message);
    
}


function updateOrderStatus(order_id){
    
    
    $.getJSON(base_url+"actions/order_getNote/"+order_id+"/"+Math.random(),
        function(data){
        	
        	if (data.note!="") {
        		$('#status_message').html('<span>'+data.note+'</span>');
				
				// clear the update interval
				clearTimeout ( order_update_status_interval );	
       		}
        	
        });

    
    
}




function load_left_porychka(day){
    

    $.getJSON(base_url+"actions/getOrderJSON/"+day+"/"+Math.random(),
        function(data){
        	
        	$("#porychka_sum_hrani").html(data.sum_hrani);
			$("#porychka_sum_kutii").html(data.sum_kutii);
			$("#porychka_sum_dostavka").html(data.sum_dostavka);
			$("#porychka_sum_total").html(data.sum_total);
			
			// kakwo da prawim 
			if (day=="dnes") {
				
				// dnes
				$('#porychka_liavo').html('<div class="orderbtns_on">за днес</div>');	
				$('#porychka_diasno').html('<a href="javascript:void(0);" onclick="load_left_porychka(\'utre\');" class="orderbtns">за утре</a>');	
			} else {
				
				// utre
				$('#porychka_liavo').html('<a href="javascript:void(0);" onclick="load_left_porychka(\'dnes\');" class="orderbtns">за днес</a>');
				$('#porychka_diasno').html('<div class="orderbtns_on">за утре</div>');	
	
			}
			
			// update na linka w kareto
			$('#porychka_link').attr('href',base_url+"order/display/"+day);
			
			        	
        });

    
    
}









































// ---------------------------------------------------------
function OpenPopup(title,subtitle) {
	

	if (openFlag==false) {
		// otwariame go
		openFlag=true;
		// open the popup
		
		
		// Finding and position of the popup
		var BodyCoors=getPageCoords($('body'));
		
		// attaching the message window
		$("#after_body").after('<div id="message"><a href="javascript:void(0);" onclick="PopupClose();">X</a><h1>'+title+'</h1>'+subtitle+'</div>');

		// showing the message
		$('#message').show("slow");
		
		// centering the popup
		$('#message').center();

		
		setTimeout(PopupClose,1000);
		
		// --------------------------------

	} else {
		// zatwariame go
		openFlag=false;
		
		$('#message').fadeOut("slow");

		$("#message").remove();


	}
	
}	


// ---------------------------------------------------------
function PermaPopup(title,subtitle) {
	

	if (openFlag==false) {
		// otwariame go
		openFlag=true;
		// open the popup
		
		
		// Finding and position of the popup
		var BodyCoors=getPageCoords($('body'));
		
		// attaching the message window
		$("#after_body").after('<div id="message"><a href="javascript:void(0);" onclick="PopupClose();">X</a><h1>'+title+'</h1>'+subtitle+'</div>');

		// showing the message
		$('#message').show("slow");
		
		// centering the popup
		$('#message').center();
		
		// --------------------------------

	} else {
		// zatwariame go
		openFlag=false;
		
		$('#message').fadeOut("slow");

		$("#message").remove();


	}
	
}	
// -----------------------------


// ---------------------------------------------------------
function PermaCongrats(title,subtitle) {
	

	if (openFlag==false) {
		// otwariame go
		openFlag=true;
		// open the popup
		
		
		// Finding and position of the popup
		var BodyCoors=getPageCoords($('body'));
		
		// attaching the message window
		$("#after_body").after('<div id="message_congrats"><a href="javascript:void(0);" onclick="PopupClose();">X</a><h1>'+title+'</h1>'+subtitle+'</div>');

		// showing the message
		$('#message_congrats').show("slow");
		
		// centering the popup
		$('#message_congrats').center();
		// --------------------------------

	} else {
		// zatwariame go
		openFlag=false;
		
		$('#message_congrats').fadeOut("slow");

		$("#message_congrats").remove();


	}
	
}


function PopupClose() {

	// nulirame flaga na listennow
	openFlag=false;
	
	$('#message').fadeOut("slow",function () {
     $(this).remove();
    });
	
	
	$('#message_congrats').fadeOut("slow",function () {
     $(this).remove();
    });

}



function getPageCoords (el) {
  var coords = {x: 0, y: 0};
  do {
    coords.x += el.offsetLeft;
    coords.y += el.offsetTop;
  }
  while ((el = el.offsetParent));
  return coords;
}  



function getPageSize() {
	        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function createPlayer(theFile,destinationDiv,width,height) {

	var s1 = new SWFObject(theFile,'player',width,height,'9');
	s1.addParam('allowfullscreen','false');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('wmode','transparent');
	s1.write(destinationDiv);

	
}


