// jquery swfobject
(function(C){var E=window,D=document,A="",B="object";C.flashPlayerVersion=(function(){var H,F=null,M=false,L="ShockwaveFlash.ShockwaveFlash";if(!(H=navigator.plugins["Shockwave Flash"])){try{F=new ActiveXObject(L+".7")}catch(K){try{F=new ActiveXObject(L+".6");H=[6,0,21];F.AllowScriptAccess="always"}catch(J){if(H&&H[0]===6){M=true}}if(!M){try{F=new ActiveXObject(L)}catch(I){H="X 0,0,0"}}}if(!M&&F){try{H=F.GetVariable("$version")}catch(G){}}}else{H=H.description}H=H.match(/^[A-Za-z\s]*?(\d+)(\.|,)(\d+)(\s+r|,)(\d+)/);return[H[1]*1,H[3]*1,H[5]*1]}());C.flashExpressInstaller="expressInstall.swf";C.hasFlashPlayer=(C.flashPlayerVersion[0]!==0);C.hasFlashPlayerVersion=function(G){var F=C.flashPlayerVersion;G=(/string|integer/.test(typeof G))?G.toString().split("."):G;G=[G.major||G[0]||F[0],G.minor||G[1]||F[1],G.release||G[2]||F[2]];return(C.hasFlashPlayer&&(G[0]>F[0]||(G[0]===F[0]&&(G[1]>F[1]||(G[1]===F[1]&&G[2]>=F[2])))))};C.flash=function(Q){if(!C.hasFlashPlayer){return false}var G=Q.swf||A,O=Q.params||{},I=D.createElement("body"),F,P,L,H,N,M,K,J;Q.height=Q.height||180;Q.width=Q.width||320;if(Q.hasVersion&&!C.hasFlashPlayerVersion(Q.hasVersion)){C.extend(Q,{id:"SWFObjectExprInst",height:Math.max(Q.height,137),width:Math.max(Q.width,214)});G=Q.expressInstaller||C.flashExpressInstaller;O={flashvars:{MMredirectURL:E.location.href,MMplayerType:(C.browser.msie&&C.browser.win)?"ActiveX":"PlugIn",MMdoctitle:D.title.slice(0,47)+" - Flash Player Installation"}}}if(typeof O===B){if(Q.flashvars){O.flashvars=Q.flashvars}if(Q.wmode){O.wmode=Q.wmode}}for(N in (M=["swf","expressInstall","hasVersion","params","flashvars","wmode"])){delete Q[M[N]]}F=[];for(N in Q){if(typeof Q[N]===B){P=[];for(M in Q[N]){P.push(M.replace(/([A-Z])/,"-$1").toLowerCase()+":"+Q[N][M]+";")}Q[N]=P.join(A)}F.push(N+'="'+Q[N]+'"')}Q=F.join(" ");if(typeof O===B){F=[];for(N in O){if(typeof O[N]===B){P=[];for(M in O[N]){if(typeof O[N][M]===B){L=[];for(K in O[N][M]){if(typeof O[N][M][K]===B){H=[];for(J in O[N][M][K]){H.push([J.replace(/([A-Z])/,"-$1").toLowerCase(),":",O[N][M][K][J],";"].join(A))}O[N][M][K]=H.join(A)}L.push([K,"{",O[N][M][K],"}"].join(A))}O[N][M]=L.join(A)}P.push([M,"=",E.escape(E.escape(O[N][M]))].join(A))}O[N]=P.join("&amp;")}F.push(['<PARAM NAME="',N,'" VALUE="',O[N],'">'].join(A))}O=F.join(A)}if(!(/style=/.test(Q))){Q+=' style="vertical-align:text-top;"'}if(!(/style=(.*?)vertical-align/.test(Q))){Q=Q.replace(/style="/,'style="vertical-align:text-top;')}if(C.browser.msie){Q+=' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';O='<PARAM NAME="movie" VALUE="'+G+'">'+O}else{Q+=' type="application/x-shockwave-flash" data="'+G+'"'}I.innerHTML=["<OBJECT ",Q,">",O,"</OBJECT>"].join(A);return C(I.firstChild)};C.fn.flash=function(G){if(!C.hasFlashPlayer){return this}var F=0,H;while((H=this.eq(F++))[0]){H.html(C.flash(C.extend({},G)));if(document.getElementById("SWFObjectExprInst")){F=this.length}}return this}}(jQuery));


// mapa polski
/*
Polska, klikalna mapa województw - 460px | http://winstonwolf.pl/css,polska.html
script version: 2.3 by Winston Wolf | http://winstonwolf.pl
Copyright (C) 2009 - 2011 Winston_Wolf | All rights reserved
*/

function mapPl (){
  $('#map').prepend('<span id="loader">Trwa ładowanie mapy ...</span>').addClass('script'); $('#mapa').find('li').hide();
  var mapUrl=$('#pl').css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "");
  var mapImg=new Image();
  $(mapImg).load(function(){$('#loader').fadeOut(); $('#map').find('li').fadeIn(); $('#pl').find('a').prepend('<span class="map" />');
   for(var i=1;i<17;i++){ $('#pl .map').append('<span class="s'+i+'" />'); }
   if($('#map').hasClass('tooltip')){$('#pl').find('li').each(function(){ var tooltipName=$(this).children('a').text(); var tooltipLeft=$('.tt').outerWidth()/-2; var tooltipTop=$('.tt').outerHeight()/-2; $(this).append('<span class="tt">'+tooltipName+'</span>'); $('.tt').css({'display':'none','margin-left':tooltipLeft,'margin-top':tooltipTop});});$('#pl li').hover(function(){$(this).children(".tt:visible").hide(); $(this).children(".tt").show(); $(this).children("a").removeAttr('title');},function(){$(this).children(".tt").hide();});}
   }).error(function(){$('#loader').text('Brak mapy!'); $('#pl span').hide(); $('#map,#pl').css({'height':'auto','left':'0','margin':'0 auto'});
    }).attr('src',mapUrl);
  var loaderPos=$('#loader').outerWidth()/-2; $('#loader').css('margin-left',loaderPos);
  $('#pl').find('li').click(function(){window.location.href=$(this).children('a').attr('href');});
}


// otwierania linku o klasie openNew w nowym oknie
function newWindow () {
	$(document).find('a[rel="openNew"]').click(function(){
		window.open(this.href);
		return false;
	})
}


// osadzanie flasha
function homeSwf() { 
	var href = String(document.location.href);
	if (href.match("/en/")) {
		selectedLanguage = "en";
	}
	else if (href.match("/de/")) {
		selectedLanguage = "de";
	}
	else if (href.match("/ru/")) {
		selectedLanguage = "ru";
	}
	else {
		selectedLanguage = "pl";
	}

$('#homeFlash').flash ({
		swf: '/swf/aa_intensive_intro.swf',
		width: 994,
		height: 376,
		wmode: 'opaque',
		flashvars: {
			config: '/xml/config-'+selectedLanguage+'.xml'
		}
	});
}

function slideProductTab() {
	$('#fullDescription div.sectionTab').find('h4').click( function () {
			/*$(this).parent().parent().find('div.sectionTab').slideUp('300', function() {
				$(this).find('ul').hide();
				$(this).find('p').hide();
			});*/
		if($(this).parent().find('ul').length) {
			$(this).parent().parent().find('div.sectionTab').find('p').parent().animate({"height": 16}, 200,function(){
				$(this).find('p').hide();
			});
			
			descrHeight=$(this).parent().find('ul').height();
			newHeight=descrHeight+16;
			$(this).parent().stop(true, true).animate({"height": newHeight}, 300,function(){
				$(this).find('ul').fadeIn('slow');
			});
		}
		if($(this).parent().find('p').length) {
			$(this).parent().parent().find('div.sectionTab').find('ul').parent().animate({"height": 16}, 200,function(){
				$(this).find('ul').hide();
			});
			
			descrHeight=$(this).parent().find('p').height();
			newHeight=descrHeight+16;
			$(this).parent().stop(true, true).animate({"height": newHeight}, 300,function(){
				$(this).find('p').fadeIn('slow');
			});
		}
		return false;	
	});
}

function newsletterAction() {

	$('#newsletter input[name=email]').click( function () {
		if($(this).val()=="email@domena.pl") {
			$(this).val('');
             return false;
		}
	});
	 $('#newsletter').find('button').click(function() {
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		var email=$('#newsletter input[name=email]').val();
		if (!emailPattern.test(email)) {
				if($('#newsletter small.error').length) {
					$('#newsletter small.error').html('Podaj poprawny adres e-mail.');
				} else {
					$('#newsletter').append('<small class="error">Podaj poprawny adres e-mail.</small>');
				}
				$('#newsletter input[name=email]').val('');
				return false;
		}
	});

}
function distributors() {
	/*
	$('#province').parent().find('a.[href=#province]').click(function () {
		$('#province').show();
		return false;
	});
	$('#distributors thead ul.sorting li').removeClass('selected');
	$('#province li').click(function() {
		$('#distributors thead ul.sorting li').removeClass('selected');
		var woj=$(this).html();
		var order='city asc';		
		$('#province').parent().find('a[href=#province]').html(woj);
		distributors_ajax(woj,order);	
		$('#distributors thead th:nth-child(3) ul.sorting li:first-child').addClass('selected');
		$('#province').hide();
		return false;
	});
	*/
	if ($('#distributors').length) {
		$('#distributors thead ul.sorting li').removeClass('selected');
		var woj='';
		var order='woj asc,city asc';	
		$('#distributors thead th:nth-child(3) ul.sorting li:first-child').addClass('selected');
	} else {
		$('#province').parent().find('a.[href=#province]').click(function () {
			$('#province').show();
			return false;
		});
		$('#province li').click(function() {			
			if ($('#province li:first-child').html()!='Wszystkie') {
				$('#province').prepend('<li>Wszystkie</li>');
				$('#province li:first-child').click(function() {
					var city='';
					var order='city asc';		
					$('#province').parent().find('a[href=#province]').html($(this).html());
					distributors_ajax(woj,city,'',order);	
					$('#province').hide();
					$('#province li:first-child').remove();
					return false;
				});
			}
			var city=$(this).html();
			var order='city asc';		
			$('#province').parent().find('a[href=#province]').html(city);
			distributors_ajax(woj,city,'',order);	
			$('#province').hide();
			return false;
		});		
		var woj=$('#region h3').html();
		var order='city asc';
	}
	$('#sellerFilter dl.zip dd input').focus(function() {
		if($(this).val()=='Wpisz kod') {
			$(this).val('');
		}
	});
	$('#sellerFilter dl.zip dd input').focusout(function() {
		if($(this).val()=='') {
			$(this).val('Wpisz kod');
		}
	});
	$('#sellerFilter dl.zip dd span').click(function() {
		var zip=$('#sellerFilter dl.zip dd input').val();
		if (zip!='' && zip!='Wpisz kod') {
			var city='';
			var order='city asc';	
			$('#province').parent().find('a[href=#province]').html('Wszystkie');
			if($('#province li:first-child').html()=='Wszystkie') {
				$('#province li:first-child').remove();
			}

			distributors_ajax(woj,city,zip,order);	
			$('#province').hide();
			return false;
		}
	});
	distributors_ajax(woj,'','',order);

}
function distributors_ajax(woj,city,zip,order) {
			var randomnumber=Math.floor(Math.random()*10001);
			if ($('#distributors').length) {
				var type='dystrybucja';
			} else {
				var type='sprzedaż';
			}
			var query = encodeURI("woj="+woj+"&city="+city+"&zip="+zip+"&order="+order+"&type="+type+"&rrr="+randomnumber);
			$.ajax({
			type: "GET",
			url: "/dystrybucja_ajax.htm",
			dataType: "html",
			data: query,
			error: function(){
					//alert('Spróbuj ponownie poźniej');
			},
			beforeSend: function() {
				$('#distributors tbody').html('<tr><td colspan="6" class="info"><strong class="preloader">Trwa wczytywanie danych...</strong></td></tr>');
				$('#sellers tbody').html('<tr><td colspan="5" class="info"><strong class="preloader">Trwa wczytywanie danych...</strong></td></tr>');
			},
			success: function(data){
				if (data.length<10) {
					$('#distributors tbody').html('<tr><td colspan="6" class="info"><strong class="info">Brak wyników...</strong></td></tr>');
					$('#sellers tbody').html('<tr><td colspan="5" class="info"><strong class="info">Brak wyników...</strong></td></tr>');				
				} else {
					$('#distributors tbody').html(data);
					$('#sellers tbody').html(data);
					distributors_sort(woj, order);				
				}
			}
		});	
		return false;	
}
function distributors_sort(woj, order) {
	$('#distributors thead ul.sorting li a').unbind('click');
	$('#distributors thead ul.sorting li a').click(function() {
		var sort_element=$(this).parent().parent().parent().attr('class');
		var dir=$(this).attr('class');
		var woj=$('#province').parent().find('a[href=#province]').html();
		$('#distributors thead ul.sorting li').removeClass('selected');
		$('#distributors thead th.'+sort_element+' a.'+dir).parent().addClass('selected');
		dir=dir.replace('ascending', 'asc').replace('decreasing', 'desc');
		order=sort_element+' '+dir;
		distributors_ajax(woj, '','',order);	
		return false;	
	});	
}
	

$(document).ready( function() {
	newWindow();
	if($('#homeFlash').length) {homeSwf();}
	if($('#product').length) {slideProductTab();}
	if($('#newsletter').length) {newsletterAction();}
	if($('#distributors, #sellers').length) {distributors();}
	if($('#map').length) {mapPl();}
});


