$(document).ready(function() {
	
	$('input[type="text"]').focus(function() {
        if (this.value == this.defaultValue){
        	this.value = '';
    	}
        if(this.value != this.defaultValue){
	    	this.select();
        }
    });
    $('input[type="text"]').blur(function() {
        if (this.value == ''){
        	this.value = this.defaultValue;
    	}
    });
	
	$('.confirm').click( function() {
		if(confirm("Czy jesteś pewien?")) {
			return true;
		} else { return false; }
	});
	
    $("#topSlide").cycle();
    
    th_var=0;
    th_left=$("#fb_main").css('left');
    $("#fb_button").click(function(){
        if(th_var==0){
                $("#fb_main").animate({
                        left: '0'
                }, 500,function(){
                        th_var=1;
                });
        }else{
                $("#fb_main").animate({
                        left: ''+th_left+''
                }, 500,function(){
                        th_var=0;
                });
        }
    });
    
    $("#updateCash").change(function() {
        var optionValue = $('#updateCash :selected').val();
        var kasa = $('#updateMoney').val();
        
        var values = optionValue.split(':');
        
        var ile = values[2];

        var razem = ile*kasa;

        $("#updateMoney").val(razem);
    });
    
	$("div.errorNotice").delay(200).fadeTo("slow", 1);
	$("div.addedNotice").delay(500).show(200).fadeTo("slow", 1).delay(3750).fadeTo("slow",0).hide(200);
	
	$("a[rel=lightbox]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition'  	: 'inside'
	});
    
	$('.activateRowClick tr').click(function(event) {
		if (event.target.type !== 'checkbox') {
			$(':checkbox', this).trigger('click');
		}
	});
	
	$('.btnAddComment').click(function() {
		$('.objectCommentForm').slideDown(500);
	});
    
    var cssDotpayActivate = $("#dotpay-activate-table").val();
    
    if(cssDotpayActivate=="true") {
        $("#dotpay_activate_table").show();
    } else {
        $("#dotpay_activate_table").hide(); 
    }
});


$().ready(function() {

        $("#formValidate").validate();
        
		$("#moduleContactForm").validate({
			rules: {
				contact_email: { required: true, email: true }
			}
		});
		
		$("#moduleCommentForm").validate();
        
		$("#moduleObjectAdd").validate({
			rules: {
				object_www: { required: false, url: true },
				object_email: { required: false, email: true }
			},
			messages: {
				object_name: { required: "podaj poprawną nazwę obiektu" },
				object_street: { required: "podaj dokładny adres np. Krótka 1/2" },
				object_postcode: { required: "podaj poprawny kod pocztowy w formacie xx-xxx" },
				object_city: { required: "podaj nazwę miejscowości" },
				object_rooms: { required: "podaj poprawną liczbę pokoi" },
				object_cost_min: { required: "podaj cenę minimalną" },
				object_cost_max: { required: "podaj cenę maksymalną" },
				object_phone: { required: "podaj numer telefonu do obiektu" }
			}
		});
	
		$("#moduleUserRegister").validate({
			rules: { 
				user_name: { required: true, minlength: 2, maxlength: 30 },
				user_email: { email: true, required: true },
				user_email_repeat:	{ required: true, minlength: 5, maxlength: 80, equalTo: "#userEmail"},
				user_pass: { required: true, minlength: 3,	maxlength: 20 },
				user_pass_haslo: { required: true, minlength: 3, maxlength: 20, equalTo: "#userPassword" }
			},
			messages: {
				user_name: {
					required: "podaj nazwę użytkownika",
					rangelength: jQuery.format("podaj conajmniej {0} znaków")
				},
				user_email: {
					required: "podaj adres e-mail np.(grzegorz@avatec.pl)",
					rangelength: jQuery.format("podaj conajmniej {0} znaków")
				},
				user_email_repeat: { 
					required: "podaj ponownie adres e-mail", 
					equalTo: "#userEmail"
				},
				user_pass: {
					required: "Podaj hasło (minimum 3 znaki, maksimum 20)",
					rangelength: jQuery.format("Co najmniej {0} znaków")
				},
				user_pass_repeat: {
					required: "Podaj swoje hasło ponownie",
					minlength: jQuery.format("Co najmniej {0} znaków"),
					maxlength: jQuery.format("Co najwyżej {0} znaków"),
					equalTo: "Podaj swoje hasło ponownie"
				}

			}
		});
		
		$("#moduleUserRemember").validate({
			rules: { 
				user_name: { required: true, minlength: 2, maxlength: 30 },
				user_email: { email: true, required: true }
			},
			messages: {
				user_name: {
					required: "podaj nazwę użytkownika",
					rangelength: jQuery.format("podaj conajmniej {0} znaków")
				},
				user_email: {
					required: "podaj adres e-mail np.(grzegorz@avatec.pl)",
					rangelength: jQuery.format("podaj conajmniej {0} znaków")
				}
			}
		});
	});

   /*google.load("language", "1");*/
   google.load("elements", "1", { packages: "transliteration" });
   function translate( lang )
   {
        google.language.transliterate(["Namaste"], "pl", lang, function(result) {
          if (!result.error) {
            var container = document.getElementById("main");
            if (result.transliterations && result.transliterations.length > 0 &&
                result.transliterations[0].transliteratedWords.length > 0) {
              container.innerHTML = result.transliterations[0].transliteratedWords[0];
            }
          }
        });
   }
   
   
   
/**********************************************************************************************************
   Gotowe funkcje
 **********************************************************************************************************/
   
   function explode (delimiter, string, limit) {

	    var emptyArray = { 0: '' };
	    
	    // third argument is not required
	    if ( arguments.length < 2 ||
	        typeof arguments[0] == 'undefined' ||
	        typeof arguments[1] == 'undefined' ) {
	        return null;
	    }
	 
	    if ( delimiter === '' ||
	        delimiter === false ||
	        delimiter === null ) {
	        return false;
	    }
	 
	    if ( typeof delimiter == 'function' ||
	        typeof delimiter == 'object' ||
	        typeof string == 'function' ||
	        typeof string == 'object' ) {
	        return emptyArray;
	    }
	 
	    if ( delimiter === true ) {
	        delimiter = '1';
	    }
	    
	    if (!limit) {
	        return string.toString().split(delimiter.toString());
	    } else {
	        // support for limit argument
	        var splitted = string.toString().split(delimiter.toString());
	        var partA = splitted.splice(0, limit - 1);
	        var partB = splitted.join(delimiter.toString());
	        partA.push(partB);
	        return partA;
	    }
	}

	function dump(arr,level) {
		var dumped_text = "";
		if(!level) level = 0;
		
		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";
		
		if(typeof(arr) == 'object') { //Array/Hashes/Objects
		 for(var item in arr) {
		  var value = arr[item];
		 
		  if(typeof(value) == 'object') { //If it is an array,
		   dumped_text += level_padding + "'" + item + "' ...\n";
		   dumped_text += dump(value,level+1);
		  } else {
		   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
		  }
		 }
		} else { //Stings/Chars/Numbers etc.
		 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		}
		return dumped_text;
	} 
	
	function str_replace (search, replace, subject, count) 
	{
	    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
	            f = [].concat(search),
	            r = [].concat(replace),
	            s = subject,
	            ra = r instanceof Array, sa = s instanceof Array;
	    s = [].concat(s);
	    if (count) {
	        this.window[count] = 0;
	    }
	 
	    for (i=0, sl=s.length; i < sl; i++) {
	        if (s[i] === '') {
	            continue;
	        }
	        for (j=0, fl=f.length; j < fl; j++) {
	            temp = s[i]+'';
	            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
	            s[i] = (temp).split(f[j]).join(repl);
	            if (count && s[i] !== temp) {
	                this.window[count] += (temp.length-s[i].length)/f[j].length;}
	        }
	    }
	    return sa ? s : s[0];
	}
