function fileSetStyle(obj) {
    jQuery(obj).filestyle({
        image: "/img/common/browse-files.gif",
        imageheight : 22,
        imagewidth : 93,
        width : 277
    });

    jQuery('.file').each(function(el) {
        if (jQuery(this).attr('value') == '') jQuery(this).attr('value', 'wybierz zdjęcie');
    });
}

function changeInputType(oldObject, oType, focus) {
    if (jQuery.browser.msie) {

        var onfocus = oldObject.onfocus;
        var onblur = oldObject.onblur;
        
        var newObject = jQuery('<input type="'+oType+'" />');
        newObject.val(jQuery(oldObject).val());
        newObject.attr('name', jQuery(oldObject).attr('name'));
        newObject.attr('id', jQuery(oldObject).attr('id'));
        newObject.attr('class', jQuery(oldObject).attr('class'));
        newObject.attr('autocomplete', jQuery(oldObject).attr('autocomplete'));
        jQuery(oldObject).replaceWith(newObject);

        if (focus == 'focus') {
            setTimeout(function() {
                newObject.get(0).focus();
                setTimeout(function() {
                    newObject.focus(onfocus);
                    newObject.blur(onblur);
                    newObject.get(0).onfocus = onfocus;
                    newObject.get(0).onblur = onblur;
                },1);
            },1);
        } else {
            newObject.focus(onfocus);
            newObject.blur(onblur);
            newObject.get(0).onfocus = onfocus;
            newObject.get(0).onblur = onblur;
        }
        
        return false;
        

    } else {
        oldObject.type = oType;
    }
}

//jQuery(function() {
jQuery(document).ready(function(){

    jQuery("body").addClass("js-enabled");

    if (jQuery.browser.msie && jQuery.browser.version < 9) {
            jQuery(".reviews .review-content p:last-child, #masthead .options > li:last-child, #ui-inbox tr:last-child, fieldset > ul > li:last-child, .rankingTbl tbody tr:last-child, .prods-comparison tbody tr:last-child, .forum-category-module tbody tr:last-child, .forum-category tr:last-child").addClass("last-child");
            jQuery("#nav-personal li:nth-child(2n), .rankingTbl tbody tr:nth-child(even), .nav-related li:nth-child(2n), .optionsSet li:nth-child(2n), #cosmeticsNav > li:nth-child(4n+5), .products-listing-01 li:nth-child(2n+3), .prods-comparison tbody tr:nth-child(even), .forum-thread > ol > li:nth-child(2n)").addClass("nth-child");
            jQuery(".nav-related li:nth-child(2n+3)").addClass("clear");
            jQuery(".usersEntries li:nth-child(4n+5)").css("clear",'left').css("margin-left",0);
    }
   
    if (jQuery.browser.opera) {
        jQuery("body").addClass("opera");
    }

    fileSetStyle(jQuery(".fileInput :file"));
    var nbFiles = jQuery(".fileInput :file").length - 1;
    if (jQuery(".lookUploadFrm").length > 0) nbFiles++;

    jQuery('.addNextAtt').click(function() {
        var input = jQuery('<input type="file" name="look_photos[file_'+ (nbFiles++) +']" />');
        jQuery(this).parent().before(jQuery('<li class="fileInput"></li>').append(input));
        fileSetStyle(input);
        return false;
    });
    var bzFiles = jQuery(".fileInput :file").length;
    jQuery('.addBazaarNextAtt').click(function() {
        var input = jQuery('<input type="file" name="bazaar_photos[file_'+ (bzFiles++) +']" />');
        jQuery(this).parent().before(jQuery('<li class="fileInput"></li>').append(input));
        fileSetStyle(input);
        return false;
    });

    var slFiles = jQuery(".fileInput :file").length;
    jQuery('.addSalonNextAtt').click(function() {
    	
    	if(slFiles > 9) {
    		alert('Możesz dodać maksymalnie 10 zdjęć swojego salonu');
    	} else {
    		slFiles = slFiles + 1;
    		var input = jQuery('<input type="file" class="salonAddFile" name="salon_photos[file_'+ slFiles +']" />');
    		jQuery(this).parent().before(jQuery('<li class="fileInput"></li>').append(input));
    		fileSetStyle(input);
    	}
    	return false;
    });
    
    var msgFiles = jQuery(".fileInput :file").length;
    jQuery('.messageFrm .attach').click(function() {
        var input = jQuery('<input type="file" name="iw_message_photos[file_'+ (++msgFiles) +']" />');
        jQuery('ul',jQuery(this).parent()).append(jQuery('<li class="fileInput"></li>').append(input));
        fileSetStyle(input);
        return false;
    });
    
    nbFiles++;
    jQuery('.addNextForumFile').click(function() {
        var input = jQuery('<input type="file" name="iw_forum_attachment[att_file_'+ (++nbFiles) +']" />');
        jQuery(this).parent().before(jQuery('<li class="fileInput uploadPhoto"></li>').append(input));
        fileSetStyle(input);
        return false;
    });

    var nbUrls = jQuery(".urlPhoto input").length - 1;
    jQuery('.addNextForumUrl').click(function() {
        var input = jQuery('<input type="text" name="iw_forum_attachment[att_url_'+ (++nbUrls) +']" />');
        jQuery(this).parent().before(jQuery('<li class="urlPhoto"></li>').append(input));
        return false;
    });

    
    jQuery(".lookbookNav .trigger").click(function() {
        var unfolded = jQuery.cookie('unfolded');
        unfolded = (unfolded == null) ? "" : unfolded;
        if (jQuery(this).parent("li").hasClass('unfolded')) {
            jQuery(this).parent("li").removeClass('unfolded');
            jQuery(this).text('rozwiń');
            unfolded = unfolded.replace(jQuery(this).parent("li").attr('id').replace('category-','') + ',','');
        } else {
            jQuery(this).parent("li").addClass('unfolded');
            jQuery(this).text('zwiń');
            unfolded = unfolded + jQuery(this).parent("li").attr('id').replace('category-','') + ',';
        }
        jQuery.cookie('unfolded',unfolded,{path: '/'});
        return false;
    });

    jQuery(".cookbookNav .trigger, .cookbookNav p a").click(function() {
        var unfolded = jQuery.cookie('cookbook_unfolded');
        unfolded = (unfolded == null) ? "" : unfolded;
        if (jQuery(this).closest("li").hasClass('unfolded')) {
            jQuery(this).closest("li").removeClass('unfolded');
            jQuery('.trigger',jQuery(this).closest("li")).text('rozwiń');
            unfolded = unfolded.replace(jQuery(this).closest("li").attr('id').replace('category-','') + ',','');
        } else {
            jQuery(this).closest("li").addClass('unfolded');
            jQuery('.trigger',jQuery(this).closest("li")).text('zwiń');
            unfolded = unfolded + jQuery(this).closest("li").attr('id').replace('category-','') + ',';
        }
        jQuery.cookie('cookbook_unfolded',unfolded,{path: '/'});
        return false;
    });

    jQuery(".forum-cats-preview ul ul").each(function(){
        var lh = jQuery(this).height();
        if(lh < 30) {
            jQuery(this).addClass('short');
        }
    });

    jQuery("#nav-subpage a").mouseover(function() {
        jQuery("#nav-subpage a").removeClass("active");
        jQuery(this).addClass("active");
        jQuery('.hairstylenav').addClass("none");
        jQuery('.'+jQuery(this).attr('id')).removeClass("none");
    });
    
    /**///kliknięcie na listę w uploadzie plików
    jQuery(".choices-lists .lookCategory a").click(function(){
        jQuery(".choices-lists .lookCategory a").removeClass('selected');
        jQuery(".choices-lists .lookSubCategory").addClass("none");
        jQuery('#look_look_category_id').val(jQuery(this).attr('class').replace('lookCategory-',''));
        jQuery('.'+jQuery(this).attr('class')).removeClass("none");
        jQuery(this).addClass('selected');
        return false;
    });

    jQuery(".choices-lists .lookSubCategory a").click(function(){
        jQuery(this).toggleClass('selected');
        var id = jQuery(this).attr('class').replace('lookCategory-','').replace('selected','').trim();
        if (jQuery(this).hasClass('selected')) {
            jQuery('#look_look_subcategorys').val(jQuery('#look_look_subcategorys').val() + id + ',');
        } else {
            jQuery('#look_look_subcategorys').val(jQuery('#look_look_subcategorys').val().replace(id + ',',''));
        }
        return false;
    });

    

    jQuery(".choices-lists .hairLength a").click(function(){
        jQuery(".choices-lists .hairLength a").removeClass('selected');
        jQuery('#hair_hair_length').val(jQuery(this).attr('class').replace('hairLength-',''));
        jQuery(this).addClass('selected');
        return false;
    });

    jQuery(".choices-lists .hairStyle a").click(function(){
        jQuery(this).toggleClass('selected');
        var id = jQuery(this).attr('class').replace('hairStyle-','').replace('selected','').trim();
        if (jQuery(this).hasClass('selected')) {
            jQuery('#hair_hair_style').val(jQuery('#hair_hair_style').val() + id + ',');
        } else {
            jQuery('#hair_hair_style').val(jQuery('#hair_hair_style').val().replace(id + ',',''));
        }
        return false;
    });

    jQuery(".choices-lists .hairOccasion a").click(function(){
        jQuery(this).toggleClass('selected');
        var id = jQuery(this).attr('class').replace('hairOccasion-','').replace('selected','').trim();
        if (jQuery(this).hasClass('selected')) {
            jQuery('#hair_hair_occasion').val(jQuery('#hair_hair_occasion').val() + id + ',');
        } else {
            jQuery('#hair_hair_occasion').val(jQuery('#hair_hair_occasion').val().replace(id + ',',''));
        }
        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").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});
            }

            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});
    }


    if (jQuery('#signin_email').length > 0) {
        if ((jQuery('#signin_email').val() == '') || (jQuery('#signin_email').val() == 'nazwa użytkownika*')) {
            jQuery('#signin_email').addClass('emptyField');
            jQuery('#signin_email').val('nazwa użytkownika*')
        }else {
            jQuery('#signin_email').removeClass('emptyField');
        }
    }

    

    if (jQuery('#signin_password').length > 0) {
        if ((jQuery('#signin_password').val() == '')) {
            //jQuery('#signin_password').addClass('emptyField');
        } else {
            //jQuery('#signin_password').removeClass('emptyField');
        }

        jQuery('#signin_password').parent().css('position','relative');
        var passov = jQuery('<span>hasło*</span>').css('position','absolute');
        var pos = jQuery('#signin_password').position();
        passov.css({
            position:'absolute',
            left:pos.left,
            top:pos.top,
            width:jQuery('#signin_password').width(),
            height:jQuery('#signin_password').height(),
            'font-family': 'Georgia,"Times New Roman",Times,serif !important',
            padding: '5px 5px 5px 15px',
            display:'none'
        }).addClass('emptyField').appendTo(jQuery('#signin_password').parent());

        passov.click(function() {
            passov.css('display','none');
            jQuery('#signin_password').focus();
        });

        jQuery(window).load(function () {
            if ((jQuery('#signin_password').val() == '')) passov.css('display','block');
        });

        

        jQuery('#signin_password').blur(function() {
            if ((jQuery('#signin_password').val() == '')) passov.css('display','block');
        }).focus(function() {
            passov.css('display','none');
        });

    }

//});


//jQuery(document).ready(function(){

    if (jQuery('.featuredArticles').length > 0) {
        var slider;
        var current = jQuery('#featArticle1');
        jQuery('div.featArticle').css({opacity:0});
        current.css({opacity: 1});

        var slideTo = function(pos) {
            if (current.attr('id') != ('featArticle'+pos)) {
                var prev = current;
                current = jQuery('#featArticle'+pos);
                prev.fadeTo(150,0,function() {prev.addClass("hide");});
                current.removeClass("hide").fadeTo(150,1,function() {current.removeClass("hide");current.css({opacity: 1});} );
                
            }
        };

        var elements = jQuery('div.featArticle').size();
        
        var currPos = 1;
        var autoSlide = setInterval(function() {
            if (currPos > elements) {currPos = 1;}
            slideTo(currPos);
            currPos++;
        },10000);

        jQuery('.newsPagination li a').mouseover(function() {
            if (slider) slider.stop();
            if (autoSlide) clearInterval(autoSlide);
            var pos = jQuery(this).text();
            slideTo(pos,jQuery(this));
        });

    }
    if (jQuery('#photo-description').length > 0) {
        jQuery('#photo-description').autogrow({minHeight: 100,maxHeight:250});
    }

    if (jQuery('.commentBody').length > 0) {
        jQuery('.commentBody').autogrow({minHeight: 100,maxHeight:250});
    }

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

        $.get(jQuery(this).attr('href'), function(data) {
           jQuery(em).css('display','').text(data);
           jQuery(ind).remove();
            if (data >= 0) {
                jQuery(em).addClass('black');
            } else {
                jQuery(em).removeClass('black');
            }
        });

       return false;

    });
    var prevRate = 0;
    jQuery(".vote a").each(function(i,el) {
       if (!$(el).hasClass('best')) prevRate++;
    });
    jQuery(".vote a").click(function () {
        if (!jQuery(this).hasClass('login-do')) {
        if (!jQuery('#related-rating .vote, #rating .vote').hasClass('voted')) {
            prevRate = jQuery(this).text();
            var parent = jQuery(this).parent();
            jQuery(".vote a").css('display','none');
            var ind = jQuery("<img src='/img/common/indicator9mq.gif' style='position:relative;top:0' />");
            parent.append(ind);
            $.get(jQuery(this).attr('href'), function(data) {
                if (data.error) {
                    jQuery('<p>'+data.error+'</p>').insertAfter(jQuery(".vote"));
                }else{
                    jQuery('#related-rating .vote, #rating .vote').addClass('voted');
                    jQuery('#related-rating .vote span, #rating .vote span').text('Ocena');
                    jQuery('#related-rating .curent-rate strong, #rating .curent-rate strong').text(data.rating);
                    jQuery('#related-rating .curent-rate em, #rating .curent-rate em').text(data.votes);
                    
                }
                jQuery(".vote a").css('display','');
                ind.remove();
            },'json');
        }
            return false;
        }
    }).mouseenter(function() {
        if (!jQuery('#related-rating .vote, #rating .vote').hasClass('voted')) {
            var curr = jQuery(this).text();
            jQuery(".vote a").each(function(i,el) {
               if ($(el).text() <= curr) {
                   $(el).removeClass('best');
               } else {
                   $(el).addClass('best');
               }
            });
        }
    });

    jQuery('#related-rating .vote, #rating .vote').mouseleave(function(){
        jQuery(".vote a").each(function(i,el) {
           if ($(el).text() <= prevRate) {
               $(el).removeClass('best');
           } else {
               $(el).addClass('best');
           }
        });
    });

    jQuery('.hairstylesOrder').change(function() {
        var form = jQuery(this).closest('form');
        form.submit();
        return false;
    });

    jQuery('.searchSet .more').click(function() {
        jQuery('.optionsSet').toggleClass('none');
        return false;
    });

    if (jQuery('linkdirectory')) {
        jQuery('.roll').click(function() {
        var item = jQuery(this).parent().children('ul');
        item.toggleClass('none');
        var rolled = jQuery.cookie('linkdir_rolled');
        if (!rolled) rolled = '';
        if (!item.hasClass('none')) {
            rolled = rolled + item.attr('id').replace('category-','') + ',';
        } else {
            rolled = rolled.replace(item.attr('id').replace('category-','') + ',','');
        }

        jQuery.cookie('linkdir_rolled',rolled,{path: '/'});
        return false;
    });
    } else {
        jQuery('.roll').click(function() {
        var item = jQuery(this).parent().children('ul');
        item.toggleClass('none');
        var rolled = jQuery.cookie('rolled');
        if (!rolled) rolled = '';
        if (!item.hasClass('none')) {
            rolled = rolled + item.attr('id').replace('category-','') + ',';
        } else {
            rolled = rolled.replace(item.attr('id').replace('category-','') + ',','');
        }

        jQuery.cookie('rolled',rolled,{path: '/'});
        return false;
    });
    }
    

    jQuery(".bazaarCategories .trigger").click(function() {
        var unfolded = jQuery.cookie('bazaar_unfolded');
        
        unfolded = (unfolded == null) ? "" : unfolded;
        if (jQuery(this).parent().hasClass('unfolded')) {
            jQuery(this).parent().removeClass('unfolded');
            jQuery(this).text('rozwiń');
            unfolded = unfolded.replace(jQuery(this).parent().attr('id').replace('category-','') + ',','');
        } else {
            jQuery(this).parent().addClass('unfolded');
            jQuery(this).text('zwiń');
            unfolded = unfolded + jQuery(this).parent().attr('id').replace('category-','') + ',';
        }
        jQuery.cookie('bazaar_unfolded',unfolded,{path: '/'});
        return false;
    });


    jQuery('.editAvatar, .avatarForm .cancel').click(function() {
        jQuery(".avatarForm").toggleClass("none");
        return false;
    });

    jQuery('.editAvatarBottom, .editAvatarBottom .cancel').click(function() {
        jQuery(".avatarFormBottom").toggleClass("none");
        return false;
    });


    jQuery('.markAll').click(function() {
        jQuery('#ui-inbox .choose input').attr('checked', true);
        return false;
    });

    jQuery('.unmarkAll').click(function() {
        jQuery('#ui-inbox .choose input').attr('checked', false);
        return false;
    });

    jQuery('.mailActions').change(function() {
        if (jQuery('.mailActions').val()) {
            $(this).closest('form').submit();
        }
    });

    jQuery('.figure.cosmetic .next').click(function(){
        var next = jQuery(this).closest('.figure').next('.figure');
        if (next.length == 0) {
            next = jQuery('.figure.cosmetic').first();
        }
        jQuery('.figure.cosmetic').addClass('none');
        jQuery(next).removeClass('none');
        return false;
    });

    jQuery('.figure.cosmetic .prev').click(function(){
        var prev = jQuery(this).closest('.figure').prev('.figure');
        if (prev.length == 0) {
            prev = jQuery('.figure.cosmetic').last();
        }
        jQuery('.figure.cosmetic').addClass('none');
        jQuery(prev).removeClass('none');
        return false;
    });

    var currCosCat = null;
    var cosTimer = null;

    jQuery("#cosmeticsNav .subNav").mouseenter(function() {
        currCosCat = jQuery(this);
        cosTimer = setTimeout(function() {
            if (currCosCat) currCosCat.addClass('ihover');
        },400);
    }).mouseleave(function() {
        clearTimeout(cosTimer);
        currCosCat = null;
        jQuery("#cosmeticsNav .subNav").removeClass('ihover');
    })

    jQuery('.commentsFrm').submit(function() {
        if (this.submitted) {
            return false;
        }
        this.submitted = true;
        jQuery('.commentsFrm .button').attr("disabled", true);
    });

    jQuery('.bazaar .introductory .close').click(function() {
        jQuery(this).parent().addClass('hide');
        jQuery.cookie('bazaar_intro','hide',{path: '/',expires: 30});
        return false;
    })

    jQuery(".bazaar-rules").colorbox({opacity: 0.5,inline:true, href: "#bazaar-rules",maxHeight: 300});

    jQuery("form.commentsFrm").submit(function() {
        jQuery(this).submit(function() {
            return false;
        });
        console.log(1);
        return true;
    });


});
