var categories = false;
var language = false;
var currency = false;
var sortby = false;
var filter_flag = false;

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function Nascondi(option){
	$('.menu-option-cart').fadeOut({opacity: 0.2}, 300);
	$('.menu-option-wishlist').fadeOut({opacity: 0.2}, 300);
	if(option==null && option!='currecy'){
		$('.menu-option-currency').fadeOut({opacity: 0.2}, 300);
		$('.menu-option-sep').fadeOut({opacity: 0.2}, 300);	
	}
}

function Mostra(){
	$('.menu-option-cart').fadeIn({opacity: 1}, 300);
	$('.menu-option-sep').fadeIn({opacity: 0.2}, 300);
	$('.menu-option-wishlist').fadeIn({opacity: 1}, 300);
	$('.menu-option-currency').fadeIn({opacity: 0.2}, 300);
}

function NascondiSort(div){
	$('#'+div).css('-moz-border-radius','5px 5px 0px 0px');
	$('#'+div+'-content').css('border-top','0px solid #FFF');
	$('#'+div).css('border-bottom','0px solid #FFF');
}

function MostraSort(div){
	$('#'+div).css('-moz-border-radius','5px 5px 5px 5px');
	$('#'+div+'-content').css('border-top','1px solid #FFF');
	$('#'+div).css('border-bottom','2px solid #4DA7CF');
}

function addWishlist(idtour){
	newFav = idtour;
	newFavorites = new Array();
	if (!isNaN(newFav)) {
		sFavorites = getsetCookie('CAF_fav');
		if (typeof sFavorites == "string") { 
			aFavorites = sFavorites.split(",");
		} else {
			aFavorites = new Array();
		}
		for (i=0; i < aFavorites.length; i++) {
			if (parseInt(aFavorites[i]) == newFav) {
				return;
			} else if (!(isNaN(parseInt(aFavorites[i])))) {
				newFavorites.push(aFavorites[i]);
			}
		}
		newFavorites.push(newFav);
		sFavorites = newFavorites.join(",");
		$.cookie('CAF_fav',sFavorites,{ path: '/', expires: 30 });
		alert(LABEL_JS_ADDED_FAV);
		if(idpagina==idwishlist){window.location.reload();}
		var div = '#wishlist-'+newFav;
		$(div).removeClass('wishlist');
		$(div).addClass('rm-wishlist');
		$('#wishlist_'+idtour).removeClass('wish');
		$('#wishlist_'+idtour).addClass('unwish');
		$('#wishlist_'+idtour).html(LABEL_JS_REMOVE_FROM_WISHLIST);
		$('#wishlist_'+idtour).attr('href', 'javascript:removeWishlist('+newFav+')');
	}
}

function removeWishlist(idtour){
	if (confirm(LABEL_JS_CONFIRM_FAV_REMOVE)) {
		remFav = idtour;
		newFavorites = new Array();
		if (!isNaN(remFav)) {
			sFavorites = getsetCookie('CAF_fav',30);
			aFavorites = sFavorites.split(",");
			for (i=0; i < aFavorites.length; i++) {
				if (parseInt(aFavorites[i]) != remFav) {
					newFavorites.push(aFavorites[i]);
				}
			}
			sFavorites = newFavorites.join(",");
			$.cookie('CAF_fav',sFavorites,{ path: '/', expires: 30 });
			alert(LABEL_JS_REMOVED_FAV);
			if(idpagina==idwishlist){window.location.reload();}
			var div = '#wishlist-'+remFav;
			$(div).removeClass('rm-wishlist');
			$(div).addClass('wishlist');
			$('#wishlist_'+idtour).removeClass('unwish');
			$('#wishlist_'+idtour).addClass('wish');
			$('#wishlist_'+idtour).html(LABEL_JS_ADD_TO_WISHLIST);
			$('#wishlist_'+idtour).attr('href', 'javascript:addWishlist('+remFav+')');
		}
	}
}

function getsetCookie(cookieName, validity){
	if($.cookie(cookieName)!= null){
		return $.cookie(cookieName);
	}else{
		return $.cookie(cookieName, '', { path: '/', expires: validity});
	}
}

function popupLogin(){
	var login = getsetCookie('login_popup', 0);
	if(login != null && login!=""){
		$('#popup').load('/ajax/popup_login.php?cookievalue='+login+'&lang='+lingua+'&idlingua='+idlingua, '', function(){
			$('#alertpopup').click();
			$.cookie('login_popup', '', {path:'/'});
		});
	}
}

function popup_show(id){
	var idpopup = id;
	$('#popup').load('/ajax/popup.php?lang='+lingua+'&idlingua='+idlingua+'&idpagepopup='+idpopup, '', function(){$('#alertpopup').click();});
}

function filter_by(filterId, search, starting_city, datada, dataa, interest){
	var filtro = filterId;
	var sort_by = $('#sortbyid').val();
	
	$('#tourlist').html('<div style="width:820px; height: 100%; text-align: center; padding-top: 100px"><img src="/images/ajax-loader3.gif" alt="'+LABEL_JS_LOADING+'..." style="margin: auto"/></div>');
	
	$.get('/ajax/tourlist.php?lang='+lingua+
		'&idpagina='+idpagina+
		'&filter='+filtro+
		'&sort='+sort_by+
		'&search='+search+
		'&starting_city='+starting_city+
		'&datada='+datada+
		'&dataa='+dataa+
		'&interest='+interest, 
		function(data){
			$('#tourlist').html(data);
			reinitialize_FilterSort();
	});
}

function sort_by(sortbyval, search, starting_city, datada, dataa, interest){
	var sort_by = sortbyval;
	var filtro = $('#filterid').val();

	$('#tourlist').html('<div style="width:820px; height: 100%; text-align: center; padding-top: 100px"><img src="/images/ajax-loader3.gif" alt="'+LABEL_JS_LOADING+'..." style="margin: auto"/></div>');
	
	$.get('/ajax/tourlist.php?lang='+lingua+
		'&idpagina='+idpagina+
		'&sort='+sort_by+
		'&filter='+filtro+
		'&search='+search+
		'&starting_city='+starting_city+
		'&datada='+datada+
		'&dataa='+dataa+
		'&interest='+interest, 		
		function(data){
			$('#tourlist').html(data);
			reinitialize_FilterSort();
	});
	
}

function reinitialize_FilterSort(){

	$('#sortby-content').mouseleave(function(){if(!sortby){$('#sortby-content').fadeIn({opacity: 1}, 300);sortby=true;NascondiSort('sortby');}else{$('#sortby-content').fadeOut({opacity: 0}, 300);sortby=false; MostraSort('sortby');}});
	$('#filter-content').mouseleave(function(){if(!filter_flag){$('#filter-content').fadeIn({opacity: 1}, 300);filter_flag=true;NascondiSort('filter');}else{$('#filter-content').fadeOut({opacity: 0}, 300);filter_flag=false; MostraSort('filter');}});
	$('#sortby-content li a').click(function(){$('#sortby-content').fadeOut({opacity: 1}, 300);sortby=false;$('.sortby-option').text($(this).text()); MostraSort('sortby');});
	$('#filter-content li a').click(function(){if(!filter_flag){$('#filter-content').fadeIn({opacity: 1}, 300);filter_flag=true;NascondiSort('filter');}else{$('#filter-content').fadeOut({opacity: 1}, 300);filter_flag=false;$('.filter-option').text($(this).text());MostraSort('filter');}});
	$('#sortby').click(function(){if(!sortby){$('#sortby-content').fadeIn({opacity: 1}, 300);sortby=true;NascondiSort('sortby');}else{$('#sortby-content').fadeOut({opacity: 0}, 300);sortby=false;MostraSort('sortby');}});	
	$('#filter').click(function(){if(!filter_flag){$('#filter-content').fadeIn({opacity: 1}, 300);filter_flag=true;NascondiSort('filter');}else{$('#filter-content').fadeOut({opacity: 0}, 300);filter_flag=false;MostraSort('filter');}}); 
	//Pagination
	$('#tourlist').pajinate({
		items_per_page: 10,
		nav_label_first : '<<',
		nav_label_last : '>>',
		nav_label_prev : '<',
		nav_label_next : '>' 
	});	
}

function get_rescue(email){

	var form = document.forms['frm_rescue'];
	var email = form.email.value;	

	$.get('/ajax/recupera.php?email='+email, function(data){
		$('#innerPopup').html(data);
	});	

}

function get_change_passwd(passwd, confirm){

	var form = document.forms['frm_change_psw'];
	var passwd = form.passwd.value;	
	var confirm = form.confirm_passwd.value;	

	$.get('/ajax/cambia_passwd.php?passwd='+passwd+'&confirm='+confirm+'&idlingua='+idlingua, function(data){
		$('#innerPopup').html(data);
	});	
}

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 30;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("span.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<div id='tooltip'>"+ this.t +"</div>");
		yOffset = $('#tooltip').height() + 55;
		$("#tooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(e.pageX - xOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

$(document).ready(function() {
	$('#galleria').jcarousel();
	var isIE = (navigator.userAgent.indexOf("MSIE") > 0) ? true : false;	
	//Fix problemi select booking box
	if(isIE){
		$(function() {$('.booking-input-wide')
			.click(function(){
				$(this).css('width', 'auto');
			})
			.focusin(function(){
				$(this).css('width', 'auto');
			})
			.blur(function(){
				$(this).css('width', '225px');
			});
			$('.booking-input-wide').change(function(){
				$('#select_tour_day').focus();
			});
		});
	}
	//Menu categories
	$('#menu-categories-btn').click(function(){if(!categories){$('#menu-categories-content').fadeIn({opacity: 1}, 300);categories=true;}else{$('#menu-categories-content').fadeOut({opacity: 0}, 300);categories=false;}});
	$('#menu-categories-content').mouseleave(function(){$('#menu-categories-content').fadeOut({opacity: 0}, 300);categories=false;});
	// slideshow
	$('#slideshow').cycle({fx: 'fade'});
	//Filter  SE SI CAMBIA QUALCOSA QUI CAMBIARE ANCHE NELLA FUNZIONE REINITIALIZE_FILTERSORT
	$('#filter').click(function(){if(!filter_flag){$('#filter-content').fadeIn({opacity: 1}, 300);filter_flag=true;NascondiSort('filter');}else{$('#filter-content').fadeOut({opacity: 0}, 300);filter_flag=false;MostraSort('filter');}}); 
	$('#filter-content').mouseleave(function(){if(!filter_flag){$('#filter-content').fadeIn({opacity: 1}, 300);filter_flag=true;NascondiSort('filter');}else{$('#filter-content').fadeOut({opacity: 0}, 300);filter_flag=false; MostraSort('filter');}});
	$('#filter-content li a').click(function(){if(!filter_flag){$('#filter-content').fadeIn({opacity: 1}, 300);filter_flag=true;NascondiSort('filter');}else{$('#filter-content').fadeOut({opacity: 1}, 300);filter_flag=false;$('.filter-option').text($(this).text());MostraSort('filter');}});
	//Sort	  SE SI CAMBIA QUALCOSA QUI CAMBIARE ANCHE NELLA FUNZIONE REINITIALIZE_FILTERSORT
	$('#sortby').click(function(){if(!sortby){$('#sortby-content').fadeIn({opacity: 1}, 300);sortby=true;NascondiSort('sortby');}else{$('#sortby-content').fadeOut({opacity: 0}, 300);sortby=false;MostraSort('sortby');}});	
	$('#sortby-content').mouseleave(function(){if(!sortby){$('#sortby-content').fadeIn({opacity: 1}, 300);sortby=true;NascondiSort('sortby');}else{$('#sortby-content').fadeOut({opacity: 0}, 300);sortby=false; MostraSort('sortby');}});
	$('#sortby-content li a').click(function(){$('#sortby-content').fadeOut({opacity: 1}, 300);sortby=false;$('.sortby-option').text($(this).text()); MostraSort('sortby');});
	//Language
	$('.menu-option-language').click(function(){if(!language){$('#menu-language-content').fadeIn({opacity: 1}, 300);language=true;Nascondi();}else{$('#menu-language-content').fadeOut({opacity: 0}, 300);language=false;Mostra();}});
	$('#menu-language-content').mouseleave(function(){$('#menu-language-content').fadeOut({opacity: 0}, 300);language=false;Mostra();});
	$('#menu-language-content li a').click(function(){$('#menu-language-content').fadeOut({opacity: 1}, 300);language=false;Mostra();});
	//Currency
	$('.menu-option-currency').click(function(){if(!currency){$('#menu-currency-content').fadeIn({opacity: 1}, 300);currency=true;Nascondi('currency');}else{$('#menu-currency-content').fadeOut({opacity: 0}, 300);currency=false;Mostra();}});
	$('#menu-currency-content').mouseleave(function(){$('#menu-currency-content').fadeOut({opacity: 0}, 300);currency=false;Mostra();});
	$('#menu-currency-content li a').click(function(){$('#menu-currency-content').fadeOut({opacity: 1}, 300);currency=false;Mostra();});
	//Popup Login
	popupLogin();
	//Booking procedure
	$('.popup_pages').click(function(){popup_show($(this).attr('rel'));	return false;});
	//Booking box popup
	$('.popup_booking_box').prettyPhoto({overlay_gallery: false, keyboard_shortcuts: false,show_title: false,hideflash: true,wmode: 'transparent',	allow_resize: false, default_width: 251, default_height: 430, iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" scrollbars="no" frameborder="no"></iframe>'}); 
	//Forgot Password
	$('.forgot_password').prettyPhoto({overlay_gallery: false, keyboard_shortcuts: false,show_title: false,hideflash: true,wmode: 'transparent',	allow_resize: false, default_width: 430, default_height: 430, iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" scrollbars="no" frameborder="no"></iframe>'});  
	//Change Password
	$('.change_password').prettyPhoto({overlay_gallery: false, keyboard_shortcuts: false,show_title: false,hideflash: true,wmode: 'transparent',	allow_resize: false, default_width: 430, default_height: 430, iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" scrollbars="no" frameborder="no"></iframe>'}); 
	//Popup generico
	$('#alertpopup').prettyPhoto({overlay_gallery: false, keyboard_shortcuts: false,show_title: true,hideflash: true,wmode: 'transparent',	default_width: 500,	default_height: 300}); 
	// Prettyphoto
	$("a[rel^='prettyPhoto']").prettyPhoto({overlay_gallery: false,keyboard_shortcuts: false,show_title: false,hideflash: true,wmode: 'transparent',default_width: 800});
	// popup transaction ko
	if (transaction_ko>0) {
		$('#popup').load('/ajax/popup.php?lang='+lingua+'&idlingua='+idlingua+'&idpagepopup='+transaction_ko, '', function(){$('#alertpopup').click();});
	}	
	// popup coupon error
	if (coupon_error>0) {
		$('#popup').load('/ajax/popup.php?lang='+lingua+'&idlingua='+idlingua+'&idpagepopup='+coupon_error, '', function(){$('#alertpopup').click();});
	}
	tooltip();
	// Review rating
	$('#reviews :radio.star').rating();
	//$('#div_booking_calendarbox').mouseleave(function(){$('#div_booking_calendarbox').fadeOut({opacity: 0}, 300);calendar=false});
	
	// disable right click
	$(this).bind("contextmenu", function(e) {
		if (document.domain != 'caf.aperion.it') {
			e.preventDefault();
			alert("Copyright 2011 Caf Tour & Travel srl.\n\nALL RIGHTS RESERVED.");
		}
    });
	//Pagination
	$('#tourlist').pajinate({
		items_per_page: 10,
		nav_label_first : '<<',
		nav_label_last : '>>',
		nav_label_prev : '<',
		nav_label_next : '>',
		abort_on_small_lists: true,
		num_page_links_to_display : 5
	});	
});

