var mapa;
var afterLoadSetCenter=false;
var obiektLat=1
var obiektLng=1;
var t;

window.onload = function() {
  if (navigator.product=="Gecko" && navigator.userAgent.indexOf("rv:1.7")>=-1) { // blad w Gecko 1.7
    var s=document.body.style;
    s.display="none";
    s.display="";
  }
    
    if(GBrowserIsCompatible()){	
	mapa = new GMap2(document.getElementById("naglowek_mapa"));
	mapa.addControl(new GSmallMapControl());
	mapa.setCenter(new GLatLng(52.23,21.00),10);
	if(afterLoadSetCenter){
	    mapa.setCenter(afterLoadSetCenter,15);
	}
//	mapa.enableScrollWheelZoom();
//	mapa.addControl(new GOverviewMapControl());
    }				
    
    $("#left_szukaj_nav > div").click(function(){
	n = $(this).attr("name");
	$("div[class=left_szukaj_box]").hide();
	$("#left_szukaj_"+n).show();
	$("#left_szukaj_nav > div").css("background-color","#90D5A9");
	$(this).css("background-color","#19723A");
    });    

    imagePreview();	
    
    $.getJSON("getLocation.php",
	function(data){
	    $.each(data, function(i,item){	
		addMarker(item[0], item[1], item[2], item[3], item[4], item[5]);
	    });
	});

    $("a[rel=ggg]").lightBox({
	txtImage: "Obraz",
	txtOf: "z",
	containerResizeSpeed:100
    });

    $("a[rel=top]").lightBox({
	txtImage: "Obraz",
	txtOf: "z",
	containerResizeSpeed:100
    });

    $("img[name=dodaj_opinie]").mouseover(function(){$(this).attr("src","img/dodaj_opinie_hover.gif");});
    $("img[name=dodaj_opinie]").mouseout(function(){$(this).attr("src","img/dodaj_opinie.gif");});
    
    $("input[name=zalety_dodaj]").click(function(){dodajInneAction("zalety");});
    $("input[name=wady_dodaj]").click(function(){dodajInneAction("wady");});
    
    $("input[name=zalety]").blur(function(){this.focused=false;}).focus(function(){this.focused=true;}).autocomplete("lib/turystyka/ajax/opinia_autocomplete.php", {extraParams:{typ: "zalety"}});
    $("input[name=wady]").blur(function(){this.focused=false;}).focus(function(){this.focused=true;}).autocomplete("lib/turystyka/ajax/opinia_autocomplete.php", {extraParams:{typ: "wady"}});

    $("input[name=ankieta_pole_2]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    $("input[name=ankieta_pole_3]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    $("input[name=ankieta_pole_4]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    $("input[name=ankieta_pole_5]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    $("input[name=ankieta_pole_6]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    $("input[name=ankieta_pole_7]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    $("input[name=ankieta_pole_8]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    $("input[name=ankieta_pole_9]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
    
    $("input[class=date_pick]").datePicker();
    
    $("input[name=login]").blur(function(){
	$("#loginCheckResult").html("...");
	v = $(this).val();
	$.post("lib/turystyka/ajax/login_check.php", {login: v}, loginCheck);
    });
    
    $("img[rel=helptooltip]").hover(function(){
	t = $(this).attr("title");
	$(this).attr("title","");
	o = $(this).parent();
	extra = "";
	if($(this).attr("id")=="lolopopo")
	    extra = "height:70px;";
	     
	o.append("<div style='position:absolute; "+extra+"' class='tooltipWrapper'>"+t+"</div>");
    }).mouseout(function(){
	$("div[class=tooltipWrapper]").remove();
	$(this).attr("title",t);
    });
    
    $("td[rel=t-button]").click(function(){
	$("div[class=tabki-srodek]").hide();
	$("td[rel=t-button]").css("color","#fff");
	c = $(this).attr("class");
	if(c=="op1"){$("#m-tabki-srodek-1").show(); $("#m-tabki-gora").css("background-image", "url(/img/tab-gora-1.jpg)"); $(this).css("color", "#333");}
	if(c=="op2"){$("#m-tabki-srodek-2").show(); $("#m-tabki-gora").css("background-image", "url(/img/tab-gora-2.jpg)"); $(this).css("color", "#333");}
	if(c=="op3"){$("#m-tabki-srodek-3").show(); $("#m-tabki-gora").css("background-image", "url(/img/tab-gora-3.jpg)"); $(this).css("color", "#333");}
    }).hover(function(){
	t = $(this).attr("title");
	$(this).attr("title","");
	o = $(this).parent();
	o = $("#miejsce-na-tooltipa");
	o = $(this);
	extra="margin:10px 0 0 40px;";
	o.append("<div style='position:absolute; "+extra+"' class='tooltipWrapper'>"+t+"</div>");
    }).mouseout(function(){
	$("div[class=tooltipWrapper]").remove();
	$(this).attr("title",t);
    });;
    
    $("img[class=flaga]").hover(function(){s = $(this).attr("src");x = s.replace("grey","color");$(this).attr("src", x);});
    $("img[class=flaga]").mouseout(function(){s = $(this).attr("src");x = s.replace("color","grey");$(this).attr("src", x);});
    $("img[name=login_button]").hover(function(){$(this).attr("src", "img/login_b_hover.gif");});
    $("img[name=login_button]").mouseout(function(){$(this).attr("src", "img/login_b.gif");});
    $("input[name=form_user]").val("- login -").focus(function(){$(this).val("").unbind("focus");});
    
    if(navigator.appName!="Microsoft Internet Explorer"){
	document.flogowanie.form_pass.type="text";
	$("input[name=form_pass]").val("- hasło -").focus(function(){$(this).val("").unbind("focus");this.type="password";});
    }
    else{
	//$("input[name=form_pass]").val("- hasło -").focus(function(){$(this).val("").unbind("focus");$(this).attr("type","password");});
    }


    $("marquee").mouseover(function(){$(this).attr("scrollamount","0");});
    $("marquee").mouseout(function(){$(this).attr("scrollamount","5");});
    
    $("img[name=scroll_right]").hover(function(){$("marquee").attr("direction","right");});
    $("img[name=scroll_right]").mouseout(function(){$("marquee").attr("direction","left");});
    $("img[name=scroll_left]").hover(function(){$("marquee").attr("scrollamount","20");});
    $("img[name=scroll_left]").mouseout(function(){$("marquee").attr("scrollamount","5");});

    $("td[name=zbior_pikto] > img").click(function(){
	i = $(this).attr("foro");
	c = $("#"+i).attr("checked");
	if(c=="true" || c==true || c=="checked"){
	    $("#"+i).attr("checked","");
	    $(this).attr("src", $(this).attr("src").replace("_over",""));
	}
	if(c=="false" || c==false){
	    $("#"+i).attr("checked","true");
	    p = $(this).attr("src").split(".");
	    if(p.length>2)u = p[2];
	    else u = p[0];
	    $(this).attr("src", u+"_over.png");
	}
    });
}

function loginCheck(res){
    r = eval(res);
    
    $("#loginCheckResult").html(r[1]);
    
    if(r[0]==1){
	$("form[name=dodajObiekt]").unbind("submit").submit(function(){return false;});
    }
    else{
	$("form[name=dodajObiekt]").unbind("submit").submit(function(){return true;});
    }
}

function dodajObiektCheck(f){
    $("#komunikat_err_pass").hide();
    $("#komunikat_err_pola").hide();
    x = f.length;
    toRet = true;
    for(i=0; i<x; i++){
	if((f[i].value==0 || f[i].value=="") && f[i].name!="dzielnica"){
	    f[i].style.backgroundColor="#fcc";
	    toRet = false;;
	    $("#komunikat_err_pola").show();
	}
	else{
	    f[i].style.backgroundColor="";
	}
    }
    if(f.haslo.value != f.haslo2.value){
	f.haslo.style.backgroundColor="#fcc";
	f.haslo2.style.backgroundColor="#fcc";
	$("#komunikat_err_pass").show();
	toRet = false;
    }
    reg = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    
    xx = "email";
    
    if(!reg.test($("input[name="+xx+"]").val())){
	toRet = false;
	f.email.style.backgroundColor="#fcc";
	$("#komunikat_err_pola").show();
    }

    return toRet;	
}	

$.fn.radioToStars = function(opt){
    w = new Array();
    obj = this;
    $.each(obj, function(i,d){w[i]=d.value})

    var x = $(this).parent();
    $(this).css("display","none");

    for(i=0;i<w.length;i++){st = "<img src='"+opt.star+"' name='"+obj.attr("name")+"' title='"+w[i]+"'/>"; x.append(st)}

    ob = $("img[name="+$(this).attr("name")+"]");

    ob.attr("rel",0)
    .css("cursor","pointer")
    .click(function(){
	$("input[name="+$(this).attr("name")+"][value="+$(this).attr("title")+"]").attr("checked","checked");
	$("img[name="+$(this).attr("name")+"]").attr("rel",0);
	$("img[name="+$(this).attr('name')+"]:lt("+$(this).attr('title')+")").attr("src", opt.star_hover).attr("rel",1);
    })
    .hover(function(){
        $("img[name="+$(this).attr('name')+"]:lt("+$(this).attr('title')+")").attr("src", opt.star_hover);
    }).mouseout(function(){
	$("img[name="+$(this).attr('name')+"][rel=0]").attr("src" , opt.star);
    });
}

function dodajInneAction(co){
    x = $("input[name="+co+"]").val(); 
    inneDodaj(co, x, $("input[name=id]").val(),  $("input[name=session]").val());
}

function addMarker(lat,lon,name,iconname,url,foto){
    var ikona = new GIcon();
//    if(lat == obiektLat && lon == obiektLng){
//	ikona.image = "img/icons/hotel.gif";
//	ikona.iconSize = new GSize(35, 35);
//    }
//    else{
	ikona.image = iconname;
	ikona.iconSize = new GSize(45, 32);
//    }
    ikona.iconAnchor = new GPoint(6, 6);
    var marker = new GMarker(new GLatLng(lat,lon),{icon: ikona});
    marker.tooltip = '<div class="mapTooltip"><b>'+name+'</b><br/><img src="'+foto+'" /><br/><a href="'+url+'">więcej</a></div>';
    marker.nazwa = name;
    mapa.addOverlay(marker);

    GEvent.addListener(marker,"mouseover", function(e) {
	pos = mapa.fromLatLngToContainerPixel(e);
	off = $("#naglowek_mapa").offset();
	$("<div id='mapa_info_in'>").html(marker.nazwa).css("top",pos.y+off.top-25).css("left",pos.x+off.left+5).appendTo("body");
    });

    GEvent.addListener(marker,"mouseout", function(e) {
	$("#mapa_info_in").remove();
    });
						    	    	    			
    GEvent.addListener(marker,"click", function(e) {
	mapa.openInfoWindow(marker.getLatLng(), marker.tooltip,{suppressMapPan:true, onCloseFn:function(){mapa.panTo(marker.getLatLng()); }});	
	np = mapAddPx(marker.getLatLng(), 0, -160);
//	mapa.panTo(np);
	mapa.setCenter(np);
    });
						    	    	    			
    return marker;
}

function mapMovePx(x,y){
    mapa.panBy(new GSize(x,y));
}

function mapAddPx(gll, x, y){
    mapa.setCenter(gll);
    mp = mapa.fromLatLngToContainerPixel(gll);
    ny = mp.y + y;
    nx = mp.x + x;

    res = mapa.fromContainerPixelToLatLng(new GPoint(nx, ny));
    
    return res;
}
						    	    	    			    
function wynik_td_zmiana(id, co){
    $("#wynik_"+id+" > span").hide();
    $("#wynik_"+id+" > span[name="+co+"]").show();
    $("#wynik2_"+id+" > div[name=but_opis]").toggle();
    $("#wynik2_"+id+" > div[name=but_info]").toggle();
}

this.imagePreview = function(){	
    xOffset = -30;
    yOffset = -30;
				    
    $("img.preview").hover(function(e){
        this.t = this.title;
        this.title = "";	
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='"+ this.src +"' alt='Image preview' /></p>");
        $("#preview").css("position","absolute").css("top",(e.pageY - yOffset) + "px").css("left",(e.pageX + xOffset) + "px").fadeIn("fast");
    },
    
    function(){
	this.title = this.t;	
	$("#preview").remove();
    });	
    
    $("img.preview").mousemove(function(e){
	$("#preview").css("top",(e.pageY - yOffset) + "px").css("left",(e.pageX + xOffset) + "px");
    });			
    
};

function inneDodaj(co, wart, id, session){
    $.post("lib/turystyka/ajax/opinia_inne.php", {typ: co, tresc:wart, of: id, sess: session}, ladujInne);
}

function ladujInne(res){	
    $.post("lib/turystyka/ajax/opinia_inne_load.php", {sess: res}, function(x){
	x = eval("["+x+"]");
	x = x[0];
	$("#zalety_div").html(x.zalety);
	$("#wady_div").html(x.wady);
    });
    
    $("input[name=zalety]").val("");
    $("input[name=wady]").val("");
}

function form_valid(f){
    if(document.getElementById("zalety_i").focused){
	dodajInneAction("zalety");
	return false;
    }
    if(document.getElementById("wady_i").focused){
	dodajInneAction("wady");
	return false;
    }
    
    l = f.length.value;
    p = f.prefix.value;
    idis = f.idis.value;
    idis = idis.split(";");
    err = false;
    for(i=0; i<idis.length-1; i++){
	$("input[name="+p+"_"+idis[i]+"]").css("background-color","#fff").css("border", "1px solid #000");
	$("[rel="+p+"_"+idis[i]+"]").css("color", "#000");
	
	v1 = $("[name="+p+"_"+idis[i]+"][type=radio]:checked").val();
	v2 = $("[name="+p+"_"+idis[i]+"][type!=radio]").val();
	
	if((!v1 && !v2) && v2==""){
	    $("input[name="+p+"_"+idis[i]+"]").css("background-color","#fcc").css("border", "1px solid red");
	    $("[rel="+p+"_"+idis[i]+"]").css("color", "red");
	    err = true;
	}
    }
    
    
    var reg = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    
    if(f.name=="dodaj_opinie") 		xx = "ankieta_pole_14";
    if(f.name=="dodaj_rezerwacje") 	xx = "ankieta_pole_22";
    if(f.name=="dodaj_pytanie") 	xx = "ankieta_pole_25";
    
    if(!reg.test($("input[name="+xx+"]").val())){
	$("[name="+xx+"]").css("background-color","#fcc").css("border", "1px solid red");
	$("[rel="+xx+"]").css("color", "red");
	err=true;
    }
    
    if(err){
	$("#error_info1").show();
	return false;
    }
    else
	return true;
}

function glosuj(id){
    $.post("lib/turystyka/ajax/opinia_glosuj.php", {opinia: id}, function(res){
	r = eval(res);
	alert(r[0]);
	$("#opinia_score").unbind("click").html(r[1]);
    });
}


