$guestbookpreload = 0;


function getLang(data) {
    var e= /http:\/\/[^\/]+\/([^\/]{2})(?:\/[^\/]*)*$/;
    if (data.match(e)) {
        return  RegExp.$1;
    } else {
        return  "";
    }
};

// tabai
$(function()
{
	if(!jQuery.browser.opera){
		var myUrl = parseUrl(window.location.toString());
		var curUrl = myUrl.url.split('?')[0]; //atskiriam get dali
		$("#apie_sodyba").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/info");
			//window.location = $(this).attr('href');
			return false;
		});
		$("#video").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/videoembed");
			return false;
		});
		$("#kontaktai").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/contacts");
			return false;
		});
		$("#susisiekite").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/contactowner");
			return false;
		});
		$("#paslaugos_ir_pramogos").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/entertain");
			return false;
		});
		$("#kainorastis").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/pricelist");
			return false;
		});
		$("#sveciu_knyga").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/guestbook");
			return false;
		});
		$("#nuotraukos").click(function(){
			$("#countrytabs li").removeClass("selected");
			$(this).parent().addClass("selected");
			$("#mycontent").html('');
			$("#mycontent").load(curUrl+"/viewalbums"/*, {}, bind_gallery_links*/);
			return false;
		});
		$("#more_images").click(function(){
			$("#nuotraukos").click();
			return false;
		});

		$().ajaxSend(function(event, XMLHttpRequest, ajaxOptions){
			if(ajaxOptions.data){
				$data = unserialize(ajaxOptions.data);
				$($data.container).html("");
				$($data.container).addClass($data.loadclass);
			} else {
				$("#mycontent").addClass("loading");
			}
		});

		$().ajaxComplete(function(event, XMLHttpRequest, ajaxOptions){
			if(ajaxOptions.data){
				$data = unserialize(ajaxOptions.data);
				$($data.container).removeClass($data.loadclass);
			} else {
				$("#mycontent").removeClass("loading");
			}
			repretty();
			rest_gal_click();
		});
	}
});

function parseUrl(data) {
    var e= /(http:\/\/[^\/]+\/(?:[^\/]{2}\/)?[^\/]+)/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                shorturl: RegExp.$2};
    }
    else {
        return  {url: window.location.toString(), shorturl:""};
    }
};

// slaptazodzio laukas
$(function()
{
	$("#password_field2").focus(function(){
		if($(this).attr('value')=="slaptažodis"){
			$(this).hide();
			$("#password_field1").show();
			$("#password_field1").focus();
		}
	});
	$("#password_field1").blur(function(){
		if($(this).attr('value')==''){
			$(this).hide();
			$("#password_field2").show();
		}
	});
});

// sveciu knyga
function switchpg(pagenumber) {
	
	var myUrl = parseUrl(window.location.toString());
	var curUrl = myUrl.url.split('?')[0]; //atskiriam get dali
	
	$(".pad16").html('');
	$(".pad16").load(curUrl+"/guestbook?page="+pagenumber);
	$(".pad16").ajaxStart(function(){
		$(this).addClass("loading");
	});
	$(".pad16").ajaxStop(function(){
		$(this).removeClass("loading");
	});
	$("#bookform").remove();
};

function repretty(){
	$(function()
	{
		$('#ownphoto').prettyPhoto({
			imageLoading: '/public/layouts/images/preload.gif',
			opacit: 0.8,
			showTitle: true,
			allowresize: true
		});
	});
}

// scrollable tabs
$(function()
{
	repretty();
	if((document.getElementById('countrytabs').offsetWidth)>598){
		$("#leftcntrl").show();
		$("#rightcntrl").show();
		$("#tabcont").scrollable({ 
	   		items:'.items'
		});
		$("#countrytabs").css("width","600px");
	}
});

// kalendorius
function calpg(pagenumber) {
	var lang = getLang(window.location.toString());
	if(lang!=''){
		lang = '/'+lang;
	}
	$("#calendar").load(lang+"/calendar/block/"+$("#farmstead_id").html()+"/"+$("#startdate").html()+"/"+pagenumber+"/2", {container:'#calcontainer', loadclass:'cal_loading'});
}

function unserialize(url) {
	   var params=url;
	   if (url.match(/\?(.+)$/)) {
	       // in case it is a full query string with ?, only take everything after the ?
	       params = RegExp.$1;
	   }
	// split the params
	var pArray = params.split("&");
	// hash to store result
	var pHash = {};
	// parse each param in the array and put it in the hash
	for(var i=0;i<pArray.length;i++) {
	    var temp = pArray[i].split("=");
	    pHash[temp[0]] = unescape(temp[1]);
	}
	return pHash;
}


// nuotrauku galerija
$(function()
{
	rest_gal_click();
});

function rest_gal_click(){
	$(".rest_gal").click(function(){
		var myUrl = parseUrl(window.location.toString());
		var curUrl = myUrl.url.split('?')[0]; //atskiriam get dali
		
		$("#mycontent").html('');
		$("#mycontent").load(curUrl+"/viewalbums/"+$(this).attr("rel"));
		return false;
	});
}
