function fileSetStyle(obj) {
    jQuery(obj).filestyle({
        image: "/img/common/browse-files.gif",
        imageheight : 22,
        imagewidth : 93,
        width : 365
    });

    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;
    }
}

function initForums() {
    var topicsHeight = jQuery(".forums > ul > li:first-child").height(); //wysokość tematów
    var headingHeight = jQuery(".forums > ul > li + li h3").height(); //wysokość nagłówka
    var forumsCropHeight = topicsHeight - headingHeight; //wysokość jaka musi zostać nadana scrollowanemu blokowi
    jQuery(".forums > ul > li + li > ul").height(forumsCropHeight); //nadaje tę wysokość, aby do niej przyciąć
}

jQuery(document).ready(function(){
	
	jQuery("body").addClass("js-enabled");
    if (jQuery.browser.opera) {
        jQuery("body").addClass("opera");
    }	
	
	/* zajawka z fotą i menu */
	//var menuCropHeight = jQuery(".featured img").height(); //wysokość obrazka
	//jQuery(".featured > div + div").height(menuCropHeight); //nadaje tę wysokość, aby do niej przyciąć

    jQuery(".bazaar-rules").colorbox({opacity: 0.5,inline:true, href: "#bazaar-rules", width: 400, height: 300, maxHeight: 300});
    
    fileSetStyle(jQuery(".fileInput :file"));
    var nbFiles = jQuery(".fileInput :file").length - 1;
    
    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;
    });  
    
    jQuery('.editAvatar, .avatarForm .cancel').click(function() {
        jQuery(".avatarForm").toggleClass("none");
        return false;
    });

    jQuery('.editAvatarBottom, .editAvatarBottom .cancel').click(function() {
        jQuery(".avatarFormBottom").toggleClass("none");
        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;
    });    
    
    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;
    });    
	
	initForums();

        jQuery(".r-bad a,.r-ok a").click(function () {
            var el = this;
            var back = jQuery(el).css('background');
            jQuery(el).css('background','transparent url(/img/common/indicator9mq.gif) no-repeat scroll 50% 0');

            jQuery.get(jQuery(el).attr('href'), function(data) {
               jQuery(el).parent().parent().children('strong').text(data);
               jQuery(el).css('background',back);
            });

           return false;
        });
	
	/* zwijanie pytania */
	jQuery("#question .frmHead > a").click(function (e) {
            e.preventDefault();
            jQuery(this).toggleClass("invert").parent().next().toggleClass("disp-n");
            var testState = jQuery(this).hasClass("invert");
            if (testState) {
                jQuery(this).text("rozwiń");
            } else {
                jQuery(this).text("zwiń");
            }
        });

    if (jQuery('.featured')) {
        var slider;
        var current = jQuery('#articleimg0');
        jQuery('.featured .slide div').css({opacity:0,display:"block"});
        current.css({opacity: 1,display: "block"});

        var slideTo = function(pos,handler) {
            //slider = jQuery('.featured .slide').animate({'top':(-1 * pos * 282) + 'px'},200);
            if (current.attr('id') != ('articleimg'+pos)) {
                jQuery('#articleimg'+pos).fadeTo(200,1);
                current.fadeTo(200,0);
                current = jQuery('#articleimg'+pos);
                jQuery('.featured li').removeClass('active');
                handler.addClass('active');
            }
        };

        var elements = jQuery('.featured li').size();
        var currPos = 0;
        var autoSlide = setInterval(function() {
            if (currPos >= elements) {currPos = 0;}
            slideTo(currPos,jQuery('#article'+currPos));
            currPos++;
        },10000);

        
        jQuery('.featured li').mouseover(function() {
            if (slider) slider.stop();
            if (autoSlide) clearInterval(autoSlide);
            var pos = jQuery(this).attr('id').replace('article','');
            slideTo(pos,jQuery(this));
        });

    }

});

