$(document).ready(function(){
/* ------------------------------------------------------------------------------------------------------------- */
$("#tabs").tabs({ 
	cache: true
});
//$('#labelSearch').focus();
/* ------------------------------------------------------------------------------------------------------------- */	
//$("tbody tr:first-child, tr th:first-child, tr td:first-child, ul li:first-child, ol li:first-child").addClass("first");
//$("tbody tr:last, tr th:last, tr td:last, tr th:last-child, tr td:last-child, ul li:last, ol li:last").addClass("last");
$("ul li:first-child, ol li:first-child").addClass("first");
$("ul li:last, ol li:last").addClass("last");
/* ------------------------------------------------------------------------------------------------------------- */	
// --- datepicker pl ------------------------------------
$.datepicker.regional['pl'] = {
	closeText: 'Zamknij',
	prevText: '&laquo; Poprzedni',
	nextText: 'Następny &raquo;',
	currentText: 'Dziś',
	monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
	monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze','Lip','Sie','Wrz','Pa','Lis','Gru'],
	dayNames: ['Niedziela','Poniedzialek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
	dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
	dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
	dateFormat: 'yy-mm-dd', firstDay: 1,
	isRTL: false};
$.datepicker.setDefaults($.datepicker.regional['pl']);
// --- end datepicker pl ------------------------------------   

$(".datepicker").datepicker({
	showOn: 'button', 
	buttonImage: '/img/ico.calculator.gif', 
	buttonImageOnly: true,
	duration: 'fast' ,
	showAnim: 'fadeIn',
	showButtonPanel: true,
	showOn: 'both'
});		
/* ------------------------------------------------------------------------------------------------------------- */


$("#admin div.admin").hover(function(){
	$(this).addClass('adminHover');
  }, function(){
	$(this).removeClass('adminHover');
  }
);
/* ------------------------------------------------------------------------------------------------------------- */	
$("#header div.user dl dd").hover(function(){
	$(this).find("div.menu").show(100);
  }, function(){
	$(this).find("div.menu").hide(100);
  }
);
/* ------------------------------------------------------------------------------------------------------------- */
$('#admin input.btn').click(function() {
	var $action = $(this).attr("title");			
	if($(this).hasClass('deleteMsg')){
		var answer = 'Czy na pewno usunać?';
		if (confirm(answer)){
			if ($action != '' &&  $action != ' ') { 
				$(this).parents("form").attr("action", $action).submit();
			}
		} else {
			return false;
		}		
	} else {
		if ($action != '' &&  $action != ' ') { 
			$(this).parents("form").attr("action", $action).submit();
		}
	}
});
/* ------------------------------------------------------------------------------------------------------------- */	
$('input.delete, a.delete').click(function(){
  var answer = 'Czy na pewno usunać?';
  var title = $(this).attr('title');
	if (title != '' &&  title != ' ') { 
		if (confirm(title)) {
			return answerTitle;
		} else {
			return false;
		}
	} else {
		if (confirm(answer)){
			return answer;
		} else {
			return false;
		}			
	}
}); 
/* ------------------------------------------------------------------------------------------------------------- */	
	$("a[rel='gallery'], a[rel='thumbs']").colorbox({slideshow:true});
/* ------------------------------------------------------------------------------------------------------------- */	
		$('.btnChoose').click(function() {
			$(this).after('<div id="dialog"></div>');	
			var id_list_category = $('#id_category').val();
			var multiple = $('#id_category').attr('title');
			var onlyLeaf = $('#id_category').attr('alt');
			var isMain = $('#is_main').attr('name');
			var isMainVal = $('#is_main').val();																				
			if(isMain=='is_main'){														
				var url = this.href+'id_list_category='+id_list_category+'&multiple='+multiple+'&onlyLeaf='+onlyLeaf+'&isMain=1&isMainVal='+isMainVal;
			} else {
				var url = this.href+'id_list_category='+id_list_category+'&multiple='+multiple+'&onlyLeaf='+onlyLeaf;
			}
			var title = this.title;			  		
			$("#dialog").load(url, [], function(){
				$("#dialog").dialog("open");
			});
			$("#dialog").dialog({
				closeText: 'Zamknij',
				title: title,
				modal: true,
				bgiframe: true,
				autoOpen: true,
				height: 500,				
				minHeight: 300,
				maxHeight: 800,
				width: 980,
				minWidth: 980,
				draggable: true,
				resizeable: true,	
				zIndex: 200000,			
				buttons: { 
					"Zamknij": function() { 
						$(this).dialog("close");
						$('#dialog').remove();						 
					}
				}
			});
			return false;
		});	
/* ------------------------------------------------------------------------------------------------------------- */	
		$('.btnSearchProducer').click(function() {
			$(this).after('<div id="dialogSearchProducer"></div>');	
			var id_list_producer = $('#id_producer').val();
			var multiple = $('#id_producer').attr('title');			
			var url = this.href+'id_list_producer='+id_list_producer+'&multiple='+multiple;
			var title = this.title;			  		
			$("#dialogSearchProducer").load(url, [], function(){
				$("#dialogSearchProducer").dialog("open");
			});
			$("#dialogSearchProducer").dialog({
				closeText: 'Zamknij',
				title: title,
				modal: true,
				bgiframe: true,
				autoOpen: true,
				height: 500,				
				minHeight: 300,
				maxHeight: 800,
				width: 980,
				minWidth: 980,
				draggable: true,
				resizeable: true,	
				zIndex: 200000,	
				closeOnEscape: false,
				buttons: { 
					"Zamknij": function() { 
						$(this).dialog("close");
						$('#dialogSearchProducer').remove();						 
					}
				}
			});
			return false;
		});
/* ------------------------------------------------------------------------------------------------------------- */
	$('.BuyBt a').click(function() {		
		var $action = $(this).attr("rev");		
		if ($action != '' &&  $action != ' ') { 
			$(this).parents("form").attr("action", $action).submit();
		}
	});
/* ------------------------------------------------------------------------------------------------------------- */
	$('.FilterBox .Widok img').click(function() {		
		var $listType = $(this).attr("rev");		
		if ($listType != '' &&  $listType != ' ') { 
			$('#list_type').val($listType);
			if ($listType == 'list'){
				$(this).parents(".Widok").html('<div>Widok</div><div><img style="cursor:pointer" rev="list" src="/img/listaA.png"/></div><div><img style="cursor:pointer" rev="gallery" src="/img/galeriaNA.png"/></div><div class="Clr"></div>');
			};
			if ($listType == 'gallery'){
				$(this).parents(".Widok").html('<div>Widok</div><div><img style="cursor:pointer" rev="list" src="/img/listaNA.png"/></div><div><img style="cursor:pointer" rev="gallery" src="/img/galeriaA.png"/></div><div class="Clr"></div>');
			};
			$('#listFilter').submit();			
		}
	});
/* ------------------------------------------------------------------------------------------------------------- */
$('.numberonly').keyup(function(e){
	if(e.which==13) return false;
	$(this).val(parseInt($(this).val()));
	if ($(this).val()=='NaN')
	    $(this).val(1);
	if ($(this).val()<1)
		$(this).val(1);
	$(this).val(c);
});
$('input.changeInputNumber').wrap('<div class="changeInputNumber"></div>').after('<div class="changeInputNumberPlus">+</div><div class="changeInputNumberMinus">-</div>');		
$('div.changeInputNumber .changeInputNumberPlus').click(function(){
	var $tmp = $(this).parent('.changeInputNumber').find('input').attr('value');
	$tmp = parseInt($tmp)+1;
	$(this).parent('.changeInputNumber').find('input').attr('value',$tmp);
	$(this).parents("form").submit();
});	
$('div.changeInputNumber .changeInputNumberMinus').click(function(){
	var $tmp = $(this).parent('.changeInputNumber').find('input').attr('value');
	if ($tmp>1) {
		$tmp = parseInt($tmp)-1;
		$(this).parent('.changeInputNumber').find('input').attr('value',$tmp);		
		$(this).parents("form").submit();
	}
});

$('input.changeInputNumber').click(function(){
	var $tmpValue = $(this).attr('value');
	$(this).blur(function(){									   
		var $tmpValueBlur = $(this).attr('value');
		if($tmpValue!=$tmpValueBlur){
			$(this).parents("form").submit();
		}
	});
});
/* ------------------------------------------------------------------------------------------------------------- */	
$('.btnSelectAll').click(function(){
	$(this).parent('form').find('input:checkbox').attr('checked', 'checked');
});		
$('.btnUncheckAll').click(function(){
	$(this).parent('form').find('input:checkbox').attr('checked', '');
});	
/* ------------------------------------------------------------------------------------------------------------- */	
$('.MenuProducenci .seeAll').toggle(function() {
	$('.MenuProducenci .notselected').slideDown(200);
	$(this).html('<img src="/img/pokazWybr.png" >');
}, function() {
	$('.MenuProducenci .notselected').slideUp(200);
	$(this).html('<img src="/img/pokazWszy.png" >');
});

/* ------------------------------------------------------------------------------------------------------------- */
$("div.ProdListNormal div.Item div.Photo").hover(function(){	
	var $id = $(this).find("div.PhotoZoomBox").attr("id");
	if ($id != ''){ 							
		//$('#'+$id).toogle().fadeIn(250);		
		$('#'+$id).fadeIn(250).toogle();
	}
	return false;
}, function(){		
	var $id = $(this).find("div.PhotoZoomBox").attr("id");
	if ($id != ''){ 
		$('#'+$id).fadeOut(100).toogle();	
		//$('#'+$id).toogle().fadeOut(100);
	}
	return false;
});

/* ------------------------------------------------------------------------------------------------------------- */	
	var idBoxPromotion = ($(".PromotionsBox .PromoPhotoBox .PromoTabsBox div").length-1);
	if (idBoxPromotion>0) {
		var idBoxPromotion1 = idBoxPromotion + 1
		var rotateTime = 6000;
		var rotateTimeInterval = 18000;
		if (idBoxPromotion > 1) {
			function rotatePromotion() {
				var current = parseInt($('.PromotionsBox .PromoPhotoBox .PromoTabsBox div.PromoTabA').attr('rev'));
				var next = (current + 1) % idBoxPromotion1;
				if (0 == next) {
					next += 1;
				}
				$('.PromotionsBox .PromoPhotoBox .PromoTabsBox div').removeClass('PromoTabA');
				$('.PromotionsBox div.Promo'+next).addClass('active');
				$('.PromotionsBox div.Promo'+current).removeClass('active');
				$('.PromotionsBox .PromoPhotoBox .PromoTabsBox div.Promo'+next).addClass('PromoTabA');
				$('.PromotionsBox .PromoPhotoBox .PromoTabsBox div.Promo'+current).removeClass('PromoTabA');
				$('.PromotionsBox .PromoPhotoBox .PromoTabsBox div.Promo'+current).addClass('PromoTabNA');			
				window.clearInterval(promotionId);
				promotionId = window.setInterval(rotatePromotion, rotateTime);		
			}
			
				promotionId = window.setInterval(rotatePromotion, rotateTime);
			
			$('.PromotionsBox .PromoPhotoBox .PromoTabsBox div').click(function () {
				window.clearInterval(promotionId);
				promotionId = window.setInterval(rotatePromotion, rotateTimeInterval);
				var current = parseInt($(this).attr('rev'));
				$('.PromotionsBox .PromoPhotoBox .PromoTabsBox div').removeClass('PromoTabA');
				$('.PromotionsBox .PromoPhotoBox .PromoTabsBox .Tab').addClass('PromoTabNA');
				$(this).removeClass('PromoTabNA');
				$(this).addClass('PromoTabA');
				$('.PromotionsBox div.PromoTxtBox').removeClass('active');
				$('.PromotionsBox .PromoPhotoBox div.PromoPhoto').removeClass('active');
				$('.PromotionsBox div.Promo'+current).addClass('active')			
				return false;
			});
		};
	};

/* ------------------------------------------------------------------------------------------------------------- */	
	var idWidgetPromotion = $(".WidgetPromoPhotoBox div.WidgetPromoPhoto a").length;
	if (idWidgetPromotion>0) {
		var idWidgetPromotion1 = idWidgetPromotion + 1
		var rotateWidgetTime = 4000;		
		if (idWidgetPromotion > 1) {
			function rotateWidgetPromotion() {
				var current = parseInt($('.WidgetPromoPhotoBox div.active').attr('rev'));
				var next = (current + 1) % idWidgetPromotion1;
				if (0 == next) {
					next += 1;
				}				
				$('.WidgetPromoPhotoBox div.Promo'+next).addClass('active');
				$('.WidgetPromoPhotoBox div.Promo'+current).removeClass('active');							
				window.clearInterval(promotionWidgetId);
				promotionWidgetId = window.setInterval(rotateWidgetPromotion, rotateWidgetTime);		
			}			
				promotionWidgetId = window.setInterval(rotateWidgetPromotion, rotateWidgetTime);						
		};
	};

/* ------------------------------------------------------------------------------------------------------------- */

	var idBoxPartnerLogo = $(".MainLogos div.tabsnav ul.tabs-nav li").length;				
	var idBoxPartnerLogo1 = idBoxPartnerLogo + 1
	var rotateTimePartnerLogo = 6000;
	var rotateTimeIntervalPartnerLogo = 18000;		
	function rotatePartnerLogo() {			
		var current = parseInt($('.MainLogos div.tabsnav ul.tabs-nav li.active').text());			
		var next = (current + 1) % idBoxPartnerLogo1;
		if (0 == next) {
			next += 1;
		}
		$('.MainLogos div.tabsnav ul.tabs-nav li.PartnerLogos' + current).removeClass('active');
		$('.PartnerLogos' + next).addClass('active');								
		$('div.PartnerLogos' + current).removeClass('active');
		$('div.PartnerLogos' + next).addClass('active');						
		window.clearInterval(partnerLogoId);
		partnerLogoId = window.setInterval(rotatePartnerLogo, rotateTimePartnerLogo);		
	}		
	partnerLogoId = window.setInterval(rotatePartnerLogo, rotateTimePartnerLogo);

/* ------------------------------------------------------------------------------------------------------------- */
$('#widgetFacebook').mouseover(function(){
	$('#widgetFacebook').removeClass('widget_hide');
	$('#widgetFacebook').addClass('widget_show');						
});
$('#widgetFacebook').mouseout(function(){
	$('#widgetFacebook').removeClass('widget_show');
	$('#widgetFacebook').addClass('widget_hide');						
});	
/* ------------------------------------------------------------------------------------------------------------- */
$('#widgetPromotion').mouseover(function(){
	$('#widgetPromotion').removeClass('widgetPromotion_hide');
	$('#widgetPromotion').addClass('widgetPromotion_show');						
});
$('#widgetPromotion').mouseout(function(){
	$('#widgetPromotion').removeClass('widgetPromotion_show');
	$('#widgetPromotion').addClass('widgetPromotion_hide');						
});	
/* ------------------------------------------------------------------------------------------------------------- */
	
});


function printProduct(url){

    url   = "/print"+url;
	specs = "toolbar=no,directories=no,menubar=no,scrollbars=yes,width=700,height=700";
    open(url, "", specs);

}

function sendToPrinter(){
    
	$("#PrintButton").toggle();
	window.print();

}
