var MSIE = false;
if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)MSIE=true;
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
var debug = false;
var rem_addr = '';
var Krakinwest = {
	robiem: false,actZm: 0,slTime: 4000,reklamy:[],tabs:[],errs:[],ta: false,h1: false,h2: false,debug: true,reg: [],regBackColor: '#1a0b0b',
	getSelf: function(){ return this;},
	mousemove: function(e){
		var Ob = Krakinwest.getSelf();
		//document.title = Ob.robiem;
		if (Ob.robiem) {
			return;
		}
		e.pageX;e.pageY;
		var o = $(this).offset();

		if (e.pageX-o.left<435) {
			if (!Ob.h1) {
				Ob.robiem = true;
				Ob.h1 = true;
				$('#i1').fadeIn('slow',function(){Krakinwest.robiem = false;});

			}
			if (Ob.h2) {
				Ob.h2 = false;
				$('#i2').fadeOut('slow');
			}
		}else{
			if (Ob.h1) {
				Ob.robiem = true;
				Ob.h1 = false;
				$('#i1').fadeOut('slow',function(){Krakinwest.robiem = false;});
			}
			if (!Ob.h2) {
				Ob.h2 = true;
				$('#i2').fadeIn('slow');
			}
		}
	},
	leave: function(e){
		$('.ind').hide();
		var Ob = Krakinwest.getSelf();
		Ob.h1 = false;
		Ob.h2 = false;
		Ob.robiem = false;
		//$(this).unbind('mousemove');
	},
	sun:function(e){
		var Ob = Krakinwest.getSelf();
		clearTimeout(Ob.ta);
		var o = $(this).offset();
		if (e.pageX-o.left<435){
			Ob.actZm = Ob.actZm-2;
			if(Ob.actZm<0){
				Ob.actZm = Ob.reklamy.length+Ob.actZm;
			}
		}
		Ob.startSlide();
	},
	sunsie:function(){
		var pos = $('#taca').position();
		if (this.id == 'suwL') {
			var plus = '+';
			if (pos.left >= 0) {
				return;
			}
		}else{
			var plus = '-';
			if (pos.left+$('#taca').width()<=477) {
				return;
			}
		}
		$('#taca').animate({left: plus+'=159'});
	},
	startSlide: function(){
		var Ob = Krakinwest.getSelf();
		if (Ob.reklamy.length>1) {
			//$('#ste img').eq(actZm).attr('src','/img/krop.png');
			//document.title = Ob.actZm+' '+Ob.reklamy.length;
			if (Ob.actZm>=Ob.reklamy.length) {
				Ob.actZm = 0;
			}
			var gdzie = Ob.actZm*871;
			$('#scena').animate({left: '-'+gdzie},100);
			//$('#ste img').eq(Ob.actZm).attr('src','/img/krop_ov.png');
			Ob.actZm++;
			Ob.ta = setTimeout(Ob.startSlide,Ob.slTime);
		}
	},
	Tabshow: function(){
		try{
			var fix = this.id.substr(2);
		}catch(e){
		}
		var conid = "con_"+fix;
		if ($('#'+conid).css('display') == 'block') {
			return;
		}
		var Ob = Krakinwest.getSelf();
		if (Ob.robiem) {
			return;
		}
		Ob.robiem = true;
		$('#sz'+fix).css('background',"url(/img/nactive_tab.jpg) top center repeat-x");
		$('#sz'+fix+'l').css('background',"url(/img/l_tab.jpg) top left repeat-x");
		$('#sz'+fix+'r').css('background',"url(/img/r_tab.jpg) top right repeat-x");
		for (var tt = 0; tt< Ob.tabs.length; tt++){

			if ($("#"+Ob.tabs[tt]).css('display') == 'block') {
				$('#'+Ob.tabs[tt]).fadeOut(350,Ob.Koniec);
				$('#sz'+Ob.tabs[tt].substr(4)).css('background',"url(/img/nunactive_tab.jpg) top center repeat-x");
				$('#sz'+Ob.tabs[tt].substr(4)+'l').css('background',"url(/img/ul_tab.jpg) top left repeat-x");
				$('#sz'+Ob.tabs[tt].substr(4)+'r').css('background',"url(/img/ur_tab.jpg) top right repeat-x");
				Ob.rob1 = Ob.tabs[tt];
				break;
			}
		}

		$('#'+conid).css('visibility','hidden');
		$('#'+conid).css('display', 'block');
		Ob.cohh = $('#'+conid).outerHeight();

		Ob.poz = false;
		if (Ob.cohh>$('#szczegoly').outerHeight() || Ob.cohh>$('#szrama').outerHeight()) {
			$('#szrama').css('height',Ob.cohh+10+'px');
			$('#szczegoly').css('height', Ob.cohh+20+'px');
		}else{
			Ob.poz = true;
		}
		$('#'+conid).css('visibility','visible');
		$('#'+conid).css('display', 'none');
		//if ($(conid).offsetHeight >$('szrama').offsetHeight) {
		//}
		$('#'+conid).fadeIn(350);
		Ob.rob2 = conid;
	},
	Koniec: function(){
		var Ob = Krakinwest.getSelf();
		$("#"+Ob.rob1).css('display','none');
		$("#"+Ob.rob2).css('display','block');
		if (Ob.poz) {
			$('#szrama').css('height',Ob.cohh+10+'px');
			$('#szczegoly').css('height',Ob.cohh+20+'px');
		}
		Ob.robiem = false;
	},
	RemTlo: function(){
		$('#gr').animate({
           opacity: 0,
           top: 500
         }, 'fast',Krakinwest.Af1 );
	},
	Af1: function(){
		$('#bg').unbind("click",Krakinwest.RemTlo);
		try{
			$('#gr').remove();
		}catch(e){
			alert(e.toString());
		}
		try{
			$('#bg').remove();
		}catch(e){
			alert(e.toString());
		}
	},
	confirm:function(Yeshandler, Nohandler){
			var srodek = document.createElement('div');
			srodek.id = "gr";
			$('body').append(srodek);
			var ll =  Math.round(($('body').outerWidth()-459)/2)+"px";
			$(srodek).html('<div style="position: absolute; left: 100px; top: 45px; width: 330px; font-weight: bold; color: #000000;">'+this.DFtext+'</div>')
			.css({position: 'absolute',left: ll,width:'459px',height:'119px',background:'url(/img/komunikat_del.png) center center no-repeat',zIndex: 100});
			if (!MSIE || version>6) {
				$(srodek).css('position','fixed');
				if (MSIE) {
					$(srodek).css('top',Math.max(0,Math.round((document.documentElement.clientHeight-119)/2)-50)+"px");
				}else{
					$(srodek).css('top', Math.max(0,Math.round((window.innerHeight-119)/2)-50)+"px");
				}
			}else{
				var coordinates = $('body').scrollTop();
				//srodek.style.top = 50+coordinates[1]+'px';
				$(srodek).css('top',Math.round((document.documentElement.clientHeight-119)/2)-50+coordinates+'px');
			}
			var but1 = document.createElement('IMG');
			$(but1).attr('src','/img/guziki/tak.gif').css({position:'absolute',left:'130px',bottom:'10px'});
			$(but1).click(Yeshandler);
			$(srodek).append(but1);
			var but2 = document.createElement('IMG');
			$(but2).attr('src','/img/guziki/nie.gif').css({position:'absolute',left:'250px',bottom:'10px'});
			if (Nohandler) {
				$(but2).click(Nohandler);
			}else{
				$(but2).click(Krakinwest.RemTlo);
			}
			$(srodek).append(but2);
	},
	tloConfirm: function(Yeshandler,Nohandler,text){
		var Ob = Krakinwest.getSelf();
		Ob.tlo('#000000',false);
		Ob.DFtext = text;
		Ob.confirm(Yeshandler,Nohandler);
		return false;
	},
	tlo: function(color,rem,op){
			var tlo = document.createElement('div');
			tlo.className = "tlo";
			tlo.id = 'bg';
			if (color) {
				$(tlo).css('background',color);
			}
			if (document.documentElement.clientHeight) {
				$(tlo).height(Math.max($('#bodyz3').height(),document.documentElement.clientHeight)+'px');
			}else if (window.innerHeight) {
				$(tlo).height(Math.max($('#bodyz3').height(),window.innerHeight)+'px');
			}
			if (op) {
				$(tlo).fadeTo(1, op);
			}
			$('body').append(tlo);
			if (rem) {
				$('#bg').bind('click',Krakinwest.RemTlo);
			}
			return tlo;
	},
	czekenlog: function(){
		var ret = true;
		var erry = [];
		try{
			if ($('#username').val().length < 5) {
					erry[erry.length] =  this.errs['username'] == undefined?'Brak alertu':this.errs['username'];
					this.SetAlert('username');
					ret = false;
			}
			if ($('#password').val().length < 6) {
					erry[erry.length] =  this.errs['password'] == undefined?'Brak alertu':this.errs['password'];
					this.SetAlert('password');
					ret = false;
			}
			if (ret) {
				return true;
			}
			Krakinwest.addFrontError(erry);
			return false;
		}catch(e){
			if (this.debug) {
				alert(e.toString());
			}
			return false;
		}
	},
	czekentrzy: function(){
		/*if ($('#ort').attr('type') == 'text' && $('#real_city_id').val() == '') {
			this.SetAlert('ort');
			addFrontError(this.errs['ort']);
			return false;
		}*/
		this.setcookie('_lurl','',-1);
		return true;
	},
	czeken2: function(){
			try{
				var ret = true;
				var erry = [];
				if (jQuery.trim($('#imie').val()) == '') {
						erry[erry.length] = this.errs['imie'] == undefined?'Brak alertu':this.errs['imie'];
						ret = false;
						this.SetAlert('imie');
				}
				if ($('#email').val() =='') {
					erry[erry.length] = this.errs['email'] == undefined?'Brak alertu':this.errs['email'];
					ret = false;
					this.SetAlert('email');
				}else{
					var patt = RegExp("^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+.)+[a-zA-Z0-9.-]{2,4}$");
					var macz= $('#email').val();
					var temp = patt.test(macz);
					if (!temp) {
						erry[erry.length] = this.errs['email2'] == undefined?'Brak alertu':this.errs['email2'];
						ret = false;
						this.SetAlert('email');
					}
				}
				if (jQuery.trim($('#pytanie').val()) == '') {
						erry[erry.length] = this.errs['pytanie'] == undefined?'Brak alertu':this.errs['pytanie'];
						ret = false;
						this.SetAlert('pytanie');
				}
				if (ret) {
					return true;
				}
				this.addFrontError(erry);
				return false;
			}catch(e){
				if (this.debug) {
					alert(e.toString());
				}
			}
	},
	czeken: function(form){
			try{
				this.regBackColor = '#fff';
				var ret = true;
				var erry = [];
				if ($('#imie').val() == '') {
					//erry[erry.length] = "Podaj imię i nazwisko";
					erry[erry.length] = this.errs['imie'] == undefined?'Brak alertu':this.errs['imie'];
					ret = false;
						this.SetAlert('imie');
				}
				if ($('#email').val() =='') {
					erry[erry.length] = this.errs['email'] == undefined?'Brak alertu':this.errs['email'];
					ret = false;
					this.SetAlert('email');
				}else{
					var patt = RegExp("^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+.)+[a-zA-Z0-9.-]{2,4}$");
					var macz= $('#email').val();
					var temp = patt.test(macz);
					if (!temp) {
						erry[erry.length] = this.errs['email2'] == undefined?'Brak alertu':this.errs['email2'];
						ret = false;
						this.SetAlert('email');
					}
				}

				if (!$('#agree').attr('checked')) {
					//erry[erry.length] = "Zaakceptuj regulamin";
					erry[erry.length] = this.errs['agree'] == undefined?'Brak alertu':this.errs['agree'];
					ret = false;
					this.SetAlert('agree');
				}
				if (ret) {
					return true;
				}
				this.addFrontError(erry);
				return false;
			}catch(e){
				if (this.debug) {
					alert(e.toString());
				}
				return false;
			}

		},
	GetReg: function(){
		return this.reg;
	},
	DisOff: function(event){
		var ob = event.data.ob;
		try{
			if (ob.reg[this.id]) {
				$(this).stop(true,true);
				$(this).fadeTo(1,1);
				ob.regBack(this.id);
			}
		}catch(e){
			if (ob.debug) {
				alert(e.toString());
			}
		}
	},
	SetAlert: function(id){
		var rid = id;
		$('#'+rid).css("backgroundColor", "#f67964");
		this.reg[id] = $("#"+rid).effect( "pulsate",{times:15}, 300 );

	},
	SetAlerts: function(se){
		$(se).css("backgroundColor", "#f67964");
		this.reg[se] = $(se).effect( "pulsate",{times:15}, 300 );
	},
	regBack: function(id){
		$("#"+id).animate({
           backgroundColor: this.regBackColor
          // opacity: 1
         }, 500 );
	},
	addFrontError: function(txt){
		var tlo = this.tlo('#000000',true);
		var err = document.createElement('UL');
		err.id = 'error_frontmsg';
		$(err).css({position:'absolute',left:'98px',top:'30px'});
		if (typeof(txt) != 'string') {
			var z = 0
			for(var i = 0; i<txt.length; i++){
				var ll = document.createElement('LI');
				$(ll).text(txt[i]);
				$(err).append(ll);
				z++;
			}
			var height = Math.max(119,z*15+35);
		}else{
			var ll = document.createElement('LI');
			$(ll).text(txt);
			$(err).append(ll);
			var height = 119;
		}
		var srodek = document.createElement('div');
		srodek.id = "gr";
		$('body').append(srodek);
		var lz = Math.round(($(tlo).outerWidth()-459)/2)+'px';
		$(srodek).css({position:'absolute',zIndex:'100',left:lz,width:'459px',height:height+'px',color: '#000000'}).append(err);
		if (height > 119) {
			$(srodek).css('background','url(/img/komunikat2.png) top center repeat-y');
			var srodek2 = document.createElement('div');
			$(srodek2).css({position: "absolute",zIndex: 220,width: '459px',height: '15px',left: "0px",bottom: "-5px",background: 'url(/img/komunikat_ok2.png) bottom center no-repeat'});
			$(srodek).append(srodek2);
			var srodek3 = document.createElement('div');
			$(srodek3).css({position: "absolute",zIndex: 223,width: '459px',height: '30px',left: "0px",top:"-5px",background: 'url(/img/komunikat.png) top center no-repeat'});
			$(srodek).append(srodek3);
			var srodek4 = document.createElement('div');
			$(srodek4).css({position:"absolute",zIndex:224,width:'54px',height:'80px',left: "15px",top: "35px",background: 'url(/img/komunikat3.png) center center no-repeat'});
			$(srodek).append(srodek4);
		}else{
			$(srodek).css('background','url(/img/komunikat.png) top center repeat-y');
		}

		if (!MSIE || version>6) {
			$(srodek).css('position','fixed');
			if (MSIE) {
				$(srodek).css('top',Math.max(0,Math.round((document.documentElement.clientHeight-height)/2)-50)+"px");
			}else{
				$(srodek).css('top',Math.max(0,Math.round((window.innerHeight-height)/2)-50)+"px");
			}
		}else{
			var coordinates = $('body').scrollTop();
			$(srodek).css('top',Math.round((document.documentElement.clientHeight-height)/2)-50+coordinates+'px');
		}
	},
	addFrontInfo: function(txt){
		var tlo = this.tlo('#000000',true);
		var err = document.createElement('UL');
		err.id = 'error_frontmsg';
		$(err).css({position:'absolute',left:'98px',top:'30px'});
		if (typeof(txt) != 'string') {
			var z = 0
			for(var i = 0; i<txt.length; i++){
				var ll = document.createElement('LI');
				$(ll).text(txt[i]);
				$(err).append(ll);
				z++;
			}
			var height = Math.max(119,z*15+35);
		}else{
			var ll = document.createElement('LI');
			$(ll).text(txt);
			$(err).append(ll);
			var height = 119;
		}
		var srodek = document.createElement('div');
		srodek.id = "gr";
		$('body').append(srodek);
		var lz = Math.round(($(tlo).outerWidth()-459)/2)+'px';
		$(srodek).css({position:'absolute',zIndex:'100',left:lz,width:'459px',height:height+'px',color: '#000000'}).append(err);
		if (height > 119) {
			$(srodek).css('background','url(/img/komunikat2.png) top center repeat-y');
			var srodek2 = document.createElement('div');
			$(srodek2).css({position: "absolute",zIndex: 220,width: '459px',height: '15px',left: "0px",bottom: "-5px",background: 'url(/img/komunikat_ok2.png) bottom center no-repeat'});
			$(srodek).append(srodek2);
			var srodek3 = document.createElement('div');
			$(srodek3).css({position: "absolute",zIndex: 223,width: '459px',height: '30px',left: "0px",top:"-5px",background: 'url(/img/komunikat_ok.png) top center no-repeat'});
			$(srodek).append(srodek3);
			var srodek4 = document.createElement('div');
			$(srodek4).css({position:"absolute",zIndex:224,width:'54px',height:'80px',left: "15px",top: "35px",background: 'url(/img/komunikat4.png) center center no-repeat'});
			$(srodek).append(srodek4);
		}else{
			$(srodek).css('background','url(/img/komunikat_ok.png) top center repeat-y');
		}

		if (!MSIE || version>6) {
			$(srodek).css('position','fixed');
			if (MSIE) {
				$(srodek).css('top',Math.max(0,Math.round((document.documentElement.clientHeight-height)/2)-50)+"px");
			}else{
				$(srodek).css('top',Math.max(0,Math.round((window.innerHeight-height)/2)-50)+"px");
			}
		}else{
			var coordinates = $('body').scrollTop();
			$(srodek).css('top',Math.round((document.documentElement.clientHeight-height)/2)-50+coordinates+'px');
		}
	},
	setcookie: function(c_name,value,expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";//+";/;"+location.host.replace('www','');
	},
	getCookie: function (c_name){
		if (document.cookie.length>0){
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start>-1){
			    c_start=c_start + c_name.length+1 ;
			    c_end=document.cookie.indexOf(";",c_start);
			    if (c_end==-1) c_end=document.cookie.length;
				return document.cookie.substring(c_start,c_end);
		    }
		  }
		return false;
	},
	setErrs: function(id,text){
		this.errs[id] = text;
	},
	wysylaj: function(pid){
		try{
			this.regBackColor = '#1a0b0b';
			var ret = true;
			var erry = [];
			if ($('#imie').val() == '') {
				//erry[erry.length] = "Podaj imię i nazwisko";
				erry[erry.length] = this.errs['imie'] == undefined?'Brak alertu':this.errs['imie'];
				ret = false;
					this.SetAlert('imie');
			}
			if ($('#email').val() =='') {
				erry[erry.length] = this.errs['email'] == undefined?'Brak alertu':this.errs['email'];
				ret = false;
				this.SetAlert('email');
			}else{
				var patt = RegExp("^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+.)+[a-zA-Z0-9.-]{2,4}$");
				var macz= $('#email').val();
				var temp = patt.test(macz);
				if (!temp) {
					erry[erry.length] = this.errs['email2'] == undefined?'Brak alertu':this.errs['email2'];
					ret = false;
					this.SetAlert('email');
				}
			}
			if ($('#telefon').val() == '') {
				erry[erry.length] = this.errs['telefon'] == undefined?'Brak alertu':this.errs['telefon'];
				ret = false;
					this.SetAlert('telefon');
			}
			if ($('#pytanie').val() == '') {
				erry[erry.length] = this.errs['pytanie'] == undefined?'Brak alertu':this.errs['pytanie'];
				ret = false;
					this.SetAlert('pytanie');
			}
			if (this.getCookie('p'+pid) && parseInt(this.getCookie('p'+pid))>4) {
				erry[erry.length] = this.errs['zaduzo'] == undefined?'Brak alertu':this.errs['zaduzo'];
				ret = false;
				this.SetAlert('zaduzo');
			}
			if (ret) {
				$.ajax({
				type: "POST",
				url: '/oferta/load',
				data: "pid="+pid+"&imie="+$('#imie').val()+"&telefon="+$('#telefon').val()+"&email="+$('#email').val()+"&pytanie="+$('#pytanie').val(),
				success: function(msg){
				     var Ob = Krakinwest.getSelf();
					 $('#form').html(Ob.errs['sukces']);
				     if (Ob.getCookie('p'+pid)) {
				     	Ob.setcookie('p'+pid,parseInt(Ob.getCookie('p'+pid))+1,1);
				     }else{
				     	Ob.setcookie('p'+pid,1,1);
				     }
				   }
				});
				return;
			}
			this.addFrontError(erry);
			return false;
		}catch(e){
			if (this.debug) {
				alert(e.toString());
			}
			return false;
		}
	}
};
