var $ = jQuery.noConflict();

$(document).ready(function() {
	if($('#slider').length  >0 ) {
		$('#slider').html($('#slider_more').html())
		if($('#slider a').length > 1) {
			$('#prevBtn').show();
			$('#nextBtn').show();
		}
		
		$('#slider').cycle({ 
		    fx:     'scrollLeft', 
		    timeout: 8000, 
		    delay:  -2000,
		    next:   '#prevBtn', 
		    prev:   '#nextBtn',
		    pause:   1 
		});
	}
	var new_map='<span class="map"><span class="s1" /><span class="s2" /><span class="s3" /><span class="s4" /><span class="s5" /><span class="s6" /><span class="s7" /><span class="s8" /><span class="s9" /><span class="s10" /><span class="s11" /></span>';
	var new_bg=$("<span>");
	new_bg.addClass("bg");
	$("#polska li a").append(new_map);
	$("#polska li a").append(new_bg);
	$('#filtr_wojewodztwo, #filtr_miasto, #filtr_wielkosc, #filtr_polozenie, #filtr_widok').sSelect({ddMaxHeight: '300px'}).change(function() {
		$("#filtry_form").submit();
	});
	$('#telebim_ceny_okresy, #telebim_ceny_ilosci').sSelect({ddMaxHeight: '300px'}).change(function() {
		set_kalkulacja();
	});	
	
	if($("div.detail_thumb").length) {
		$("#panorama_thumb a#panorama_demo").colorbox({
			innerWidth:"450px", 
			innerHeight:"170px", 
			iframe:true
		});
		$("div.detail_thumb a").click(function() {
			self.noweIMG = new Image();
			var tmp_href = $(this).attr('href');
			var tmp_next_href = $(this).attr('rel');
			$("#telebim_photo_change a").hide();
			$("#telebim_photo_change img#loader").show();
			self.noweIMG.src = tmp_href;
			self.noweIMG.rel = tmp_next_href;
			czy_jest();

			return false;
		})
	}	else if($("a.panorama_thumb").length) {
		$("a.panorama_thumb").colorbox({
			innerWidth:"810px", 
			innerHeight:"610px", 
			iframe:true
		});

	}
	$("#telebim_photo_change a").colorbox();
	if($("a.telebim_remember").length && location.pathname.substr(0,6) != '/forum') {
		$("a.telebim_remember").click(function() {
			var tab_p_a = "2";
			var tab_p_b = "1";
			if($("#telebim_ceny_okresy").length > 0) {
				tab_p_a = $("#telebim_ceny_okresy").val();
			}
			if($("#telebim_ceny_ilosci").length > 0) {
				tab_p_b = $("#telebim_ceny_ilosci").val();
			}
			
			var old_cookie = $.cookie('plan_emisji');
			var act_id = $(this).attr('id').substr(11);
			var act_price = $('#telebim_cena_' + act_id).val();
			if (!act_price) {
				act_price = 0;
			}	else	{
				act_price = parseFloat(act_price);
			} 
			if(old_cookie) {
				var tmp_old = old_cookie.split('|');
				var tel_ile = 0;
				var tel_suma = 0;
				var flag_ok=1;
				var new_string = '';
				
				for(i=0;i<tmp_old.length;i++) {
					if(tmp_old[i]) { 
						var tmp_tel = tmp_old[i].split("_"); 
						tel_suma += parseFloat(tmp_tel[0]);
						if(tmp_tel[1] == act_id) {
							flag_ok = 0;
						}
						new_string += '|' + tmp_tel[0] + '_' + tmp_tel[1] + '_' + tmp_tel[2] + '_' + tmp_tel[3];
						tel_ile ++;
					} 
				}
				
				if(flag_ok) {
					tel_suma += act_price;
					tel_ile ++;
					new_string += '|' + act_price + '_' + act_id + '_' + tab_p_a + '_' + tab_p_b;
					new_string = new_string.substr(1);
					$.cookie('plan_emisji',new_string);
					$("#plan_suma").html(tel_suma +',-');
					$("#plan_ile").html(tel_ile);
					
				}
				
			}	else	{
				
				$.cookie('plan_emisji',act_price + '_' + act_id + '_' + tab_p_a + '_' + tab_p_b);
				$("#plan_suma").html(act_price+',-');
				$("#plan_tresc").html("<a href='/plan-emisji'>lista dodanych&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>");
				$("#plan_ile").html("1");
			}
			return false;
		})
	}
	if($("a.telebim_delete").length && location.pathname.substr(0,6) != '/forum') {
		$("a.telebim_delete").click(function() {
			var act_id = $(this).attr('id').substr(11);
			var old_cookie = $.cookie('plan_emisji');
			if(old_cookie) {
				var tmp_old = old_cookie.split('|');
				var tel_ile = 0;
				var tel_suma = 0;
				var new_string = '';
				for(i=0;i<tmp_old.length;i++) {
					if(tmp_old[i]) { 
						var tmp_tel = tmp_old[i].split("_");
						if (tmp_tel[1] != act_id) {
							tel_suma += parseFloat(tmp_tel[0]);
							new_string += '|' + tmp_tel[0] + '_' + tmp_tel[1] + '_' + tmp_tel[2] + '_' + tmp_tel[3];
							tel_ile++;
						}
					} 
				}
				if(new_string) {
					new_string = new_string.substr(1);
					$.cookie('plan_emisji',new_string);
					$("#plan_suma").html(tel_suma +',-');
					$("#plan_ile").html(tel_ile);
					$("#ile_poz").html(tel_ile);
					$("#post_" + act_id).fadeOut('slow', function() {
					    $("#post_" + act_id).remove();
						$("#sep_" + act_id).remove();
					});
					
				}	else	{
					if(tmp_old) {
						$("#plan_suma").html(tel_suma +',-');
						$("#plan_ile").html(tel_ile);
						$("#ile_poz").html(tel_ile);
						$("#post_" + act_id).fadeOut('slow', function() {
						    $("#post_" + act_id).remove();
							$("#sep_" + act_id).remove();
							$("#register_error").show();3
						});
					}
					$.cookie('plan_emisji','');
				}
			}
			return false;
		})
	}	
	if($(".telebim_map").length > 0) {
		$(".telebim_map").each(function() {
			load_map($(this).attr('id'));
			var xy = $(this).attr('id').split('x');
			showAddress( xy[1] + ',' + xy[0] );
		})
	}
	$("#kalkulacja_zamow, #zlecenie_zapytaj, #zlecenie_zarezerwuj, #zlecenie_zglos, #zlecenie_skontaktuj").colorbox({
		width:"50%", 
		height:"50%", 
		iframe:true
	});
	$("a.expand").colorbox();
	$("a.telafriend").colorbox({
		width:"80%", 
		height:"80%", 
		iframe:true
	});
	if($("#gielda_kontakt").length) {
		$("#gielda_kontakt input").each(function() {
			if($(this).attr('type')=='text') {
				$(this).click(function() {
					if($(this).val() == $(this).attr('defaultValue')) {
						$(this).val('');
					}
				})
				$(this).blur(function() {
					if($(this).val() == '') {
						$(this).val($(this).attr('defaultValue'));
					}
				})
			}
		})
	}
	if($("#wp-email-content input").length) {
		$("#wp-email-content input").each(function() {
			if($(this).attr('type')=='text') {
				$(this).click(function() {
					//alert($(this).attr('defaultValue'));
					if($(this).val() == $(this).attr('defaultValue')) {
						$(this).val('');
					}
				})
				$(this).blur(function() {
					//alert($(this).attr('defaultValue'));
					if($(this).val() == '') {
						$(this).val($(this).attr('defaultValue'));
					}
				})
			}
		})
	}
	$("#wp-email-content textarea").each(function() {
		$(this).click(function() {
			//alert($(this).attr('defaultValue'));
			if($(this).val() == $(this).attr('defaultValue')) {
				$(this).val('');
			}
		})
		$(this).blur(function() {
			//alert($(this).attr('defaultValue'));
			if($(this).val() == '') {
				$(this).val($(this).attr('defaultValue'));
			}
		})
	})
	if($("#prev_trend").length > 0) {
		$("#prev_trend").click(function() {
			var l_tr = $("#trendy_grupy").text();
			var next_grupa_cenowa='';
			var prev_grupa_cenowa='';
			var next_label='';
			var prev_label='';
			switch(l_tr) {
				case 't/100' : prev_grupa_cenowa=6;prev_label='r/200';break;
				case 't/200' : prev_grupa_cenowa=1;prev_label='t/100';break;
				case 'm/100' : prev_grupa_cenowa=2;prev_label='t/200';break;
				case 'm/200' : prev_grupa_cenowa=3;prev_label='m/100';break;
				case 'r/100' : prev_grupa_cenowa=4;prev_label='m/200';break;
				case 'r/200' : prev_grupa_cenowa=5;prev_label='r/100';break;
			}
			$("#trendy_grupy").text(prev_label);
			$("#trendy_main").load('/trendy.php?grupa_cenowa='+prev_grupa_cenowa);
		})
		$("#next_trend").click(function() {
			var l_tr = $("#trendy_grupy").text();
			var next_grupa_cenowa='';
			var prev_grupa_cenowa='';
			var next_label='';
			var prev_label='';
			switch(l_tr) {
				case 't/100' : next_grupa_cenowa=2;next_label='t/200';break;
				case 't/200' : next_grupa_cenowa=3;next_label='m/100';break;
				case 'm/100' : next_grupa_cenowa=4;next_label='m/200';break;
				case 'm/200' : next_grupa_cenowa=5;next_label='r/100';break;
				case 'r/100' : next_grupa_cenowa=6;next_label='r/200';break;
				case 'r/200' : next_grupa_cenowa=1;next_label='t/100';break;
			}
			$("#trendy_grupy").text(next_label);
			$("#trendy_main").load('/trendy.php?grupa_cenowa='+next_grupa_cenowa);
		})
	}
	var gielda_front_cache = new Array();
	if($("#gielda_front_menu_ul").length > 0) {
		if($("#gielda_front_menu_ul #cm_1").hasClass('current')) {
			gielda_front_cache[1] = $("#gielda_mod_right").html();
		}
		if($("#gielda_front_menu_ul #cm_2").hasClass('current')) {
			gielda_front_cache[2] = $("#gielda_mod_right").html();
		}
		if($("#gielda_front_menu_ul #cm_3").hasClass('current')) {
			gielda_front_cache[3] = $("#gielda_mod_right").html();
		}
		if($("#gielda_front_menu_ul #m_1").attr('href') =='#') {
			$("#gielda_front_menu_ul #m_1").css('cursor','default');
		}
		$("#gielda_front_menu_ul #m_1").click(function() {
			if($(this).attr('href') != '#') {
				if(!gielda_front_cache[1]) {
					$("#gielda_mod_right").load('/gielda_front.php?tab_zr=143',function() {
						gielda_front_cache[1] = $("#gielda_mod_right").html();
					});
				}	else	{
					$("#gielda_mod_right").html(gielda_front_cache[1]);
				}			
				$("#gielda_front_menu_ul li").removeClass('current');
				$("#gielda_front_menu_ul #cm_1").addClass('current');
			}	
			return false;
		})
		if($("#gielda_front_menu_ul #m_2").attr('href') =='#') {
			$("#gielda_front_menu_ul #m_2").css('cursor','default');
		}
		$("#gielda_front_menu_ul #m_2").click(function() {
			if($(this).attr('href') != '#') {
				if(!gielda_front_cache[2]) {
					$("#gielda_mod_right").load('/gielda_front.php?tab_zr=144',function() {
						gielda_front_cache[2] = $("#gielda_mod_right").html(); 
					});
				}	else	{
					$("#gielda_mod_right").html(gielda_front_cache[2]);
				}
				$("#gielda_front_menu_ul li").removeClass('current');
				$("#gielda_front_menu_ul #cm_2").addClass('current');
			}	
			return false;
		})
		if($("#gielda_front_menu_ul #m_3").attr('href') =='#') {
			$("#gielda_front_menu_ul #m_3").css('cursor','default');
		}
		$("#gielda_front_menu_ul #m_3").click(function() {
			if($(this).attr('href') != '#') {
				if(!gielda_front_cache[3]) {
					$("#gielda_mod_right").load('/gielda_front.php?tab_zr=145',function() {
						gielda_front_cache[3] = $("#gielda_mod_right").html(); 
					});
				}	else	{
					$("#gielda_mod_right").html(gielda_front_cache[3]);
				}
				$("#gielda_front_menu_ul li").removeClass('current');
				$("#gielda_front_menu_ul #cm_3").addClass('current');
			}	
			return false;
		})
	}
	if($('#counter_0').length) {
		setInterval("c_down('counter_0')",1000);
	}
	if($('#counter_1').length) {
		setInterval("c_down('counter_1')",1000);
	}
	if($('#counter_2').length) {
		setInterval("c_down('counter_2')",1000);
	}
	if($('#counter_3').length) {
		setInterval("c_down('counter_3')",1000);
	}
	if($('#counter_4').length) {
		setInterval("c_down('counter_4')",1000);
	}
	if(location.hash && location.hash == '#wycena_kampanii' && $('#temat_form').length > 0) {
		$('#temat_form').val('wycena kampanii reklamowej');
	}
	
	$('#temat_form').sSelect({ddMaxHeight: '300px'});
	if($("#contact_cont_form").length>0) {
		$("#contact_cont_form input").each(function() {
			if($(this).attr('type')=='text') {
				$(this).click(function() {
					//alert($(this).attr('defaultValue'));
					if($(this).val() == $(this).attr('defaultValue')) {
						$(this).val('');
					}
				})
				$(this).blur(function() {
					//alert($(this).attr('defaultValue'));
					if($(this).val() == '') {
						$(this).val($(this).attr('defaultValue'));
					}
				})
			}
		})
		$("#contact_cont_form textarea").each(function() {
			$(this).click(function() {
				//alert($(this).attr('defaultValue'));
				if($(this).val() == $(this).attr('defaultValue')) {
					$(this).val('');
				}
			})
			$(this).blur(function() {
				//alert($(this).attr('defaultValue'));
				if($(this).val() == '') {
					$(this).val($(this).attr('defaultValue'));
				}
			})
		})
	}
	$("#s_form_t").focus();
});
function c_down(src_id) {
	var tmp_d = $("#"+src_id).html();
	if(tmp_d) {
		tmp_d = tmp_d.split(":");
		var h = parseInt(tmp_d[0]);
		var m = parseInt(tmp_d[1]);
		var s = parseInt(tmp_d[2]);
		if(s > 0 ) {
			s = s-1;
		}	else	{
			
			if(m > 0) {
				m = m-1;
				s = 59;
			}	else	{
				if(h > 0) {
					h = h-1;
					m = 59;
					s = 59;
				}	else	{
					h = 0;
					m = 0;
					s = 0;
				}
			}
		}
		$("#"+src_id).html(h+':'+m+':'+s);
		
			
	}	else	{
		return false;
	}
	
};
function czy_jest() {
	if(self.noweIMG.complete) {
		$("#telebim_photo_change a").remove();
		$("#telebim_photo_change").prepend("<a href='" + self.noweIMG.rel +"'><img class='medium' src='" + self.noweIMG.src + "'/></a>");
		$("#telebim_photo_change a img.medium").attr('src',self.noweIMG.src);	
		//alert(self.noweIMG.rel);
		//$("#telebim_photo_change a").attr('href',self.noweIMG.rel);
		$("#telebim_photo_change img.loader").hide();
		$("#telebim_photo_change a").colorbox();
		return false;
	} else {
		setTimeout("czy_jest()",100);
	}
	
}

function set_kalkulacja() {
	var tmp_o = $("#telebim_ceny_okresy").val();
	var tmp_i = $("#telebim_ceny_ilosci").val();
	if(tab_p[tmp_o][tmp_i]) {
		$("#telebim_kalkulacja").text(tab_p[tmp_o][tmp_i] + ',-');
		$(".telebim_cena_remember").val(tab_p[tmp_o][tmp_i]);
		$(".telebim_remember").show()
	}	else	{
		$("#telebim_kalkulacja").text('brak danych');
		$(".telebim_cena_remember").val(0);
		$(".telebim_remember").hide();
	}

}

function init_mapa_popup() {
	$("#mapa_right img[title]").mouseover(function() {
		var tmp = parseInt($(this).css('left'));
		var tmp2 = parseInt((parseInt($("#demotip").css('width')))+38)/2;
		tmp = tmp - tmp2;
		$("#demotip").css('left',(tmp)+'px');
		var tmp = parseInt($(this).css('top'));
		var tmp2 = parseInt((parseInt($("#demotip").css('height')))+41);
		tmp = tmp - tmp2;
		$("#demotip").css('top',(tmp)+'px');
		
		$("#demotip").html($("#d"+$(this).attr('id')).html());
		$("#demotip a").mouseover(function(){
			self.tip_on = 1;
		});
		$("#demotip a").mouseout(function(){
			self.tip_on = 0;
		});
		$("#demotip").show();
	});
	$("#demotip").mouseout(function() {
		self.tip_on = 0;
		setTimeout("close_tip()",1);
	});
	$("#demotip").mouseover(function() {
		self.tip_on = 1;
	});
}
function close_tip() {
	if (!self.tip_on) {
		$("#demotip").hide();
	}	
}
var geodecoder = null;
var map = null;
var obszar = null;
var error=0;

function load_mapa(act_el) {
  if (GBrowserIsCompatible()) {
  	$("#mapa_right").hide();
	$("#mapa_right2").show();
	$("#schowaj_mapa").show();
	map = new GMap2(document.getElementById("mapa_right2"));
	map.setUIToDefault();   
	map.setCenter(new GLatLng(51.76728,18.085346), 11);
	geocoder = new GClientGeocoder();
	obszar = new GLatLngBounds();
	showAddress(act_el,act_el);
  }
}
function unload_mapa(){
	$("#mapa_right2").hide();
	$("#mapa_right").show();
	$("#schowaj_mapa").hide();
}
function showAddress(address,text) {
  geocoder.getLatLng(
    address, function(point) {
      if (!point) {
        error++;
      } else {
	map.setCenter(point, 11);
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() { 
		marker.openInfoWindowHtml(text);
	});
	map.addOverlay(marker);
	//	marker.openInfoWindowHtml(text);
   
	obszar.extend(point);
	var nowyZoom = map.getBoundsZoomLevel(obszar);  
	var nowyPunkt = obszar.getCenter();  
	map.setCenter(nowyPunkt,nowyZoom); 
      }
    }
  );
}



/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			if(!options.vertical) $("li", obj).css('float','left');
			
			if(options.controlsShow){
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
	
			$("a","#"+options.nextId).click(function(){		
				animate("next",true);
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			$("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			$("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});		
			
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$("a","#"+options.nextId).hide();
						$("a","#"+options.lastId).hide();
					} else {
						$("a","#"+options.nextId).show();
						$("a","#"+options.lastId).show();					
					};
					if(t==0){
						$("a","#"+options.prevId).hide();
						$("a","#"+options.firstId).hide();
					} else {
						$("a","#"+options.prevId).show();
						$("a","#"+options.firstId).show();
					};					
				};				
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);
//mapka google

var map = null;
var geocoder = null;

function load_map(obj_id) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById(obj_id + '_cont'));
map.setUIToDefault();   
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    geocoder = new GClientGeocoder();
  }
}


function showAddress(address) {
	geocoder = new GClientGeocoder();
  if (geocoder) {
  	geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
         el("nomap", '')
        } else {
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          var tmp_a = '';
		  if($('.telebim_street').length > 0) {
		  	tmp_a += ',' + $('.telebim_street').text();
		  }
		  if($('.post-title').length > 0) {
		  	tmp_a += ',' + $('.post-title').text();
		  }
		  if(tmp_a) {
		  	tmp_a = tmp_a.substr(1);
		  	marker.openInfoWindowHtml(tmp_a);
		  }
		  $("div.telebim_map img.loader").hide();
		  $("div.map_cont").css("visibility","visible");
        }
      }
    );
  }
}





