// popup menu action
var timeout=500;
var mmtimer=0;
var mmitem=0;

function mmenu_open(){
	mmenu_clear();
	mmenu_close();
	mmitem = $(this).find('ul').eq(0);
	w1=Math.ceil(parseFloat($(this).width()));
	w2=parseInt(mmitem.width());
	mmitem.css('width', ((w1>w2)?(w1+'px'):(w2+'px'))).css('visibility', 'visible');
}
function mmenu_close(){
	if(mmitem)
		mmitem.css('visibility', 'hidden');
}
function mmenu_timer(){
	mmtimer = window.setTimeout(mmenu_close, timeout);
}
function mmenu_clear(){
	if(mmtimer){
		window.clearTimeout(mmtimer);
		mmtimer = null;
	}
}

// get node bounds
function getBounds(element){
	var left = element.offsetLeft;
	var top = element.offsetTop;
	for (var parent = element.offsetParent; parent; parent = parent.offsetParent){
		left += parent.offsetLeft - parent.scrollLeft;
		top += parent.offsetTop - parent.scrollTop;
	}
	return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

// header action
function play(){
	$('#header-bg').css('background-image', "url('"+imgs[plimg].bg.src+"')");
	$('#header-img').css('background-image', "url('"+imgs[plimg].img.src+"')");
	plimg=++plimg%imgs.length;
}
function writer(){
	$.getJSON('/ajax/main/pressman_review.php',{},function(data){
		if(data.error==1) alert(data.errorText);
		else $('#writer-div').html(data.html);
	});
}
function openW(url,w,h,s){
	var l=Math.floor((screen.width-w)/2);
	var t=Math.floor((screen.height-h)/2);
	window.open(url,'','width='+w+',height='+h+',top='+t+',left='+l+',resizable=Yes,scrollbars='+s);
}
function getPrintVersion(){
	var s=location.pathname;
	openW('/en/print_version/' + s.substr(s.indexOf('/en/')+4),950,600,'Yes');
}
function showImage(img){
	openW('/UserFiles/view.php?img='+img,100,100,'No');
}
function openURL(url){
	if(url.indexOf('tour_search')!=-1) document.location=url;
	else openW('/en/print_version/' + url.substr(url.indexOf('/en/')+4),950,600,'Yes');
}
function validate(email) {
	return testem(email);
}
function testem(src) {
	if(!src) return false;
    var emailReg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\D{2,4})+$/;
    var regex = new RegExp(emailReg);
    return regex.test(src);
}

function do_search(){
	var keyword=$('#search-field').val();
	if(!keyword) return alert('Please specify search keyword!');
	location='/en/do_search/'+keyword+'/';
}

function subscibe(){
	mail=$('#subscribe').val();
	if(!testem(mail))
		return alert('Please specify right email address!');
	$.getJSON('/ajax/main/subscribe_newsletter.php', {mail:mail, lang:lang}, function (data){
		if(data.error==1) alert(data.errorText);
		else{
			$('#subscribe').val($('#subscribe').attr('title'));
			alert(data.html);
		}
	});
}
function buildCalendar(button, field){
	Calendar.setup({
        inputField     :    field,
        ifFormat       :    "%d.%m.%Y",
        button         :    button,
        showsTime      :    false,
        align          :    "Bl",
        singleClick    :    true
    });
}
function quickSearch(){
	var tour_ref=$('#quick-choose-tour').val();
	if(tour_ref!='0') return location=tour_ref;
	
	var param='';
	val=$('#quick-choose-block').val();
	if(val!='0') param+='&block='+val;
	val=$('#quick-choose-region').val();
	if(val!='0') param+='&region='+val;
	val=$('#date-tour-from').val();
	if(val!='') param+='&from='+val;
	val=$('#date-tour-till').val();
	if(val!='') param+='&till='+val;
	
	if(param!='') param='?'+param.substring(1);
	location='/en/quick_trip_finder.html'+param;
}
	

// img preload
var preimg=[];
var prelist=[
	'/images/template/btn-guide-active.gif',
	'/images/template/btn-book-active.gif',
	'/images/template/btn-search-active.gif',
	'/images/template/tour-info-bottom.gif',
	'/images/template/tour-info-top.gif'
];

for(i=0;i<prelist.length;i++){
	var img=new Image(); img.src=prelist[i];
	preimg[i]=img;
}

var plid;
var wrid;
var plimg=0;
var imgs=[];

window.onload=function(){
	// head img preload
	var ilist=[1,15,18,2,3,5,7,8,10,11,14];
	var ipath='/images/header/img';
	for(i=0;i<ilist.length;i++){
		var img=new Image(); img.src=ipath+ilist[i]+'.jpg';
		var bg=new Image(); bg.src=ipath+ilist[i]+'_bg.jpg';
		imgs[i]={img:img,bg:bg};
	}
	plid = setInterval("play()", 5000);
}
$(document).ready(function(){
	// --- logo click action -------
	$('#logo-png').click(function(){location='/';});
	// --- popup menu action -------
	$('.tm').bind('mouseover', mmenu_open);
	$('.tm').bind('mouseout',  mmenu_timer);
	// --- photogallery action -------
	$('.gallery-image').hover(
		function(){$(this).css('background-color', '#b5d266');},
		function(){$(this).css('background-color', '#eef4dc');}
	);
	// --- top tours action -------
	$('.top-tours-tour').mouseover(function(){
		mmenu_clear();
		$(this).css('background-color', '#f9fbf3');
		id=$(this).attr('id')+'-info';
		$('#top-tour-infotext').html($('#'+id).html());
		var bounds=getBounds($(this).get(0));
		cw=$('#top-tour-info').width();
		ch=$('#top-tour-info').height();
		t=bounds.top;//+(bounds.height-ch)/2;
		l=bounds.left-cw+7;
		mmitem = $('#top-tour-info').css('left', l).css('top', t).css('visibility', 'visible');
	});
	$('.top-tours-tour').mouseout(function(){
		$(this).css('background-color', '');
		/*$('#top-tour-info').css('visibility', 'hidden');*/
		mmtimer = window.setTimeout(mmenu_close, timeout);
	});
	$('#top-tour-info').mouseover(function(){
		mmenu_clear();
	});
	$('#top-tour-info').mouseout(function(){
		mmtimer = window.setTimeout(mmenu_close, timeout);
	});
	$('#top-tours-region').change(function(){
		var val=$(this).val();
		if(val!='0')location='/en/quick_trip_finder.html?region='+val;
	});
	// --- subscribe & search bars action -------
	$('.text-input').focus(function(){
		if($(this).val()==$(this).attr('title'))
			$(this).val('');
	});
	$('.text-input').blur(function(){
		if($(this).val()=='')
			$(this).val($(this).attr('title'));
	});
	$('#subscribe').keypress(function(e){
		if(e.which==13)subscibe();
	});
	$('#go-subscribe').click(function(e){
		subscibe();
	});
	$('#search-field').keypress(function(e){
		if(e.which==13)do_search();
	});
	$('#go-search-field').click(function(e){
		do_search();
	});
	buildCalendar('date-tour-from','date-tour-from');
	buildCalendar('date-tour-till','date-tour-till');
	// ----------------------------
	wrid = setInterval("writer()", 7000);
	writer();
});