/// d. {

function popMeUpScotty(url, w, h, nm)
{
	if(typeof(w)=='undefined')
		w=600;

	if(typeof(h)=='undefined')
		h=600;

	if(typeof(nm)=='undefined')
		nm='lalapopup';

   w = window.open (url,
						  nm,
						  "location=0,status=0,scrollbars=1,width="+w+",height="+h+",toolbar=0,menubar=0,directories=0");
	//w.moveTo(0,0);
	w.focus();
}

function log(a)
{
	if(typeof(console)!='undefined' && typeof(console.log)!='undefined')
	{
		console.log(a);
	}
}

function setBackUrl(url)
{
	if(url!='')
	{
		$('#article_back').attr('href', url);
		$('#back_to_browsing').attr('href', url);
	}
}

var CartQuantity=
{
	increase: function(id)
	{
		var o=$('#kolicina_' + id);
		var v=o.val();
		v=parseInt(v);

		if(isNaN(v) || v<=0)
			v=1;
		else
			v++;

		o.val(v);
	}
};

var PNPCheckout=
{
	submit_pnpCalc: function(countryId)
	{
		countryId = parseInt(countryId);

		if(isNaN(countryId) || countryId<=0)
			return;

		fwajax.phpExecute(
				{url: '/xshop/pnp-calc'},
				countryId
			);
	},

	callback_pnpCalc: function(html)
	{
		if(html)
		{
			$('#checkout_2_pnp_container').html(html)
											.show();
		}
		else
		{
			$('#checkout_2_pnp_container').html(html)
											.hide();
		}
	}
};


var Cart=
{
	_attrSetsObj:null,

	init: function()
	{
		Cart._attrSetsObj=$('.attr_set');
	},

	submit_addMultiCart: function(prodIdArr)
	{
		var attrArray=new Array();
		fwajax.phpExecute(
					{url: '/xshop/cart-multi-add'},
					prodIdArr,
					0,
					attrArray,
					1
			);
	},

	submit_addCart: function(prodId)
	{
		//if(!Cart._attrSetsObj)
		//{
		//	log(1);
		//	return false;
		//}

		if(!Cart.checkSelected())
		{
			return false;
		}

		// ok dodajem u kosaricu
		var attrArr=[];
		Cart._attrSetsObj.find('a.active').each( function(ix)
													{
														attrArr[ attrArr.length ] = this.id;
													}
												   );

		var quantityObj=$('#quantity');
		var q = quantityObj.val();
		q=parseInt(q);


		fwajax.phpExecute(
					{url: '/xshop/cart_add'},
					parseInt(prodId),
					Cart._attrSetsObj.length,
					attrArr,
					q
			);

		return true;
	},


	callback_addCart:function(status, msg, cartMsg, total, totalQuantity, partErrorMsg)
	{
		if(typeof(partErrorMsg)=='undefined')
			partErrorMsg = '';

		//log( '('+status+')('+msg+')('+cartMsg+')('+total+')('+totalQuantity+')' );

		if(status)
		{
			var cartObj=$('#cart_lite');

			cartObj.html( cartMsg );

			if(totalQuantity>0)
				cartObj.addClass('filled');
			else
				cartObj.removeClass('filled');
		}

		GeneralNotification.show( status, msg, partErrorMsg );

		if(totalQuantity>0)
		{
			$('#shop_cart_lite').show();
		}
		else
		{
			$('#shop_cart_lite').hide();
		}

		$('.form_display_list input[type=checkbox][name^=product_sel]').removeAttr('checked');
		$('.form_display_list .select_all_check').removeAttr('checked');
	},

	checkSelected:function(onlyRemoveErrors)
	{
		//if(!Cart._attrSetsObj)
		//	return false;

		if(typeof(onlyRemoveErrors)=='undefined')
			onlyRemoveErrors=false;

		var quantityErrorCount=0;
		var unselectedCount=0;

		Cart._attrSetsObj.each( function(ix)
							  {
								var setObj=$(this);

								if( setObj.find('dd a.active').length < 1 )
								{
									// aha! nije odabran
									if( !onlyRemoveErrors )
									{
										Cart._markUnselected(setObj, true);
									}

									unselectedCount++;
								}
								else
								{
									Cart._markUnselected(setObj, false);
								}
							  }
							 );
		// provjeri kolicinu
		var quantityObj=$('#quantity');
		var q = quantityObj.val();
		q=parseInt(q);

		if(isNaN(q))
			q=0;

		if(q<1)
		{
			if( !onlyRemoveErrors )
			{
				Cart._markUnselected( quantityObj.closest('dl.select_quantity'), true );
				Cart._showQuantityErrorMsg(true);
			}

			quantityErrorCount++;
		}
		else
		{
			Cart._markUnselected( quantityObj.closest('dl.select_quantity'), false );
			Cart._showQuantityErrorMsg(false);
		}

		// prikazi poruku, ili ne
		if(unselectedCount)
		{
			if( !onlyRemoveErrors )
				Cart._showAttrErrorMsg( true);
		}
		else
		{
			Cart._showAttrErrorMsg( false);
		}

		Cart._showAddCartError(onlyRemoveErrors);

		return (quantityErrorCount>0 || unselectedCount>0) ? false : true;
	},


	_markUnselected:function(setObj, flag)
	{
		if(flag)
			setObj.addClass('unselected');
		else
			setObj.removeClass('unselected');

	},

	_showAttrErrorMsg:function(flag)
	{
		if(flag)
			$('#attribute_error').show();
		else
			$('#attribute_error').hide();
	},


	_showQuantityErrorMsg:function(flag)
	{
		if(flag)
			$('#quantity_error').show();
		else
			$('#quantity_error').hide();
	},

	_showAddCartError: function(onlyRemoveErrors)
	{
		if( $('#quantity_error').css('display') != 'none' || $('#attribute_error').css('display') != 'none' )
		{
			if(!onlyRemoveErrors)
			{
				$('#addcart_error').show();
			}
		}
		else
		{
			$('#addcart_error').hide();
		}
	}
} // Cart


var ArticleProductAttrSelect=
{
	_quantity: null,

	_quantityTimeoutHandle: null,

	init:function()
	{
		$('.attr_set dd a').click(
						function()
						{
							var o=$(this);

							o.closest('dd').find('a').removeClass('active');
							o.addClass('active');

							Cart.checkSelected(true);

							return false;
						}
					);

		$('#add_quantity').click( ArticleProductAttrSelect.increase );


		ArticleProductAttrSelect._quantity=$('#quantity');
		if(!ArticleProductAttrSelect._quantity.length)
		{
			ArticleProductAttrSelect._quantity = null;
			return;
		}


		ArticleProductAttrSelect._quantity.keyup( function(e)
										   {
												if(ArticleProductAttrSelect._quantityTimeoutHandle)
												{
													clearTimeout(ArticleProductAttrSelect._quantityTimeoutHandle);
													ArticleProductAttrSelect._quantityTimeoutHandle=null;
												}

												ArticleProductAttrSelect._quantityTimeoutHandle=setTimeout( function()
																										   {
																												Cart.checkSelected(true);
																										   }
																										   , 350 );
										   }
										  ).
									focusout( function(e)
											 {
												if(ArticleProductAttrSelect._quantityTimeoutHandle)
												{
													clearTimeout(ArticleProductAttrSelect._quantityTimeoutHandle);
													ArticleProductAttrSelect._quantityTimeoutHandle=null;
												}

												Cart.checkSelected(true);
											 }
											);
	},

	increase:function(id)
	{
		if(!ArticleProductAttrSelect._quantity)
			return false;

		var v=ArticleProductAttrSelect._quantity.val();
		v=parseInt(v);

		if(isNaN(v) || v<=0)
			v=1;
		else
			v++;

		ArticleProductAttrSelect._quantity.val(v);

		Cart.checkSelected(true);

		return false;
	}
}


var GeneralNotification=
{
	_obj:null,
	_timeoutHandle:null,

	init:function()
	{
		GeneralNotification._obj=$('#prompt');

		if(!GeneralNotification._obj.length)
		{
			GeneralNotification._obj=null;
			return;
		}

		GeneralNotification._obj.find('.close_prompt').click( function()
															 {
																GeneralNotification.hide();
															 }
															);

		$(window).bind('resize', GeneralNotification.resizeWindow);
	},

	show:function(status, msg, partErrorMsg)
	{
		if(!GeneralNotification._obj)
		{
			return;
		}

		if( typeof( partErrorMsg ) == 'undefined')
			partErrorMsg = '';

		GeneralNotification.resizeWindow();

		if(GeneralNotification._timeoutHandle)
		{
			clearTimeout( GeneralNotification._timeoutHandle );
			GeneralNotification._timeoutHandle = null;
		}

		GeneralNotification._obj.find('h3').text( msg );


		if(partErrorMsg)
			GeneralNotification._obj.find('h4.parterrormsg').show().text( partErrorMsg );
		else
			GeneralNotification._obj.find('h4.parterrormsg').hide().text( partErrorMsg );

		if(status)
		{
			GeneralNotification._obj.addClass('success');
			GeneralNotification._obj.removeClass('error');
		}
		else
		{
			GeneralNotification._obj.removeClass('success');
			GeneralNotification._obj.addClass('error');
		}


		GeneralNotification._obj.fadeIn(500);


		GeneralNotification._timeoutHandle = setTimeout( GeneralNotification.hide, 3500 );
	},

	resizeWindow:function()
	{
		var winWidth = $(window).width();
		var winHeight = $(window).height();

		var x=(winWidth - GeneralNotification._obj.width())/2;
		var y=(winHeight - GeneralNotification._obj.height())/2;

		GeneralNotification._obj.css('left', x+'px');
		GeneralNotification._obj.css('top', y+'px');
	},

	hide:function()
	{
		if(!GeneralNotification._obj)
			return;

		if(GeneralNotification._timeoutHandle)
		{
			clearTimeout( GeneralNotification._timeoutHandle );
			GeneralNotification._timeoutHandle = null;
		}

		GeneralNotification._obj.fadeOut('fast');
	}
}


/// d. }

$(document).ready(function(){

	GeneralNotification.init();
	Cart.init();
	ArticleProductAttrSelect.init();



//preload css images - default directory /images
	$.preloadCssImages();

// nivas link external
	$("a.url").attr('target','_blank');

// add "last" class to last list item
	$('div.release_list ul li').last().addClass('last');

//suckerfish on steroids
	$("ul.nav_lvl_one").superfish({
		delay:       1000,                            // one second delay on mouseout
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation
		speed:       'fast',                          // faster animation speed
		autoArrows:  true,                            // disable generation of arrow mark-up
		dropShadows: false                            // disable drop shadows
	});

// home slider
	if ($("#slider").length > 0){
		$("#slider").easySlider({
			auto: true,
			pause: 5000,
			continuous: true,
			numeric: true
		});
	}
// home slider
	if ($("#slider2").length > 0){
		$("#slider2").easySlider({
			auto: true,
			pause: 5100,
			continuous: true,
			numeric: false,
			controlsShow: false

		});
	}
	
// zebra product table
	$('table.shop_list_table tbody tr:odd').addClass('odd');
	$('table.shop_list_table tbody tr:even').addClass('even');

// add "first" class to shop nav
	$('ul.shop_nav li:first-child').addClass('first');

	//$('.carousel_content').topZIndex();




// gallery
/*
	$('#thumbs').delegate('img','click', function(){
		$('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
	});
*/


	$("tr.hidden").hide();
	$('a.toggle_player').bind('click', function() {
		$("tr.hidden").hide();
		$("tr.highlight").removeClass("highlight");
		$(this).parent("td").parent("tr").next("tr.hidden").toggle();
		$(this).parent("td").parent("tr").toggleClass("highlight");
		return false;
	});

// hover share tools
	$(".tour_dates li").hover(
	  function () {
		$(this).addClass("active");
	  },
	  function () {
		$(this).removeClass("active");
	  }
	);


//content X gallery
	var galleries = $('.ad-gallery').adGallery({
	  loader_image: 'images/loader.gif',
	  thumb_opacity: 1, // Opacity that the thumbs fades to/from, (1 removes fade effect)

	  enable_keyboard_move: true, // Move to next/previous image with keyboard arrows?
	  cycle: true // If set to false, you can't go from the last image to the first, and vice versa
	});


	$('.box dl').last().css('background', 'none');

	/*d*/
	$('.form_display_list .select_all_check').click( function()
												   {
														if(this.checked)
														{
															$('.form_display_list input[type=checkbox][name^=product_sel]').attr('checked', 'checked');
															$('.form_display_list .select_all_check').attr('checked', 'checked');
														}
														else
														{
															$('.form_display_list input[type=checkbox][name^=product_sel]').removeAttr('checked');
															$('.form_display_list .select_all_check').removeAttr('checked');
														}
												   } );
	$('#buy_selected').click( function()
							 {
								var ids=new Array;

								$('.form_display_list input[type=checkbox][name^=product_sel]:checked').each(
														function(ix)
														{
															var v=this.value;
															v=parseInt(v);

															if(!isNaN(v) && v>0)
															{
																ids[ids.length]=v;
															}
														}
													   );

								Cart.submit_addMultiCart(ids);
								return false;
							 }
							);
});

