var changeInputType = function(oldObject, oType, focus) {
    if ($.browser.msie) {
      var newObject = jQuery('<input type="'+oType+'" />');
      newObject.val($(oldObject).val());
      newObject.attr('name', $(oldObject).attr('name'));
      newObject.attr('id', $(oldObject).attr('id'));
      newObject.attr('class', $(oldObject).attr('class'));
      $(oldObject).replaceWith(newObject);

    //  console.log(newObject.val());
      if (focus == 'focus') setTimeout(function() {newObject.focus()},1);

      initForms(true);
      
      //return newObject;
    } else {
        oldObject.type = oType;
    }
}

var initForms = function (dontmap) {
    /**///formularze
	$('input[type="text"], input[type="password"], textarea, select').prev('label').addClass("hide");
        if (!dontmap) {
            $('input[type="text"], textarea').map(function(){
                var test = $.trim($(this).val()).length;
                if (test == 0) {
                    var newVal = $(this).prev("label").text();
                    $(this).val(newVal);
                    $(this).addClass('emptyField');
                };
            });
        }
        $('input[type="text"], input[type="password"], textarea').unbind();
	$('input[type="text"], input[type="password"], textarea').focus(function () {
            var label = $(this).prev("label").text();
            var inputVal = $.trim($(this).val());
            $(this).removeClass('emptyField');
            if (label == inputVal) {
                $(this).val('');
                if ($(this).hasClass('passwordField')) {
                    changeInputType(this,'password','focus');
                }
            }
        });
	$('input[type="text"], input[type="password"], textarea').blur(function () {
            var label = $(this).prev("label").text();
            var inputValLength = $.trim($(this).val()).length;
            if (inputValLength == 0) {
                $(this).val(label);
                $(this).addClass('emptyField');
                if ($(this).hasClass('passwordField')) changeInputType(this,'text');
                
            }
        });
}

$(function() {
	/**///nadaję klasę body, mówiącą, że jest obsługa js
	$("body").addClass("js-enabled");


	/**///co drugi rząd w tabeli dla IE lte 8 i inne selektory css3
	if ($.browser.msie && $.browser.version < 9) {
		$("table").attr('cellspacing', '0').find("tbody tr:odd").addClass("odd");
		$(".nav-popular-dreams li:nth-child(5n+6)").addClass("nth-child");
		$(".listing .inner ul.nav li:first-child, .listing .inner ul.nav li:nth-child(4n+5)").addClass("nth-child");
		$(".section:last-child").addClass("last-child");
	}

	/**///piętnuję operę
	if ($.browser.opera) {
		$("body").addClass("opera");
	}

	/**///piętnuję chrome
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	if (is_chrome) {
		$("body").addClass("googlechrome");
	}

	/**///colorbox
	$("a[rel='gallery'], a.thumbnail").colorbox({opacity:0.65});

	initForms();

    jQuery(".rating a").click(function () {
        var self = jQuery(this);
        self.parent().children().css('display','none')
        var ind = jQuery("<img src='/img/common/indicator9mq.gif' style='position:relative;top:0' />");
        self.parent().append(ind);

        $.get(jQuery(this).attr('href'), {'json':1},function(data) {
            jQuery(".rating a").css('display','');
            jQuery(ind).remove();
            var yes,pyes,no,pno;
            if (data.votes != 0) {
                pyes = yes = Math.round((data.votes_yes/data.votes)*100);
                if (pyes > 90) pyes = 90;
                if (pyes < 10) pyes = 10;
                no = 100 - yes;
                pno = 100 - pyes;
                if (pyes > pno) {pyes--;} else {pno--};
                self.closest('li').children('.rating-status').html(
                '<span style="width: '+pyes+'%;"><strong>Tak</strong> '+yes+'%</span><span style="width: '+pno+'%;"><strong>Nie</strong> '+no+'%</span>'
                ).css('display','');
            }
            
        },'json');

       return false;

    });

    var redirect = window.location;
    var loginFunc = function () {
        var formcall = null;
        if (jQuery(this)[0].tagName.toLowerCase() == 'input') {
            formcall = jQuery(this).closest('form');
        }else if (jQuery(this).attr('href')) {
            redirect = jQuery(this).attr('href');
        }
        jQuery("#cboxLoadedContent form.registerFrm, #cboxLoadedContent form.loginFrm").unbind().submit(function () {
            var form = jQuery(this);
            jQuery("#cboxLoadedContent").hide();
            jQuery("#cboxLoadingOverlay").show();
            jQuery("#cboxLoadingGraphic").show();
            var res = $.ajax({async: false,type: 'POST',url: form.attr('action'),data: form.serialize()}).responseText;
            var json = eval('('+res+')');
            if (json.status == 1) {
                if (formcall && (formcall.length > 0)) {
                    formcall.submit();
                    return false;
                }else{
                    if (redirect == jQuery('#login-do').attr('href')) redirect = window.location.href;
                    if (redirect == jQuery('#register-do').attr('href')) redirect = window.location.href;
                    if (redirect == window.location.href) {
                        location.reload();
                    } else {
                        window.location = redirect;
                    }
                }
            } else {
                jQuery("#cboxLoadedContent").show();
                jQuery("#cboxLoadingOverlay").hide();
                jQuery("#cboxLoadingGraphic").hide();
                $.colorbox({'html': res, onComplete: loginFunc});
            }
            initForms();
            return false;
        });
    };

    if (jQuery('.disp-n #loginFrm').length > 0) {
        jQuery(".login-do").colorbox({opacity: 0.5,inline:true, href: "#loginFrm", onComplete: loginFunc});
    }

    if (jQuery('.disp-n #registerFrm').length > 0) {
        jQuery(".register-do").colorbox({opacity: 0.5,inline:true, href:"#registerFrm", onComplete: loginFunc});
    }


});

