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);
    }
  });
  //alert(obj +' '+w+' '+h); 
}

function relocateObj(objName,objDestination){
	var objMessage = $(objName);
	if(!objMessage != null){
		$(objName).each(function(){
			$(this).remove();
		});
		$(objDestination).append(objMessage);
		return true;
	}
	return false;
}

function addBorderDivs(objName,addPrefix,addClass){
	var udnefined;
	if ((addPrefix != udnefined) && (addClass != udnefined)){
		$(objName).each(function(){
			//alert('oba');
			$(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(){
			//alert('oba');
			$(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(){
			//alert('oba');
			$(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(){
			//alert('oba');
			$(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(){

$("#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 .ctable_sort').html() != undefined){
        $('#c2 .ctable_sort').wrap('<div id="cartForm"/>').addClass('top_text');
        var objCtableSortTdInnerHTML = $('.ctable_sort_td table').html();
        $('.ctable_sort_td table').html('').remove();
        var objCtableSortTdHTML = $('.ctable_sort_td').html();
        $('.ctable_sort_td').remove();
        addCartFormInner();
        $('#c2 .ctable_sort').remove();
        $('#cartForm-').append('<div class="top_text"></div>');
        $('#cartForm- .top_text').append(objCtableSortTdHTML);
        $('.top_text br').remove();
        $('#cartForm-').append('<table class="ctable_sort"/>');
        $('#cartForm- .ctable_sort').append(objCtableSortTdInnerHTML);
        var numIndex = 0;
        $('#cartForm- .ctable_sort td').each(function(){
            $(this).attr('style','').attr('id','ctableSortTd-'+numIndex);
            numIndex++;
        });
        numIndex = 0;
        $('#cartForm- .ctable_sort tr').each(function(){
            $(this).attr('style','').attr('id','ctableSortTr-'+numIndex);
           numIndex++;
        });
        numIndex = undefined;
        relocateObj('.search_notice', '#cartForm-');
    }
	//End Sortowanie
	//Start login
	if($('.login').html() != undefined){
        var objCartFormHTML = $('#cartForm').html();
        $('#cartForm').html('');
        addCartFormInner();
        relocateObj('.top_text', '#cartForm-');
        $('#cartForm-').append(objCartFormHTML);
		objCartFormHTML = undefined;
	}
	//End login
	//Start szukanie
	if($('#c2 form').attr('name') == 'searching'){
		$('#c2 form').attr('id','cartForm');
		addCartFormInner();
		var cartForm = $('#cartForm-').html();
		$('#cartForm-').html('');
		relocateObj('.top_text', '#szukaj #cartForm-');
		$('#cartForm-').append(cartForm);
		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 = cartForm = undefined;
	}
	//End szukanie
	//Start szukanie
	if($('#c2 form').attr('name') == 'order_form'){
		addCartFormInner();
		var cartForm = $('#cartForm-').html();
		$('#cartForm-').html('');
		relocateObj('.top_text', '#cartForm-');
		$('#cartForm-').append(cartForm);
		addLeftAndCenterDiv('#cartForm- .sendform_long', 'btn-');
		var paddingLeft =  parseInt($('#cartForm- .sendform_long').css('paddingLeft'));
		var paddingRight =  parseInt($('#cartForm- .sendform_long').css('paddingRight'));
		$('#cartForm- .btn-c').width($('#cartForm- .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() {
		
	//if($.browser.safari){
		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($('.top_text').html() != undefined){
		var objTop_textHTML = $('.top_text').html();
		$('.top_text').html('');
		$('.top_text').append('<h2/>');
		$('.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('');
		relocateObj('.top_text','.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)){
		relocateObj('.backward','#c2');
	}
	
	if($('.productDetails').html() != undefined){
		var numIndex = 1;
		$('.productDetails').each(function(){
			$(this).attr('id','prdDetItem-'+numIndex);
			numIndex++;
		});
	}
	if($('.other').html() != undefined){
		var numIndex = 1;
		$('.other').each(function(){
			$(this).attr('id','otherItem-'+numIndex);
			numIndex++;
		});
	}

	if($('.relocate').html() != undefined){
		var numIndex = 1;
		$('.productDetails').each(function(){
				relocateObj('#prdDetItem-'+numIndex+' .relocatePrice .bruttoPrice','#prdDetItem-'+numIndex+' .relocatePrice');
				relocateObj('#prdDetItem-'+numIndex+' .relocateTotal .bruttoTotal','#prdDetItem-'+numIndex+' .relocateTotal');
				numIndex++;
		});
		var numIndex = 1;
		$('.other').each(function(){
				relocateObj('#otherItem-'+numIndex+' .relocateOther .bruttoOther','#otherItem-'+numIndex+' .relocateOther');
				numIndex++;
		});
		numIndex = undefined;
		relocateObj('.relocateSummary .summaryBrutto','.relocateSummary');
	}
	
	if($('.cart_control_links').html() != undefined){
		var numIndex = 1;
		$('.cart_control_links-').each(function(){
			$(this).attr('id','cart_control_links_item-'+numIndex);
			numIndex++;
		});
		numIndex = undefined;
	}
	if ($('#dataTable table').html() != undefined) {
		var 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();
	}

//Chmurki
/*$('#frm_z').hover(
		function(){
				$('.cloud.zapisz').css({'display':'block'});
			},
		function(){$('.cloud.zapisz').css({'display':'none'})}
);
$('#frm_w').hover(
		function(){
			$('.cloud.wypisz').css({'display':'block'});
		},
	function(){$('.cloud.wypisz').css({'display':'none'})}
);
$('#search_submit').hover(
		function(){
			$('.cloud.szukaj').css({'display':'block'});
		},
	function(){$('.cloud.szukaj').css({'display':'none'})}
);
$('#submit_login').hover(
		function(){
			$('.cloud.zaloguj').css({'display':'block'});
		},
	function(){$('.cloud.zaloguj').css({'display':'none'})}
);   */



//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'){
      var 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>');
      var 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();
      relocateObj('.top_text', '#cartForm-');
      relocateObj('.ctable_myaccount', '#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($('.my_account').html() != undefined){
        relocateObj('.top_text', '#cartForm-');
        relocateObj('.my_account', '#cartForm-');
        relocateObj('.article_text.account.user', '#cartForm-');
    }
    //END Strona użytkownika
    //START zmiana hasła
    if($('.ctable_myaccount_small').html() != undefined){
        $('.top_text + form').attr('id','cartForm');
        addCartFormInner(); 
        relocateObj('.top_text', '#cartForm-');
        relocateObj('.ctable_myaccount_small', '#cartForm-');
        addLeftAndCenterDiv('.sendform_long', 'btn-');
    		var paddingLeft =  parseInt($('.ctable_myaccount_small .sendform_long').css('paddingLeft'));
    		var paddingRight =  parseInt($('.ctable_myaccount_small .sendform_long').css('paddingRight'));
    		$('.ctable_myaccount_small .btn-c').width($('.ctable_myaccount_small .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++;
    });
    $(window).load(function(){
        //equalHeights('#special .list_Products_Special .boxgrid h3');
        //equalHeights('#special .list_Products_Special .boxgrid');
        
        equalHeights('#prd_list .prd_list_ul .boxgrid h3');
        equalHeights('#prd_list .prd_list_ul .boxgrid');
    });
});


jQuery(document).ready(function(){
  //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()) ){
		jQuery('#mycarousel').jcarousel({
	        vertical: false,
	        scroll: 2
		});
	}
});
