function autoHeightAndWidth(obj,h,w){
    $(obj).each( function(){
        if($(this).height() > h && h > 0){
            $(this).height(h);
        }
        if($(this).width() > w && w > 0){
            $(this).width(w);
        }
    });
}

function addBorderDivs(objName,addPrefix,addClass){
    var udnefined;
    if ((addPrefix != udnefined) && (addClass != udnefined)){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'t '+addClass+'"><div class="'+addPrefix+'b '+addClass+'"><div class="'+addPrefix+'r '+addClass+'"><div class="'+addPrefix+'l '+addClass+'"><div class="'+addPrefix+'tl '+addClass+'"><div class="'+addPrefix+'tr '+addClass+'"><div class="'+addPrefix+'bl '+addClass+'"><div class="'+addPrefix+'br '+addClass+'"></div></div></div></div></div></div></div></div>');
        });
    }else if (addClass != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="t '+addClass+'"><div class="b '+addClass+'"><div class="r '+addClass+'"><div class="l '+addClass+'"><div class="tl '+addClass+'"><div class="tr '+addClass+'"><div class="bl '+addClass+'"><div class="br '+addClass+'"></div></div></div></div></div></div></div></div>');
        });
    }else if (addPrefix != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'t"><div class="'+addPrefix+'b"><div class="'+addPrefix+'r"><div class="'+addPrefix+'l"><div class="'+addPrefix+'tl"><div class="'+addPrefix+'tr"><div class="'+addPrefix+'bl"><div class="'+addPrefix+'br"></div></div></div></div></div></div></div></div>');
        });
    }else{
        $(objName).each(function(){
            $(this).wrap('<div class="t"><div class="b"><div class="r"><div class="l"><div class="tl"><div class="tr"><div class="bl"><div class="br"></div></div></div></div></div></div></div></div>');
        });
    }
}

function addLeftAndRightDiv(objName,addPrefix,addClass) {
    var udnefined;
    if ((addPrefix != udnefined) && (addClass != udnefined)){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'l '+addClass+'"><div class="'+addPrefix+'r '+addClass+'"></div></div>');
        });
    }else if (addClass != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="l '+addClass+'"><div class="r '+addClass+'"></div></div>');
        });
    }else if (addPrefix != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'l"><div class="'+addPrefix+'r"></div></div>');
        });
    }else{
        $(objName).each(function(){
            $(this).wrap('<div class="l"><div class="r"></div></div>');
        });
    }
}
function addLeftAndCenterDiv(objName,addPrefix,addClass) {
    var udnefined;
    if ((addPrefix != udnefined) && (addClass != udnefined)){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'c '+addClass+'"><div class="'+addPrefix+'l '+addClass+'"></div></div>');
        });
    }else if (addClass != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="c '+addClass+'"><div class="l '+addClass+'"></div></div>');
        });
    }else if (addPrefix != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'c"><div class="'+addPrefix+'l"></div></div>');
        });
    }else{
        $(objName).each(function(){
            $(this).wrap('<div class="c"><div class="l"></div></div>');
        });
    }
}
function addTopAndBottomDiv(objName,addPrefix,addClass) {
    var udnefined;
    if ((addPrefix != udnefined) && (addClass != udnefined)){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'t '+addClass+'"><div class="'+addPrefix+'b '+addClass+'"></div></div>');
        });
    }else if (addClass != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="t '+addClass+'"><div class="b '+addClass+'"></div></div>');
        });
    }else if (addPrefix != udnefined){
        $(objName).each(function(){
            $(this).wrap('<div class="'+addPrefix+'t"><div class="'+addPrefix+'b"></div></div>');
        });
    }else{
        $(objName).each(function(){
            $(this).wrap('<div class="t"><div class="b"></div></div>');
        });
    }
}
function addCartFormInner() {
    var objCartFormHTML = $('#cartForm').html();
    $('#cartForm').html('');
    $('#cartForm').append('<div id="cartForm-"/>');
    $('#cartForm-').html(objCartFormHTML);
    objCartFormHTML = undefined;
	
}
function equalHeights(strObj){
    var numHeight = 0;
    $(strObj).each(function(){
        if($(this).height() > numHeight){
            numHeight = $(this).height();
        }
    });
    $(strObj).each(function(){
        $(strObj).height(numHeight);
    });
}

$(document).ready(function(){
    var numIndex = 0;
    $("input.autocomplete").each( function () {
        $(this).autocomplete('smartsearch.php', {
            minChars: 2,
            max: 50,
            width: $(this).width(),
            selectFirst: false
        } );
    });

    $("#taby").idTabs(function(id,list,set){
        $("a",set).removeClass("selected")
        .filter("[href='"+id+"']",set).addClass("selected");
        for(i in list)
            $(list[i]).hide();
        $(id).fadeIn();
        $('.tabContent .va img').each(function(){
            var h = 150 , w = 150;
            if($(this).height() > h && h > 0){
                $(this).height(h);
            }
            if($(this).width() > w && w > 0){
                $(this).width(w);
            }
        });
        return false;
    });
	
    //Start Sortowanie
    if($('#c2 table.ctable_sort').html() != undefined){
        $('#c2 table.ctable_sort').wrap('<div id="cartForm"/>').addClass('top_text');
        var objCtableSortTdInnerHTML = $('#c2 table.ctable_sort td.ctable_sort_td table').html();
        $('#c2 table.ctable_sort td table').html('').remove();
        var objCtableSortTdHTML = $('#c2 table.ctable_sort td.ctable_sort_td').html();
        $('#c2 table.ctable_sort td.ctable_sort_td').remove();
        addCartFormInner();
        $('#c2 table.ctable_sort').remove();
        $('#cartForm-').append('<div class="top_text"></div>');
        $('#cartForm- div.top_text').append(objCtableSortTdHTML);
        $('#cartForm- div.top_text br').remove();
        $('#cartForm-').append('<table class="ctable_sort"/>');
        $('#cartForm- table.ctable_sort').append(objCtableSortTdInnerHTML);
        numIndex = 0;
        $('#cartForm- table.ctable_sort td').each(function(){
            $(this).attr('style','').attr('id','ctableSortTd-'+numIndex);
            numIndex++;
        });
        numIndex = 0;
        $('#cartForm- table.ctable_sort tr').each(function(){
            $(this).attr('style','').attr('id','ctableSortTr-'+numIndex);
            numIndex++;
        });
        numIndex = undefined;
        $('#c2 div.search_notice').appendTo('#cartForm-');
    }
    //End Sortowanie
    //Start login
    if($('#c2 .login').html() != undefined){
        var objCartFormHTML = $('#cartForm').html();
        $('#cartForm').html('');
        addCartFormInner();
        $('#c2 div.top_text').appendTo('#cartForm-')
        $('#cartForm-').append(objCartFormHTML);
        objCartFormHTML = undefined;
    }
    //End login
    //Start szukanie
    if($('#c2 form').attr('name') == 'searching'){
        $('#c2 form').attr('id','cartForm');
        addCartFormInner();
        var cartFormHTML = $('#cartForm-').html();
        $('#cartForm-').html('');
        $('#c2 div.top_text').appendTo('#szukaj #cartForm-');
        $('#cartForm-').append(cartFormHTML);
        addLeftAndCenterDiv('.sendform_long', 'btn-');
        var paddingLeft =  parseInt($('#szukaj .sendform_long').css('paddingLeft'));
        var paddingRight =  parseInt($('#szukaj .sendform_long').css('paddingRight'));
        $('#szukaj .btn-c').width($('.sendform_long').width()+paddingLeft+paddingRight).css({
            'margin':'0 auto'
        });
        paddingLeft = paddingRight = cartFormHTML = undefined;
    }
    //End szukanie
    //Start szukanie
    if($('#c2 form').attr('name') == 'order_form'){
        addCartFormInner();
        cartFormHTML = $('#cartForm-').html();
        $('#cartForm-').html('');
        $('#c2 div.top_text').appendTo('#cartForm-');
        $('#cartForm-').append(cartFormHTML);
        addLeftAndCenterDiv('#cartForm- input.sendform_long', 'btn-');
        paddingLeft =  parseInt($('#cartForm- input.sendform_long').css('paddingLeft'));
        paddingRight =  parseInt($('#cartForm- input.sendform_long').css('paddingRight'));
        $('#cartForm- div.btn-c').width($('#cartForm- input.sendform_long').width()+paddingLeft+paddingRight).css({
            'margin':'0 auto'
        });
        paddingLeft = paddingRight = cartForm = undefined;
    }
    //End szukanie
    //fix for cycle in da promotion box
    window.onload = function() {
        var numHeight = 0;
        $('#ps .promotions.menu.box').each(function (){
            
            if(($(this).height()) > numHeight){
                numHeight = $(this).height();
            }
        });
        $('#slajdy-promocje').height(numHeight+20);
    }
    //tu troszkę zamiszania - zmiana HTML'a w koszyku
    //dodanie paru atrybutów i elementów <div/>
    if(($('#c2 form').attr('name') == 'cart') || ($('#c2 form').attr('name') == 'order_details')){
        $('#c2 form').attr('id','cartForm');
        addCartFormInner();
    }
    if($('#c2 div.top_text').html() != undefined){
        var objTop_textHTML = $('.top_text').html();
        $('#c2 div.top_text').html('').append('<h2/>');
        $('#c2 div.top_text h2').html(objTop_textHTML);
    }
    $("#cartForm .cart_order").wrap('<div id="cartOrder-"/>');
    $('#cartForm- .cart_order').before('<div class="extraDivCart"/>');
    addBorderDivs('.extraDivCart','cartHeadings_');
    //addBorderDivs('#cartForm-','prd_');
    addBorderDivs('.empty_shopcart','prd_');
    if($('.empty_shopcart').html() != undefined){
        var objEmptyShopcartHTML = $('.empty_shopcart').html();
        $('.empty_shopcart').html('');
        $('#c2 div.top_text').appendTo('.empty_shopcart');
        $('.empty_shopcart').append(objEmptyShopcartHTML);
    }
    $('.cartHeadings_t').height($('.cartTopTableHeadings').height());
    $('.extraDivCart').height($('.cartTopTableHeadings').height());
    $('.ctableTd_miniImage .img_t').width($('.ctableTd_miniImage img').width() + 2);
    $('.cart_control_links- img').each(function() {
        $(this).wrap('<div class="controlLinksWrapper"/>')
    });
    if(($('#cartForm').html() != undefined) && ($('#wyniki_szukania').html() == undefined)){
        $('.backward').appendTo('#c2');
    }
	
    if($('.productDetails').html() != undefined){
        numIndex = 1;
        $('.productDetails').each(function(){
            $(this).attr('id','prdDetItem-'+numIndex);
            numIndex++;
        });
    }
    if($('.other').html() != undefined){
        numIndex = 1;
        $('.other').each(function(){
            $(this).attr('id','otherItem-'+numIndex);
            numIndex++;
        });
    }

    if($('.relocate').html() != undefined){
        numIndex = 1;
        $('.productDetails').each(function(){
            $('#prdDetItem-'+numIndex+' .relocatePrice .bruttoPrice').appendTo('#prdDetItem-'+numIndex+' .relocatePrice');
            $('#prdDetItem-'+numIndex+' .relocateTotal .bruttoTotal').appendTo('#prdDetItem-'+numIndex+' .relocateTotal');
            numIndex++;
        });
        numIndex = 1;
        $('.other').each(function(){
            $('#otherItem-'+numIndex+' .relocateOther .bruttoOther').appendTo('#otherItem-'+numIndex+' .relocateOther');
            numIndex++;
        });
        numIndex = undefined;
        $('.relocateSummary .summaryBrutto').appendTo('.relocateSummary');
    }
	
    if($('.cart_control_links').html() != undefined){
        numIndex = 1;
        $('.cart_control_links-').each(function(){
            $(this).attr('id','cart_control_links_item-'+numIndex);
            numIndex++;
        });
        numIndex = undefined;
    }
    if ($('#dataTable table').html() != undefined) {
        numIndex = 1;
        $('#dataTable table').each(function() {
            $(this).attr('id','dataTableItem-'+numIndex);
            numIndex++;
        });
        numIndex = undefined;
    }
    $('#post_code_wys').width($('#city_wys').width());
    //koniec modyfikacji koszyka
    addBorderDivs('.prd_list_ul .va-wrap','img_','fl');
    addBorderDivs('.menu-box .menubox','img_',undefined);
    addBorderDivs('#prd','prd_');
    addBorderDivs('#prd .prd_img','img_',undefined);
    addBorderDivs('#mycarousel img','img_','alignCenter');
    addBorderDivs('.ctableTd_miniImage img','img_');
    addBorderDivs('.no_products','prd_','tab');
    addBorderDivs('.no_coment','prd_','tab');
    addBorderDivs('#tabs #komentarze table table','prd_');
    addBorderDivs('div.input_amount','img_');
    autoHeightAndWidth('.prd_list_ul .va-wrap img',150,150);
    autoHeightAndWidth('#prd .va-wrap img',235,235);
    autoHeightAndWidth('#mycarousel img',70,70);
    addLeftAndRightDiv('#taby a');

    //hover effect w listach produktów - wyjeżdżający opis
    $('.boxgrid.captionfull .cnt').hover(function(){
        $(".cover", this).stop().animate({
            left:'0'
        },{
            queue:false,
            duration:160
        });
    }, function() {
        $(".cover", this).stop().animate({
            left:'325px'
        },{
            queue:false,
            duration:160
        });
    });
    $('a.reg').wrap('<div class="btn-c reg"><div class="btn-l"></div></div>');
    $('#submit_login').wrap('<div class="btn-c log-sub"><div class="btn-l"></div></div>');
    $('input.sendform').wrap('<div class="btn-c sendform"><div class="btn-l"></div></div>');
    $('ol.menubox_small_font').css({
        'margin':'0 0 20px 0'
    });
    $('ol.menubox_small_font a').hover(
        function(){
            $(this).css({
                'color':'#585858'
            });
        },
        function(){
            $(this).css({
                'color':'#000'
            });
        }
        );
    $('ol.menubox_small_font a').css({
        'color':'#000'
    });
    $('li.menubox_small_font').css({
        'padding-bottom':'0',
        'margin':'0 0 1px'
    });
    if ($('.box-1').html() != null) {
        $('.box-1').html($('.box-1').html().replace(/div/g,'ul'));
    }
    if ($('.box-55').html() != null) {
        $('.box-55').html($('.box-55').html().replace(/div/g,'ul'));
    }
    $('#klienci-kupili-rowniez td[style]').attr('style','');
    if ($('.no_coment').html() != null) {
        $('.no_coment').html($('.no_coment').html().replace(/Ten.produkt.nie.posiada.jeszcze.komentarzy/g,'<p>Ten produkt nie posiada jeszcze komentarzy</p>'));
    }
    if (($('#reklama').html() == null) || ($('#reklama').html() == '')){
        $('.banner.t').remove();
    }

    //dodaje klasy do tabelki w widoku konta użytkownika
    $('.my_account').siblings('table').addClass('article_text account user');
    //Obsługa paginacji
    $('.pag-btns').each(function() {
        $(this).html($(this).html().replace(/\|/g,'<a class="rozdzielacz">...</a>'));
        $(this).html($(this).html().replace(/\&nbsp\;/g,''));
    });
    var intPaginationMax = $('.pag-btns a:last-child u').text();
    intPaginationMax = intPaginationMax.replace(/^./,'');
    var activePaginationButton = 0;
    if(document.getElementsByTagName('b').length > 0){
        activePaginationButton = document.getElementsByTagName('b')[0].firstChild.nodeValue;
        if(activePaginationButton != null){
            activePaginationButton = activePaginationButton.replace(/^\[\s/,'');
            activePaginationButton = parseInt(activePaginationButton.replace(/\s\]$/,''));
        }
    }
    $('.pagination .btns b').wrap('<a class="current" />').html(activePaginationButton);
    intPaginationMax = activePaginationButton = undefined;
    var strHref = $('.pagination .prv').attr('href');
    if (/site_no=0$/.test(strHref)){
        $('.pagination .prv').attr('href',strHref.replace(/site_no=0$/,'site_no=1'));
    }
    strHref = undefined;
    //koniec obsługi paginacji
    //obsługa slajdów w menubox promocje
    $('#slajdy-promocje').cycle({
        fx:     'fade',
        timeout: 5000
    });
    if($('.login').html() != undefined){
        var paddingLeft = parseInt($('.input .sendform_long').css('paddingLeft'));
        var paddingRight = parseInt($('.input .sendform_long').css('paddingRight'));
        $('.input .btn-c').width($('.input .sendform_long').width()+paddingLeft+paddingRight).css({
            'margin':'0 auto'
        });
        paddingLeft = paddingRight = objCartFormHTML = undefined;
    }
    //START Site map
    if($('.top_text h2').html() == 'Mapa strony'){
        numIndex = 0;
        $('#c2 table').each(function(){
            $(this).addClass('siteMapTable-'+numIndex);
            numIndex++;
        });
        var objTopText = $('.top_text');
        var objSiteMap = $('.siteMapTable-0');
        var objBackward = $('.backward');
        $('.backward').remove();
        $('.siteMapTable-0').addClass('siteMap').attr('width','').attr('align','center');
        $('.top_text').remove();
        $('.siteMapTable-0').remove();
        $('#c2').append('<div id="cartForm"><div id="cartForm-"></div></div>');
        $('#cartForm-').append(objTopText);
        $('#cartForm-').append(objSiteMap);
        $('#c2').append(objBackward);
        objTopText = objSiteMap = objBackward = undefined;
    }
    //END Site map
    //START Artykuły
    if($('.article_text').html() != undefined){
        $('.article_text').wrap('<div id="cartForm"><div id="cartForm-"></div></div>');
        numIndex = 0;
        for(numIndex;numIndex <= 6;numIndex++){
            $('.article_text h'+numIndex).append('<div class="headerBg"/>');
        }
        numIndex = undefined;
    }
    //END Artykuły

    if($('#zagiel a').html() != undefined){
        $('#zagiel a').append('<p>Oblicz raty</p>');
    }
    if($('.top_text h2').html() == 'Promocje'){
        $('#c2').addClass('promocje');
        $('.top_text').attr('id','delete');
        $('#cartForm .top_text').attr('id','');
        $('#delete').remove();
    }
    if($('.top_text h2').html() == 'Nowo\u015bci'){
        $('.top_text').wrap('<div id="cartForm"><div id="cartForm-"></div></div>');
    }
    if($('#cartForm-').html() != undefined){
        addBorderDivs('#cartForm-','prd_');
    }
    if($('#cartForm- .top_text a').html() != undefined){
        $('.ctable_sort td').each(function(){
            if($(this).html() == 'według nazwy produktu:'){
                $('#cartForm- .top_text h2').html('Sortowanie produktów:').css('margin','0');
            }
        });
        $('.ctable_sort td a').each(function(){
            if($(this).html() == '<u>wszyscy</u>'){
                $('#cartForm- .top_text h2').html('Filtrowanie produktów:').css('margin','0');
            }
        });
        
    }
    if($('#lista_produktow .top_text a').html() != undefined){
        var objBreadcrumbsAnchors = $('#lista_produktow .top_text h2').html();
        var numIndex = 0;
        $('.top_text').each(function (){
            $(this).addClass('id-'+numIndex);
            numIndex++;
        });
        $('.id-0').html('').attr('id', 'breadcrumbs');
        $('#breadcrumbs').removeClass('top_text id-0');
        $('#breadcrumbs').html(objBreadcrumbsAnchors);
        $('#cartForm').css('margin','0');
        numIndex = objBreadcrumbsAnchors = undefined;
    }
    if($('#prd_list.cat_card.show.categories').html() != undefined){
        $('.prd_list_ul').css('margin','0');
    }
    if($('#prd_list.cat_card.show.categories .decription p').html() == ''){
        $('#prd_list.cat_card.show.categories .decription').remove();
        $('#prd_list.cat_card.show.categories .cnt').css('background','transparent');
    }
    //START Tabela historii zamówień - konto użytkownika
    if($('.ctable_myaccount').html() != undefined){
        $('.ctable_myaccount').wrap('<div id=cartForm/>');
        addCartFormInner();
        $('#c2 div.top_text').appendTo('#cartForm-');
        $('.ctable_myaccount').appendTo('#cartForm-');
        $('.ctable_myaccount th').addClass('cartTopTableHeadings');
        $('.ctable_myaccount').wrap('<div class="myAccountHistoryTable"/>');
        $('.myAccountHistoryTable').append('<div class="extraDivCart"/>');
        addBorderDivs('.extraDivCart','cartHeadings_');
        addBorderDivs('#cartForm-','prd_');
        $('.cartHeadings_t').height($('.cartTopTableHeadings').height());
        $('.extraDivCart').height($('.cartTopTableHeadings').height());
    }
    //END Tabela historii zamówień - konto użytkownika
    //START Strona użytkownika
    if($('#c2 .my_account').html() != undefined){
        $('#c2 div.top_text').appendTo('#cartForm-');
        $('#c2 div.my_account').appendTo('#cartForm-');
        $('#c2 table.article_text').appendTo('#cartForm-');
    }
    //END Strona użytkownika
    //START zmiana hasła
    if($('#c2 .ctable_myaccount_small').html() != undefined){
        $('#c2 div.top_text + form').attr('id','cartForm');
        addCartFormInner();
        $('#c2 div.top_text').appendTo('#cartForm-');
        $('#c2 table.ctable_myaccount_small').appendTo('#cartForm-');
        var $sendform_long = $('#c2 table.ctable_myaccount_small tr td.text_center input.sendform_long');
        addLeftAndCenterDiv($sendform_long, 'btn-');
        var paddingLeft =  parseInt($sendform_long.css('paddingLeft'));
        var paddingRight =  parseInt($sendform_long.css('paddingRight'));
        $('.ctable_myaccount_small .btn-c').width($sendform_long.width()+paddingLeft+paddingRight).css({
            'margin':'0 auto'
        });
        paddingLeft = paddingRight = undefined;
        addBorderDivs('#cartForm-','prd_');
    }
    //END zmiana hasła
    var id= 0;
    $('.cntMnuAddSpan a').each(function(){
        var tmp = $(this).html();
        $(this).html('');
        $(this).append('<span class="'+id+'"/>');
        $('.cntMnuAddSpan a span.'+id).append(tmp);
        id++;
    });
    equalHeights('.list_Products_MainPage .boxgrid h3');
    equalHeights('.list_Products_MainPage .boxgrid');
    equalHeights('.list_Products_buyAlso .boxgrid h3');
    equalHeights('.list_Products_buyAlso .boxgrid');
    equalHeights('.list_Products_Default .boxgrid h3');
    equalHeights('.list_Products_Default .boxgrid');

    //my carousel fix
    if (!$('#mycarousel').has("li")) {
        $('#mycarousel').remove();
        $('div[class^="jcarousel"]').remove();
    }
    if (!($('#mycarousel li').html())){
        $('#mycarousel').remove();
        $('div[class^="jcarousel"]').remove();
        $('.product-info').css({
            'padding-top':'15px'
        });
    }
    if ( ($('#mycarousel').has("li")) || ($('#mycarousel li').html()) ){
        $('#mycarousel').jcarousel({
            vertical: false,
            scroll: 2
        });
    }
});

