jQuery.noConflict();

var myPosters;
var mySlideshow;
var searchRegion;
var MAP_SET_FROM_TOP = 1;
var MAP_SET_FROM_MAP = 2;
var MAP_SET_DEFAULT = 0;
var MAP_FLAG = MAP_SET_FROM_TOP;

// wywolywana jest z mapki flash
function setAllFromMap(locidValue){
	//alert("setAllFromMap " + locidValue);
	if (MAP_FLAG != MAP_SET_FROM_TOP) {
		setContentTopOffers(locidValue);
		MAP_FLAG = MAP_SET_FROM_MAP;
	}else
		MAP_FLAG = MAP_SET_DEFAULT;
}

function doNotChangeAllFromMap(){
	//stara funkcja wywolywana z mapki
}
//ustawia mapke flash na odpowiedni locid
function setMap(locidValue){
	//alert("setMap " + locidValue);
	if (MAP_FLAG != MAP_SET_FROM_MAP) {
		jQuery("#maps-box-main")[0].SetVariable("showLocid", locidValue);
		MAP_FLAG = MAP_SET_FROM_TOP;
	}else
		MAP_FLAG = MAP_SET_DEFAULT;
}

function initIdeas(){
	mySlideshow = jQuery("#ideas-box-content").slideshow({
        height:135,
        width:372,
		title:false,
		effect:'fade',
		effecttime:500,
		playframe:false,
		panel:false,
		nextclick:false,
		linkajax:true,
        callback:function(i,txt){}
    });
}

function loadIdeas(){
	jQuery("#idea-box-rArrow").click(function() {
		mySlideshow.nextSlide();
	}).removeClass("disable-rArrow").addClass("enable-rArrow");
	jQuery("#idea-box-lArrow").click(function() {
		mySlideshow.prevSlide();
	}).removeClass("disable-lArrow").addClass("enable-lArrow");
}

function initTopOffers(){
    searchRegion = jQuery("#search-region")[0];
	setHeaderTopOffers();
}
// zarzadza zmiana zawartosci w TOP5
function setContentTopOffers(value){
    value = value=="START"?"":value;
	value = setSearchRegion(value)?value:"";
	params = varArray["panstwoID"]=="twojewakacje"?{panstwo:value}:{locid:value};
	var topURL = "/topoffers.xml";
	jQuery("#top-box-content div").remove();
	jQuery("#top-box-btm-linker a").remove();
	jQuery("#top-box-hdr-link a").remove();
	jQuery("#top-box-hdr-link").append("<p>Proszę czekać ...</p>")
	jQuery("#top-box-hdr-link").addClass("loading");
	jQuery("#top-box-content").addClass("loading");
	jQuery("#top-box-lArrow").toggleClass("enable-lArrow", false);
    jQuery("#top-box-lArrow").toggleClass("disable-lArrow", true);
    jQuery("#top-box-rArrow").toggleClass("disable-rArrow", true);
    jQuery("#top-box-rArrow").toggleClass("enable-rArrow", false);
	jQuery("#top-box-lArrow").unbind("click");
	jQuery("#top-box-rArrow").unbind("click");
	jQuery.get(topURL, params, function(data){
        jQuery("#top-box").replaceWith(data);
		jQuery("#top-box-content").removeClass("loading");
		setHeaderTopOffers(value);
		setMap(value);		
	});
	
}
// konfiguruje naglowek TOP5
function setHeaderTopOffers(value){
    jQuery("#top-box-lArrow").toggleClass("enable-lArrow", false);
    jQuery("#top-box-lArrow").toggleClass("disable-lArrow", false);
    jQuery("#top-box-rArrow").toggleClass("disable-rArrow", false);
    jQuery("#top-box-rArrow").toggleClass("enable-rArrow", false);
    
	jQuery("#top-box-rArrow").click(function(){
            nextRegion = jQuery(searchRegion.options[searchRegion.selectedIndex + 1]).val()
			prevRegion =  (searchRegion.selectedIndex - 1) >= 0 ? jQuery(searchRegion.options[searchRegion.selectedIndex - 1]).val() : "";
			setContentTopOffers(nextRegion);
		});
	jQuery("#top-box-lArrow").click(function(){
            nextRegion = jQuery(searchRegion.options[searchRegion.selectedIndex + 1]).val()
			prevRegion =  (searchRegion.selectedIndex - 1) >= 0 ? jQuery(searchRegion.options[searchRegion.selectedIndex - 1]).val() : "";
			setContentTopOffers(prevRegion);
		});
	if (searchRegion.selectedIndex == 0) {
        jQuery("#top-box-lArrow").unbind("click");
		jQuery("#top-box-lArrow").toggleClass("disable-lArrow");
        jQuery("#top-box-rArrow").toggleClass("enable-rArrow");
	}else if (searchRegion.selectedIndex == searchRegion.length - 1) {
            jQuery("#top-box-lArrow").toggleClass("enable-lArrow");
            jQuery("#top-box-rArrow").toggleClass("disable-rArrow");
			jQuery("#top-box-rArrow").unbind("click");
    }else {
            jQuery("#top-box-lArrow").toggleClass("enable-lArrow", true);
            jQuery("#top-box-rArrow").toggleClass("enable-rArrow", true);
    }
	
	if(varArray["panstwoID"]!="twojewakacje" && value != null && value != "" ){ 
		var prefix = "Najpopularniejsze w ";
		var headerName = jQuery("#search-region option[value='" + value + "']").html();
		var headerLink = prefix + headerName;
		if(headerLink.length > 47)
			headerLink = headerLink.substring(0,42) + " ...";
		jQuery("#top-box-hdr-link a").html(headerLink);
		jQuery("#top-box-hdr-link a").attr("title",prefix + headerName);
	}
}

function setSearchRegion(regionValue){
	filtr="option[value='" + regionValue + "']";
	rgSlct=jQuery("#search-region option").filter(filtr)[0];
	if(rgSlct != null){
		rgSlct.selected = true;
		return true;
	}else{
		return false;
	}
}

function initSearcher(){
	jQuery("#searcher-tabs").tabs();
	jQuery("#searcher-tabs ul.ui-tabs-nav li").each(function(index){
		jQuery(document.createElement('li')).css({ width:"1px", "background-color":"#0FB900", height:"22px" }).insertAfter(this);
	});
	jQuery("#searcher-tabs ul.ui-tabs-nav li:first a").addClass("corner-top-left");
	jQuery("#searcher-tabs").tabs('select',0);
	
	/* Zmiana URLi */
	if (varArray["panstwoID"] == 'twojewakacje') {
		jQuery("#searcher-tabs ul.ui-tabs-nav li").children().each(function(){
			if(jQuery(this).html() == 'Wycieczki'){
				jQuery(this).attr('href','http://www.travelone.pl/#wysz-wycieczki');
			}else if(jQuery(this).html() == 'Narty'){
				jQuery(this).attr('href','http://www.skione.pl/#wysz-wycieczki');
			}else if(jQuery(this).html() == 'Bilety lotnicze'){
				jQuery(this).attr('href','http://www.skyone.pl/#wysz-wycieczki');
			}else if(jQuery(this).html() == 'Noclegi'){
				jQuery(this).attr('href','http://www.hotelone.pl/#wysz-wycieczki');
			};
		});
		jQuery("#searcher-tabs").bind('tabsselect', function(event, ui) {
			if (jQuery(ui.tab).html() == 'Wycieczki')
				window.location = 'http://www.travelone.pl';
			else if (jQuery(ui.tab).html() == 'Narty')
				window.location = 'http://www.skione.pl';
			else if (jQuery(ui.tab).html() == 'Bilety lotnicze')
				window.location = 'http://www.skyone.pl';
			else if (jQuery(ui.tab).html() == 'Noclegi')
				window.location = 'http://www.hotelone.pl';
		});
	}else{
		jQuery("#searcher-tabs ul.ui-tabs-nav li").children().each(function(){
		if(jQuery(this).html() == 'Wycieczki'){
			jQuery(this).attr('href','http://www.' + varArray["serwis"] + '/#wysz-wycieczki');
		}else if(jQuery(this).html() == 'Narty'){
			jQuery(this).attr('href','http://www.narty.' + varArray["serwis"] + '/#wysz-wycieczki');
		}else if(jQuery(this).html() == 'Bilety lotnicze'){
			jQuery(this).attr('href','http://www.bilety-lotnicze.' + varArray["serwis"] + '/#wysz-wycieczki');
		}else if(jQuery(this).html() == 'Noclegi'){
			jQuery(this).attr('href','http://www.hotele.' + varArray["serwis"] + '/#wysz-wycieczki');
		};			
		});
		jQuery("#searcher-tabs").bind('tabsselect', function(event, ui){
			if(jQuery(ui.tab).html() == 'Wycieczki')
				window.location='http://www.' + varArray["serwis"];
			else if(jQuery(ui.tab).html() == 'Narty')
				window.location='http://www.narty.' + varArray["serwis"];
			else if (jQuery(ui.tab).html() == 'Bilety lotnicze')
				window.location='http://www.bilety-lotnicze.' + varArray["serwis"];
			else if (jQuery(ui.tab).html() == 'Noclegi')
				window.location='http://www.hotele.' + varArray["serwis"];
		});
	}
	
    jQuery.getScript("/inc/js/common/calendar.js", function(){
        searchCalendar = new OneCalendar('searchCalendar', 0);
    });
    jQuery("#search-bottom-button").click(function(){
        search();
    });
    jQuery("#search-region").change(function(){
        setContentTopOffers(this.value);
		setHeaderTopOffers(this.value);
	});
//    jQuery("#feedback").click(function(){
//		openPopUp("http://www.austria.pl/feedback.xml","521","393");return false;
//	});
    jQuery("#search-warranty").hover(function(){
    	//openPopUp("http://www.travelone.pl/gwarancjaceny.html","500","450");return false;
    	jQuery("#gwarancja-main").show();
    	jQuery("#media-box").hide();
    },function(){
    	jQuery("#gwarancja-main").hide();
    	jQuery("#media-box").show();
    });
    
}

function search(){
    typeTrip = jQuery("#search-type-trip option:selected").val() != "" ? jQuery("#search-type-trip option:selected").val() : "";
    region = jQuery("#search-region option:selected").val() != "" ? jQuery("#search-region").attr("name") + "=" + jQuery("#search-region option:selected").val() : "";
    standard = "";
    if (jQuery("#search-class option:selected").val() == "low" || jQuery("#search-class option:selected").val() == "high") {
        standard = "kategoria=" + jQuery("#search-class option:selected").val();
    }
    else 
        if (jQuery("#search-class option:selected[value!='']").val() != null) {
            standard = "standard=" + jQuery("#search-class option:selected").val();
        }
    countOfDays = jQuery("#search-amount-days option:selected").val() != "" ? "pobyt=" + jQuery("#search-amount-days option:selected").val() : "";
    price = jQuery("#search-price option:selected").val() != "" ? jQuery("#search-price option:selected").val() : "";
    promo = jQuery("#search-check-promo:checked").val() == 1 ? "lm=1&promocje=1" : "";
    lastMinute = jQuery("#search-check-lm:checked").val() == 1 ? "lm=1" : "";
    allInclusive = jQuery("#search-check-ai:checked").val() == 1 ? "allInclusive=1" : "";
    freeSki = jQuery("#search-check-skis:checked").val() == 1 ? "freeski=1" : "";
    myDate = searchCalendar.cal_d.ocurDate;
    days = (myDate.getDate() < 10) ? "0" + myDate.getDate() : myDate.getDate();
    months = (myDate.getMonth() + 1 < 10) ? "0" + (myDate.getMonth() + 1) : myDate.getMonth() + 1;
    years = myDate.getFullYear()
	nowDays = (new Date().getDate() < 10) ? "0" + new Date().getDate() : new Date().getDate();
    nowMonths = (new Date().getMonth() + 1 < 10) ? "0" + (new Date().getMonth() + 1) : new Date().getMonth() + 1;
    nowYears = new Date().getFullYear()
    nowTermin = "termin=" + nowYears + "-" + nowMonths + "-" + nowDays;
	termin = "termin=" + years + "-" + months + "-" + days;
    paramsArray = [];
    paramsArray.push(typeTrip, region, standard, countOfDays, price, promo, lastMinute, allInclusive, freeSki, termin);
    params = "";
    jQuery.each(paramsArray, function(){
        if (this != "") 
            if (params == "") 
                params = this;
            else 
                params = params + "&" + this;
    });
    baseUrl = "/oferta/szukaj.xml?extern=1&wyszfr";
	location = params!=""?baseUrl+"&"+params:baseUrl;
}

function loadMapsBox(){
	var params = {
        quality: "high",
        allowscriptaccess: "always",
		wmode: "transparent"
    };
	if(varArray["panstwoID"]=="twojewakacje")
    	flashFileName="http://www.travelone.pl/flash/maps/mapa_swiata.swf";
	else
		flashFileName = "/flash/maps/" + varArray["panstwoID"] + ".swf";
	swfobject.embedSWF(flashFileName, "maps-box-main", "372", "268", "9.0.0", "", "", params, "");
	
	var tabCountries = ["grecja","turcja","bulgaria","majorka","egipt","hiszpania","czechy","emiratyarabskie","chorwacja","cypr","wyspykanaryjskie","portugalia","tunezja","brazylia","bali","kenia","maroko","izrael","stanyzjednoczone","szwajcaria", "chiny", "indie", "australia","jordania","kostaryka","peru","filipiny","niemcy","nowazelandia","kanada","wenezuela","irlandia","rosja","rpa","wyspyzielonegoprzyladka","wielkabrytania","fidzi","gambia","meksyk","malta-gozo"];
	if (jQuery.inArray(varArray["panstwoID"], tabCountries) != -1){
		jQuery("#maps-tabs").tabs('select',1);
		jQuery("#maps-tabs").tabs('disable',0);
		jQuery("#maps-tabs").bind('tabsselect', function(event, ui){
			if (ui.index == 1) 
				jQuery("#maps-box-main")[0].SetVariable("showByTabs", "lokacje");
			else 
				if (ui.index == 2) 
					jQuery("#maps-box-main")[0].SetVariable("showByTabs", "osrodki");
		});
	}
}

// wywolywana jest z arkusza XSL plakatow, na poziomie JS nie wiadomo ile plakatow bedzie prezentowanych 
function loadPosters(countOfPosters){
    jQuery(window).load(function(){
        allOK = false;
        if (countOfPosters > 1) 
            allOK = true;
        if (allOK == true) {
            jQuery.getScript("/inc/js/plakaty/mootools.v1.11.js", function(){
                jQuery.getScript("/inc/js/plakaty/front.jd.gallery.js", function(){
                    myPosters = new gallery($('myGallery'), {
                        imageSelector: "div.full",
                        thumbnailSelector: "div.thumbnail",
                        timed: allOK,
                        showCarousel: true
                    });
                    jQuery("#posters-header-lArrow").removeClass("disable-lArrow");
                    jQuery("#posters-header-lArrow").addClass("enable-lArrow");
                    jQuery("#posters-header-lArrow").click(function(){
                        myPosters.stopSliding();
                        myPosters.prevItem();
                    });
                    jQuery("#posters-header-rArrow").removeClass("disable-rArrow");
                    jQuery("#posters-header-rArrow").addClass("enable-rArrow");
                    jQuery("#posters-header-rArrow").click(function(){
                        myPosters.stopSliding();
                        myPosters.nextItem();
                    });
                    jQuery("#posters-header-show").removeClass("disable-carousel");
					jQuery("#posters-header-show").addClass("enable-carousel");
					jQuery("#posters-header-show").click(function(){
                        myPosters.carouselContainer.clearTimer();
                        myPosters.toggleCarousel();
                    });
                    jQuery(".posters-header-iterator").each(function(){
                        jQuery(this).click(function(){
                            myId = this.getAttribute("id");
                            myPosters.goTo(new Number(myId.substring(myId.lastIndexOf('-')+1, myId.length)));
                        });
                        
                    })
                })
            })
        }
    }) //end load
}

function loadMain(){
    loadMapsBox();
	loadIdeas();
}

function readyMain(){
    initSearcher();
	initTopOffers();
    jQuery("#media-tabs").tabs({
    	'show': function(event,ui){
    		if (ui.panel.id == "media-tab-sylwester"){
    			if (jQuery("#media-box-sylwester").text()!='') {
    				loadSylwesterPlakat();
    			}
    			jQuery("a[href='#media-tab-sylwester']").parent().css({"background-color" : "#57A7F7", "background-image" : "url(/inc/css/myTheme/images/zakl_blue_akt.gif)"});
    			jQuery("a[href='#media-tab-sylwester']").parent().unbind("hover");
    			jQuery("a[href='#media-tab-sylwester']").parent().hover(function(){
    				jQuery(this).css("background-image" , "url(/inc/css/myTheme/images/zakl_blue_akt.gif)");
    			},function(){
    				jQuery(this).css("background-image" , "url(/inc/css/myTheme/images/zakl_blue_akt.gif)");
    				
    			});
    		}else{
    			jQuery("a[href='#media-tab-sylwester']").parent().css("background-color" , "#ffdf3c");
    			jQuery("a[href='#media-tab-sylwester']").parent().css("background-image" , "url(/inc/css/myTheme/images/zakl_yellow_nakt.png)");
    			jQuery("a[href='#media-tab-sylwester']").parent().unbind("hover");
    			jQuery("a[href='#media-tab-sylwester']").parent().hover(function(){
    				jQuery(this).css("background-image" , "url(/inc/css/myTheme/images/zakl_yellow_hover.png)");
    			},function(){
    				jQuery(this).css("background-image" , "url(/inc/css/myTheme/images/zakl_yellow_nakt.png)");
    			});
    		}
    	}
    });
    
	jQuery("#maps-tabs").tabs();
	
	jQuery("#media-tabs ul.ui-tabs-nav li, #maps-tabs ul.ui-tabs-nav li").each(function(index){
		jQuery(document.createElement('li')).css({ width:"1px", "background-color":"#ffffff", height:"22px" }).insertAfter(this);
	});
	jQuery("#media-tabs ul.ui-tabs-nav li:first a, #maps-tabs ul.ui-tabs-nav li:first a").addClass("corner-top-left");
	initIdeas();
}

function loadSylwesterPlakat(){
	var params = {
			quality: "high",
			allowscriptaccess: "always",
			wmode: "transparent"
		};
		swfobject.embedSWF("http://www.travelone.pl/flash/sylwester/" + jQuery("#media-box-sylwester").html() + "_pla.swf","media-box-sylwester", "372", "272", "9.0.0", "", "", params, "");
}
