function checkDocHeight() {
    var h1 = jQuery(window).height()+1;
    var h2 = jQuery('div#wrap').eq(0).height();
    jQuery('div#cont').css('height',(h1 > h2 ? h1 : h2)+'px');
}

function doSifr() {

    var clan = {
        src: sifrDir+'clan.swf',
        wmode: 'opaque'
    };

    sIFR.activate(clan);
    sIFR.initialize();

    jQuery('div#content h2').addClass('beforeSifr');

    sIFR.replace(clan, {
        selector: 'h2',
        css: [
            '.sIFR-root { background-color: #ffffff; color: #d80062; font-weight: normal; }',
            'a { text-decoration: none; }',
            'a:link { color: #d80062; }',
            'a:hover { color: #d80062; }'
        ]
    });

    checkDocHeight();
}

function init36i6() {

    doSifr();

    /** h4 scalable **/
    jQuery('div#content h4').each(function() {
        jQuery(this).html('<dl><dt>'+jQuery(this).html()+'</dt><dd></dd></dl>').addClass('custom');
    });
  	/** ie top nav fix **/
    if(jQuery.browser.msie) {
        jQuery('div#links ul').each(function() {
            jQuery(this).find('li:first').addClass('first');
        });
    }

    /** (mn) dynamic user menu **/
    var _dd = new Date();
    jQuery("div#session").load("/Uzytkownik/menu?_t=" + _dd.getTime(),function() {
	    if(jQuery.browser.msie) {
	        jQuery('div#session ul').each(function() {
	            jQuery(this).find('li:first').addClass('first');
	        });
	    }
		});;

    /** table construction **/
    jQuery('table').each(function() {
        var width = {};
        var row = 0;
        var col = 0;
        jQuery(this).after('<div id="temp"></div><div id="construct" class="tablecont"><div class="table"><div class="th"><div class="tn"><div></div></div><div class="headc"><div class="headi"><div class="head"></div></div></div><div class="ts"><div></div></div></div><div class="tbc"><div class="tb"></div></div></div></div>');
        jQuery(this).appendTo(jQuery('div#temp').eq(0));
        jQuery(this).show();
        jQuery(this).find('tr:first').find('td').each(function() {
            col++;
            jQuery('div#construct div.head').append('<div class="td td'+col+'"><span>'+jQuery(this).html()+'</span></div>');
            width[col] = parseInt(jQuery(this).outerWidth()+20);
        });
        jQuery(this).find('tr').not(jQuery(this).find('tr:first')).each(function() {
            row++;
            var cl = (row % 2 == 0 ? 'even' : 'odd');
            jQuery('div#construct div.tb').append('<div class="'+cl+'"></div>');
            var div = jQuery('div#construct div.tb div:last');
            jQuery(div).append('<div class="row"><div class="tp"><div class="tr"></div></div></div>');
            col = 0;
            jQuery(this).find('td').each(function() {
                col++;
                jQuery(div).find('div.tr').append('<div class="td td'+col+'"><span>'+jQuery(this).html()+'</span></div>');
            });
        });
        var last = jQuery('div#construct').find('div.row:last').parent();
        jQuery(last).addClass(jQuery(last).attr('class')+'last');
        var total = 0;
        var len = 0;
        for(i in width) {
            total += width[i]+1;
            len++;
        }
        var max = 500;
        if(jQuery('div#banners').length == 0) max = 700;
        if(total > max) {
            total = 0;
            for(i in width) {
                var tw = Math.floor(max/len)
                width[i] = tw;
                total += tw+1;
            }
        } else {
            for(i in width) {
                total += 1;
            }
        }
        for(i in width) {
            jQuery('div#construct div.td'+i).css('width',(width[i])+'px');
        }
        jQuery('div#construct div.tr').each(function() {
            var max = 0;
            jQuery(this).find('div.td span').each(function() {
                var h = jQuery(this).height();
                if(h == 0) {
                    h = jQuery(this).parent().height()-2;
                }
                if(h > max) max = h;
            });
            jQuery(this).find('div.td span').each(function() {
                var h = jQuery(this).innerHeight();
                if(h == 0) {
                    h = jQuery(this).parent().height()-2;
                }
                if(h < max) {
                    var diff = max-h;
                    jQuery(this).css('padding-top',Math.floor(diff/2)+'px');
                    jQuery(this).css('padding-bottom',Math.ceil(diff/2)+'px');
                }
            });
        });
        if(jQuery.browser.msie) {
            w = total;
            if(window.location.href.indexOf('nomina') >= 0) { w += 1; }
            jQuery('div#construct').css('width',(w+20)+'px');
            jQuery('div#construct div.th').css('width',(w+4)+'px');
            jQuery('div#construct div.even').css('width',w+'px');
            jQuery('div#construct div.odd').css('width',w+'px');
            jQuery('div#construct div.even').css('float','none');
            jQuery('div#construct div.odd').css('float','none');
            jQuery('div#construct div.even').css('display','block');
            jQuery('div#construct div.odd').css('display','block');
            jQuery('div#content').addClass('width'+jQuery('div#content').eq(0).width());
        }
        jQuery('div#temp').remove();
        jQuery(this).show();
        jQuery('div#construct').removeAttr('id');
        checkDocHeight();
    });

    jQuery('div#content ol').each(function() {
        var ind = 0;
        jQuery(this).find('li').each(function() {
            ind++;
            jQuery(this).html('<span class="no">'+ind+'</span><p>'+jQuery(this).html()+'</p>');
        });
        jQuery(this).addClass('custom');
    });

    /* paging */
    jQuery.fn.pageList = function(atPage,extraMargin) {
        return this.each(function() {
            var ul = this;
            var page = 0;
            jQuery(this).after('<div class="pager"><ol><li class="prev"><a href="#">&laquo;</a></li><li class="selected"><a href="#">1</a></li></ol></div>');
            var pager = jQuery(this).next().find('ol').eq(0);
            jQuery(this).find('li').each(function() {
                jQuery(this).appendTo(ul);
                page++;
                if(page % atPage == 0) {
                    jQuery(ul).after('<ul></ul>');
                    jQuery(pager).append('<li><a href="#">'+((page/atPage)+1)+'</a></li>');
                    ul = jQuery(ul).next();
                }
            });
            if(jQuery(ul).children().length == 0) {
                jQuery(ul).remove();
                jQuery(pager).find('li:last').remove();
            }
            jQuery(this).removeClass('hidden');
            if(jQuery(this).siblings('ul').length == 0) {
                jQuery(pager).remove();
            } else {
                ul = this;
                jQuery(this).css('height',jQuery(this).innerHeight()+'px').siblings('ul').css('height',jQuery(this).innerHeight()+'px');
                jQuery(ul).siblings('ul').hide();
                jQuery(pager).append('<li class="next"><a href="#">&raquo;</a></li>');
                jQuery(pager).find('a').click(function() {
                    if(jQuery(this).parent().attr('class').indexOf('prev') >= 0) {
                        jQuery(pager).find('li.selected').prev('li').not('li.prev').find('a').click();
                    } else if(jQuery(this).parent().attr('class').indexOf('next') >= 0) {
                        jQuery(pager).find('li.selected').next('li').not('li.next').find('a').click();
                    } else if(jQuery(this).parent().attr('class').indexOf('selected') < 0) {
                        var no = parseInt(jQuery(this).text());
                        ul = jQuery(ul).parent().find('ul').eq(no-1);
                        jQuery(ul).show().siblings('ul').hide();
                        jQuery(pager).find('li.selected').removeClass('selected');
                        jQuery(this).parent().addClass('selected');
                    }
                    return false;
                });
                jQuery(pager).parent().css('width',(jQuery(pager).parent().outerWidth()+extraMargin)+'px');
                jQuery(pager).parent().addClass('centered');
            }
        });
    };

    setTimeout(function() { jQuery('div#gallery ul').pageList(8,20); jQuery('div.products ul').not(jQuery('div#gallery ul')).pageList(6,20); checkDocHeight(); },150);
    checkDocHeight();

    jQuery(window).resize(function() { checkDocHeight(); });

    var gal = jQuery('div#gallery').eq(0);
    jQuery(gal).find('li a').click(function() {
        jQuery(gal).find('div#photo').remove();
        if(jQuery(this).parents('div.pager').length > 0) { jQuery(gal).find('ul li').show(); return true; }
        jQuery(gal).append('<img id="temp" src="'+jQuery(this).attr('href')+'" style="display:none" />');
        jQuery('img#temp').load(function() {
            jQuery(gal).find('li').hide();
            var w = jQuery(this).width();
            var h = jQuery(this).height();
            jQuery(gal).prepend('<div id="photo"><div class="top"><div></div></div><div class="center"><div style="width:'+(w-5)+'px;height:'+(h-16)+'px"></div></div><div class="bottom"><div></div></div><a class="x">cofnij</a></div>');
            jQuery(gal).find('div#photo').css('background-image','url('+jQuery(this).attr('src')+')');
            jQuery(this).remove();
            jQuery(gal).find('div#photo').css('margin-left','-'+Math.floor(w/2)+'px').click(function() {
                jQuery(this).remove();
                jQuery(gal).find('li').show();
            });
            if(jQuery.browser.msie) {
                var w = jQuery('div#photo div.center').width()-5;
                jQuery('div#photo div.top').css('width',w+'px');
                jQuery('div#photo div.bottom').css('width',w+'px');
            }
        }).error(function() {
            jQuery(this).remove();
            jQuery(gal).find('li').show();
        });
        return false;
    });


    jQuery('#menu a[href$="/taryfa-piszmow-dobowy/"]').prepend('<span class="nowa">NOWA</span><br />');
    jQuery('#menu a[href$="/taryfa-36i6/"].selected').prepend('<span class="nowa" /><br />');
    if (jQuery('#menu a[href$="/oferta/"]').parent().hasClass('selected')
    		&& !jQuery('#menu a[href$="/taryfa-piszmow-dobowy/"]').parent().hasClass('selected')
    		&& !jQuery('#menu a[href$="/taryfa-36i6/"]').parent().hasClass('selected')
    	) {
    	jQuery('div.bottom').css('bottom', '-12px'); //another strange IE bug
    }
    jQuery('p.selected div.hide').css('padding-bottom', '0');

    /** Dodanie loga bezendu **/
    /*
    if (jQuery('#menu a[href$="/taryfa-piszmow-dobowy/"]').parent().hasClass("selected") && jQuery("#pageTitle>img").length == 0) { //dodaj logo bezendu, jeżeli nie ma
    	var suff = '';
    	if (jQuery('#menu a[href$="/cyberarena36i-497/"]').hasClass("selected")) {
			suff = '_s';
		}
    	jQuery("#pageTitle").append('<img alt="BezEndu" src="themes/36i6/images/logo_bezendu' + suff + '.gif" class="bezendu' + suff + '">');
    }
    */

}


var flashPlayers = {};
var currentFlash = null;

function getMovieName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}

function registerPlayer(id,name) {
        var fp = getMovieName(name);
    flashPlayers[id] = fp;
    checkDocHeight();
}

function play(id) {
    if(flashPlayers[currentFlash] != null) {
        if(currentFlash != id) {
            var fp = flashPlayers[currentFlash];
                        try{
                fp.zatrzymaj();
            }catch(err) {}
        }
    }
    currentFlash = id;
}

/*
 * facebook funpage
 */
function facebookCnt(){
	var hoverTimeout, self;
	jQuery('#facebook').hover(
		function(){
			if(hoverTimeout){
				clearTimeout(hoverTimeout);
			}
			jQuery(this).animate({
				left: '0'
			});
		},
		function(){
			self = this;
			hoverTimeout = setTimeout(function(){
				jQuery(self).animate({
					left: '-198'
				})
			},500);
		}
	);
}
jQuery(document).ready(function() {
	facebookCnt();
});
