$(document).ready(function() {
	
	/* PROMOTED */
	$("#myController").jFlow({
		slides: "#slides",
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		auto: false,		//auto change slide, default true
		width: "538px",
		height: "100px",
		duration: 400,
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	});
	
	/* SEARCH */
	$(".search_bt").live('click', function() {
		var phrase = $('input.search_input').val();
		if (phrase != '' && phrase.length > 1) {
			location.href = FULL_URL+'/szukaj/'+phrase;
		} else {
			alert('Podaj wyszukiwaną frazę');
		}
	});	

	$("input.search_input").autocomplete("?search");
	
	
	/* QTIP */
	
	$('.qt[title]').qtip({
		style: {
			classes: 'ui-tooltip-green'
		}
	});
		
	/* ADD2CART LIST*/
	
	function getOpts() {
		var result = new Array();
		var opts = $('div#product_options input[name="opts[]"]:checked');
		if (opts && opts.length > 0) {
			$.each(opts, function(k, v) {
				result.push($(opts[k]).val());
			});
		}
		return result;
	}
	
	$('[id^=pid]').click(function() {
		// id produkt
		var pid = this.id.split('pid');
		pid = pid[1];		
		if (pid > 0) {
			// start
			$('div.cart_details').hide();
			$('div.cart_icon').hide();
			$('div#cart_load').show();
			// opcje
			var opts = getOpts();
			// dodanie produktu
			var cart = new Cart();
			var result = cart.add(pid, opts);
			if (result.status) {			
				// komunikat
				$(this).qtip({
					content: {
						text: 'Produkt &quot;'+result.product.nazwa+'&quot; został dodany do koszyka'
					}				
				}).qtip('show');
				// aktualizacja koszyka
				$('.cart_details span#cart_count').html(result.cart.ile);
				$('.cart_details span.price').html(result.cart.razem+' zł');
				result = null;
			};
			result = null;
			cart = null;
			// end
			$('div#cart_load').hide();
			$('div.cart_details').show();
			$('div.cart_icon').show();		
		}
	});
	
	/* BUY BTN */
	/*
	

	$.updateCart = function() {
		$.get('?getCart', function(data) {
			$('.cart_details span#cart_count').html(data.ile);
			$('.cart_details span.price').html(data.razem+' zł');				
		});
	}

	$('[id^=pid]').click(function(){
		$('div.cart_details').hide();
		$('div.cart_icon').hide();
		$('div#cart_load').show();
		var pid = this.id;
		pid = pid.split('pid');
		pid = pid[1];
		
		var buy_btn = $(this);
		var bubble_popup_id = buy_btn.GetBubblePopupID();		

		$.get('?getProductName='+pid, function(data){
			
			$.get('?addToCart='+pid, function(cart) {
				//console.log(cart);
				$.updateCart();
			});			
			
			product = data;
			buy_btn.ShowBubblePopup({
				position: 'top',
				align: 'center',
				tail: {align: 'center'},
				innerHtml: '<br /><p>Produkt <b>'+product.nazwa+'</b> został dodany do koszyka!</p><br />',					   
				innerHtmlStyle:{ 
					color:'#000', 
					'text-align':'center'
				},			   
				themeName: 	'azure',
				themePath: 	PATH_GFX+'/jquerybubblepopup-theme'
			}, false); 			
		});		
		$('div#cart_load').hide();
		$('div.cart_details').show();
		$('div.cart_icon').show();
	});
	*/
	$('#sel_products_sort').live('change', function() {
		alert(this.value);
	});
	
	/* IMG ZOOM */	
	$('div.product_img_big a').lightBox({
		//overlayBgColor: '#FFF',
		//overlayOpacity: 0.6,
		imageLoading: 	PATH_GFX+'/ajaxLoading.gif',
		imageBtnClose: 	PATH_GFX+'/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: 	PATH_GFX+'/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: 	PATH_GFX+'/lightbox/lightbox-btn-next.gif',
		imageBlank: 	PATH_GFX+'/lightbox/lightbox-blank.gif',
		//containerResizeSpeed: 350,
		txtImage: 'Zdjęcie',
		txtOf: 'nr'
	});	
  
	/* newsletter */	
	$('input.newsletter_input').click(function() {
		$(this).focus();
	});
	
	$('input.newsletter_input').focus(function() {	
		if ($(this).val() == 'Twój email') {
			$(this).val('');
		}
	});
	
	$('input.newsletter_input').blur(function() {	
		if ($(this).val() == '') {
			$(this).val('Twój email');
		}
	});
	
	$('a.join').live('click', function() {
		var email = $('input.newsletter_input').val();
		if (email != '' && email != 'Twój e-mail') {
			$.post('?newsletter', {
				'email': email,
				'action': 'dodaj'
			}, function(result) {
				//console.log(result);
				if (result.status == true && result.errors == null) {
					alert('Twój e-mail został dodany do naszej bazy');
					$('input.newsletter_input').val('Twój email');
				} else {
					alert('Podany adres e-mail istnieje w naszej bazie lub jest nieprawidłowy');
					$('input.newsletter_input').val(result.values.email);
				}
			});
		} else {
			alert('Podaj adres e-mail');
		}
	});
});

/* validation */
function markErrors(e) {
	if (e && e.length > 0) {
		$.each(e, function(k,v) {
			$('#'+v).addClass("border_error");
		});
	}
}
