var Loader=
{
	_timeoutId:0,

	show: function()
	{
		Loader._timeoutId = setTimeout(Loader._showLoader, 1);

	//	Loader._showLoader();
	},

	hide: function()
	{
		clearTimeout( Loader._timeoutId );
		$('#load').addClass('displaynone');
		followRemove();
	},

	_showLoader: function()
	{
		followSet();
		$('#load').removeClass('displaynone');
	}
}



function submitShoppingCartAdd(sectionId, contentId)
{
	Loader.show();

	$('.message_model').hide();

	if(typeof(sectionId)=='undefined')
		sectionId=0;

	if(typeof(contentId)=='undefined')
		contentId=0;

	var productId = '';

	if($('#product_id').length )
	{
		productId = $('#product_id').val();
	}
	else
	{
		// pokupim ga
		$('.article_detalis a.cart').each( function(ix)
										  {
											var jo=$(this);

											if(jo.hasClass('checked'))
											{
												var id = this.id.substring(1);

												if(productId != '')
													productId +=',';

												productId += id;
											}
										  }
										 );

		if(productId == '')
		{
			// error - nemam odabrani model
			$('.message_model').show();

			Loader.hide();
			location.href = "#models";

			return false;
		}


	}




	fwajax.phpExecute(
			{url: '/cart/add'},
			sectionId,
			contentId,
			productId
		);

	return false;
}

function callbackShoppingCartAdd(success, html, sectionId, contentId, productId, newMsg, oldMsg )
{
	if(success && html!='')
	{
		$('#cart_list').html(html);
	}

	Loader.hide();

	$('.article_detalis a.cart').removeClass('checked');

	if(success)
	{
		if( $('#details_product_cart a').length )
		{
			$('#details_product_cart a').
				addClass('added').text(newMsg).
				animate( {opacity: 1.0}, 1500, function()
												{
													$('#details_product_cart a').
															removeClass('added').
															text(oldMsg);
												}
						);
		}

		if( $('#master_cart_add').length )
		{
			$('#master_cart_add').
				addClass('added').text(newMsg).
				animate( {opacity: 1.0}, 3500, function()
												{
													$('#master_cart_add').
															removeClass('added').
															text(oldMsg);
												}
						);
		}


		//if( $('#list_product_cart_msg_'+sectionId+'_'+productId+' a').length )
		//{
		//	$('#list_product_cart_msg_'+sectionId+'_'+productId+' a').
		//		addClass('added').html('<span>'+newMsg+'</span>').
		//		animate( {opacity: 1.0}, 1500, function()
		//									{
		//										$('#list_product_cart_msg_'+sectionId+'_'+productId+' a').
		//										removeClass('added').
		//										html('<span>' + oldMsg + '</span>');
		//									}
		//				);
		//}
	}
}


/*
function submitShoppingCartAdd(sectionId, productId)
{
	Loader.show();

	if(typeof(productId)=='undefined')
		productId=0;

	fwajax.phpExecute(
			{url: '/cart/add'},
			sectionId,
			productId
		);

	return false;
}

function callbackShoppingCartAdd(success, html, sectionId, productId, newMsg, oldMsg )
{
	if(success && html!='')
	{
		$('#cart_list').html(html);
	}

	Loader.hide();

	if(success)
	{
		if( $('#details_product_cart a').length )
		{
			$('#details_product_cart a').
				addClass('added').text(newMsg).
				animate( {opacity: 1.0}, 1500, function()
												{
													$('#details_product_cart a').
															removeClass('added').
															text(oldMsg);
												}
						);
		}

		if( $('#master_cart_add').length )
		{
			$('#master_cart_add').
				addClass('added').text(newMsg).
				animate( {opacity: 1.0}, 3500, function()
												{
													$('#master_cart_add').
															removeClass('added').
															text(oldMsg);
												}
						);
		}


		if( $('#list_product_cart_msg_'+sectionId+'_'+productId+' a').length )
		{
			$('#list_product_cart_msg_'+sectionId+'_'+productId+' a').
				addClass('added').html('<span>'+newMsg+'</span>').
				animate( {opacity: 1.0}, 1500, function()
											{
												$('#list_product_cart_msg_'+sectionId+'_'+productId+' a').
												removeClass('added').
												html('<span>' + oldMsg + '</span>');
											}
						);
		}
	}
}
*/

function submitShoppingCartRemove(cartId, productId)
{
	Loader.show();

	if(typeof(productId)=='undefined')
		productId=0;

	fwajax.phpExecute(
			{url: '/cart/remove'},
			cartId,
			productId
		);

	return false;
}

function callbackShoppingCartRemove(success, cartId, productId, html)
{
	if(success)
	{
		$('#cart_list').html(html);

		$('tr#cartitem_' + cartId + '_' + productId).remove();
	}

	Loader.hide();

	if( $('table#cart_table tbody tr').length == 0 )
	{
		$('table#cart_table').css('display', 'none');
		$('#cart_empty').css('display', 'block');

		$('.only_cart_full').css('display', 'none');
	}
}


var _submitShoppingCartSetQuantityHandle=new Object;

function submitShoppingCartSetQuantity(cartId, productId, diff)
{
	var obj=$('tr#cartitem_' + cartId + '_' + productId + ' input[name^=cart_quantity]');

	var quantity=obj.val();
	quantity=parseInt(quantity);

	if(isNaN(quantity))
	{
		quantity=1;
	}

	obj.val(quantity);

	if(diff==0)
	{
		if( quantity == 0 )
		{
			submitShoppingCartRemove(cartId, productId);
		}
		else
		{
			_submitShoppingCartSetQuantity(cartId, productId, quantity);

		}

		return false;
	}

	if(diff>0)
		quantity++;
	else if(quantity>1 && diff<0)
		quantity--;
	else
		return false;

	obj.val(quantity);

	var keyix=cartId + '_' + productId;

	if(typeof( _submitShoppingCartSetQuantityHandle[keyix] ) != 'undefined' )
	{
		clearTimeout(_submitShoppingCartSetQuantityHandle[keyix]);
		delete _submitShoppingCartSetQuantityHandle[keyix];
	}

	_submitShoppingCartSetQuantityHandle[keyix]=setTimeout('_submitShoppingCartSetQuantity('+cartId+','+productId+','+quantity+')', 1000);

	return false;
}

function _submitShoppingCartSetQuantity(cartId, productId, quantity)
{
	var keyix=cartId + '_' + productId;
	if(typeof( _submitShoppingCartSetQuantityHandle[keyix] ) != 'undefined' )
	{
		clearTimeout(_submitShoppingCartSetQuantityHandle[keyix]);
		delete _submitShoppingCartSetQuantityHandle[keyix];
	}

	Loader.show();

	fwajax.phpExecute(
		{url: '/cart/quantity'},
		cartId,
		productId,
		quantity
	);

	return false;
}

function callbackShoppingCartSetQuantity(success, cartId, productId, quantity, html)
{
	Loader.hide();

	if(html)
	{
		$('#cart_list').html(html);
	}
}


var _newsletterErrorTimeoutHandle=null;

function _submitNewsletterEmailAdd_error()
{
	$('#newsletter_container').addClass('error');

	$('#newsletter_container_error').show();

	if(_newsletterErrorTimeoutHandle)
	{
		clearTimeout( _newsletterErrorTimeoutHandle );
		_newsletterErrorTimeoutHandle = null;
	}

	_newsletterErrorTimeoutHandle = setTimeout(_submitNewsletterEmailAdd_error_msgHide , 2000);
}

function _submitNewsletterEmailAdd_error_msgHide()
{
	$('#newsletter_container_error').fadeOut();
}

function _submitNewsletterEmailAdd_removeError()
{
	$('#newsletter_container').removeClass('error');
	$('#newsletter_container_error').fadeOut();

	if(_newsletterErrorTimeoutHandle)
	{
		clearTimeout( _newsletterErrorTimeoutHandle );
		_newsletterErrorTimeoutHandle = null;
	}
}


function submitNewsletterEmailAdd()
{
	var str=$('#newsletter_email').val();

	if(!checkEmail(str))
	{
		_submitNewsletterEmailAdd_error();
		return false;
	}

	var str2=$('#s_email').val();
	if(str2 != '')
	{
		return false;
	}

	Loader.show();

	_submitNewsletterEmailAdd_removeError();

	fwajax.phpExecute(
			{url: '/newsletter/email-add'},
			str,
			str2
		);

	return false;
}


function callbackNewsletterEmailAdd(success, popup, leadId)
{
	$('#newsletter_section_list input[type=checkbox]').attr('checked', 'checked');
	$('#lead_id').val('');

	Loader.hide();
	if(success)
	{
		_submitNewsletterEmailAdd_removeError();

		if(popup)
		{
			$('#lead_id').val(leadId);
			//alert('lead_id (' + leadId + ')(' + $('#lead_id').val() + ')' );

			tb_show('', $('#newsletter_prijava').attr('href'), false);
		}
	}
	else
	{
		_submitNewsletterEmailAdd_error();
	}
}

function log(s)
{
	if(typeof(console)!='undefined' && typeof(console.log)!='undefined')
	{
		console.log(s);
	}
}

function submitNewsletterSectionUpdate()
{
	var str=$('#newsletter_email').val();

	//log('1 ' + str);

	if(!checkEmail(str))
	{
		//log('2 ' + str);

		tb_remove();

		return false;
	}

	var str2=$('#s_email').val();
	if(str2 != '')
	{
		//log('3 ' + str);

		tb_remove();
		return false;
	}

	var lead_id=$('#lead_id').val();
	if(lead_id == '')
	{
		//log('4 ' + str);
		//tb_remove();
		//return false;
	}

	var section_1=$('#newsletter_section_list input[type=checkbox][name=section_1]').attr('checked') ? 1: 0;
	var section_2=$('#newsletter_section_list input[type=checkbox][name=section_2]').attr('checked') ? 1: 0;
	var section_3=$('#newsletter_section_list input[type=checkbox][name=section_3]').attr('checked') ? 1: 0;
	var section_4=$('#newsletter_section_list input[type=checkbox][name=section_4]').attr('checked') ? 1: 0;

	//log('4 ('+section_1+')('+section_2+')('+section_3+')('+section_4+')');

	Loader.show();

	fwajax.phpExecute(
			{url: '/newsletter/section-update'},
			lead_id,
			str,
			str2,
			section_1,
			section_2,
			section_3,
			section_4
		);

	return false;
}


function callbackNewsletterSectionUpdate(success)
{
	Loader.hide();

	if(success)
	{
		$('#newsletter_email').val('');
	}

	tb_remove();
}











function setActiveTab(tab)
{
	$('#search_tab').val(tab);
	//$('#index_tab').val(tab);

	return true;
}






function onChangeArticleSubsection(selectDom)
{
	location.href=$(selectDom).val() + '?v=' + $('#product_view_type').val();
}






function checkEmail(value)
{
return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

//jquery specific styles
$(document).ready(function(){

	$(function(){
		// Tabs
		$('#tabs').tabs();
	});


	$('a.ext').attr('target', '_blank');



////////////////////////////////////////////////////////////////
//  Menu style fix start

//clasik top meni
	$(".nav_clasik a").mouseover(function(){

		var linkli=$(this).parent().not('.lang');

		linkli.prev().addClass("no_bar");

	}).mouseout(function(){

		var linkli=$(this).not('.current').parent();

		linkli.prev().removeClass("no_bar");

	});


//current
	function currentMain()
	{
		var aobj=$('.nav_clasik a.current');
		if(aobj.length)
		{
			var linkli=aobj.parent();

			linkli.prev().addClass("no_bar");
		}
	}
	currentMain();






//second level meni
	$(".nav_sec a").mouseover(function(){

		var linkli=$(this).parent();

		linkli.prev().addClass("no_bar");
		//linkli.addClass("no_bar");

	}).mouseout(function(){

		var linkli=$(this).not('.current').parent();

		linkli.prev().removeClass("no_bar");
		//linkli.removeClass("no_bar");

	});


	//current
	function currentSec()
	{
		var aobj=$('.nav_sec a.current');
		if(aobj.length)
		{
			var linkli=aobj.parent();

			linkli.prev().addClass("no_bar");
			//linkli.addClass("no_bar");

		}
	}
	currentSec();

	//sub title
	function subTitle()
	{
		var aobj=$('.nav_sec span.title');
		if(aobj.length)
		{
			var linkli=aobj.parent();

			linkli.prev().addClass("no_bar");
			linkli.addClass("no_link");

		}
	}
	subTitle();


//  Menu style fix stop
////////////////////////////////////////////////////////////////





////////////////////////////////////////////////////////////////
// slider start

	if( $("#slider_home").length )
	{
		$("#slider_home").easySlider( {
			auto:true,
			continuous: true,
			speed: 800,
			pause: 5000,
			numeric: true
		});

		var nContWidth = $('ol#controls li').width();
		var nContLenght = $('ol#controls li').length;
		var nContSum = nContWidth * nContLenght
		$('ol#controls').css('width', nContSum);
	}

	if( $("#slider").length )
	{
		//if( $(".autoslider").length )
		//{
		//	$("#slider").easySlider( {
		//		auto:true,
		//		continuous: true,
		//		speed: 800,
		//		pause: 5000
		//	});
		//}
		//else
		//{
		//	$("#slider").easySlider(
		//		{ continuous: true }
		//	);
		//}

		$("#slider").easySlider( {
			auto:true,
			//continuous: true,
			speed: 800,
			pause: 5000
		});


		$('#nextBtn a').stop().animate({
			opacity: 0.4
		  });

		$('#prevBtn a').stop().animate({
			opacity: 0.4
		  });


		$("#slider").mouseover(function(){
			$('#nextBtn a').stop().animate({
				opacity: 1
			  }, "fast" );

			$('#prevBtn a').stop().animate({
				opacity: 1
			  }, "fast" );
		}).mouseout(function(){
			$('#nextBtn a').stop().animate({
				opacity: 0.4
			  }, "fast" );

			$('#prevBtn a').stop().animate({
				opacity: 0.4
			  }, "fast" );
		});


		/*
		$('#slider .link').stop().animate({
			opacity: 0.6
		  });


		$("#slider").mouseover(function(){
			$('#slider .link').stop().animate({
				opacity: 1
			  }, "fast" );
		}).mouseout(function(){
			$('#slider .link').stop().animate({
				opacity: 0.4
			  }, "fast" );
		});
		*/

	}


	$(".slider_small").easySlider({
		auto:	false,
		controlsShow: false
	});



// slider stop
////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////
//  Link style setup

	$('dl.download dd a[href$=".pdf"]').append("<span>pdf</span>");
	$('dl.download dd a[href$=".xdoc"]').append("<span>xdoc</span>");
	$('dl.download dd a[href$=".doc"]').append("<span>doc</span>");
	$('dl.download dd a[href$=".xml"]').append("<span>xml</span>");
	$('dl.download dd a[href$=".zip"]').append("<span>zip</span>");
	$('dl.download dd a[href$=".rar"]').append("<span>rar</span>");
	$('dl.download dd a[href$=".pps"]').append("<span>pps</span>");
	$('dl.download dd a[href$=".gif"]').append("<span>gif</span>");
	$('dl.download dd a[href$=".jpg"]').append("<span>jpg</span>");
	$('dl.download dd a[href$=".xls"]').append("<span>xls</span>");

//  Link style setup
////////////////////////////////////////////////////////////////

	$('.ref_title').click(function(){
	  $(this).next('.ref_list').slideToggle()
             .siblings('.ref_list:visible').slideUp();
	  $(this).toggleClass('rt_open ');
	  $(this).siblings().removeClass('rt_open ');
	});



	$('.show_all').click(function(){
		$('.ref_list:hidden').slideDown();
		$('.ref_title').addClass('rt_open ');
		return false;
    });

	$('.close_all').click(function(){
		$('.ref_list:visible').slideUp();
		$('.ref_title').removeClass('rt_open ');
		return false;
    });




	$('table.ponuda_block tr').hover(
	function() { $(this).addClass('hover'); },
	function() { $(this).removeClass('hover'); });



	$('div.ponuda_block dl').hover(
	function()
	{
	    connect = $(this).attr('class')
	    var find = $(this).parents('.ponuda_block').find("a[rel='"+connect+"']")
	    find.addClass('pb_hover');
	    $(this).addClass('pb_hover');
	},
	function()
	{
	    var find = $(this).parents('.ponuda_block').find("a[rel='"+connect+"']")
	    find.removeClass('pb_hover');
	    $(this).removeClass('pb_hover');
	});

	$('div.ponuda_block a.goto').hover(
	function()
	{
	    var connect_alt = $(this).attr('rel')
	    var find = $(this).parents('.ponuda_block').find('dl.'+connect_alt)
	    find.addClass('pb_hover');
	},
	function()
	{
	    var connect_alt = $(this).attr('rel')
	    var find = $(this).parents('.ponuda_block').find('dl.'+connect_alt)
	    find.removeClass('pb_hover');
	});



// tootlip in product
	$('dl.product_attr .tooltip').css('opacity', '0');
	$('dl.product_attr dd').hover(
		function() {
			$('.tooltip', this).css('display','block').animate({ opacity:'1', bottom:'48px' }, 200 );
		},
		function() {
			$('.tooltip', this).animate({ opacity:'0', bottom:'44px' }, 200 , function(){ $(this).css('display','none').css('bottom','52px') } );
		});

// product table check model
	$('.article_detalis a.cart').click(function(e){
		e.preventDefault();
		$(this).toggleClass('checked');
	});


// sidebar item helper
    $('.content .meni_sidebar li>a').hover(
	function() {
	    $(this).parent().prev('li').not('li.current').children('a').addClass('hover_help');
	},
	function() {
	    $(this).parent().prev('li').children('a').removeClass('hover_help');
	});


// sidebar no meni bg helper
    /*
    if ( !$('.meni_sidebar ul li').length )
    {
	$('.meni_sidebar .nav_bottom').addClass('short_block');
    }
    */

// last items section
	//$('.meni_sidebar ul.sub li:last').addClass('last');


}); // end document ready



////////////////////////////////////////////////////////////////
//anchor slide start
/*******
*** Anchor Slider by Cedric Dugas ***
*** Http://www.position-absolute.com ***
Never have an anchor jumping your content, slide it.
Don't forget to put an id to your anchor !
You can use and modify this script for any project you want, but please leave this comment as credit.
*****/

$(document).ready(function() {
 $("a.anchorLink").anchorAnimate()
});

jQuery.fn.anchorAnimate = function(settings) {

 settings = jQuery.extend({
 speed : 1100
 }, settings);

 return this.each(function(){
 var caller = this
 $(caller).click(function (event) {
 event.preventDefault()
 var locationHref = window.location.href
 var elementClick = $(caller).attr("href")

 var destination = $(elementClick).offset().top;
 $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
 window.location.hash = elementClick
 });
 return false;
 })
 })
}

//anchor slide stop
////////////////////////////////////////////////////////////////

/*
function startSmallSlider1 () {
	$("#ss1").easySlider({
		auto: true,
		controlsShow: false,
		continuous: true,
		pause:	4000
	});
}
function startSmallSlider2 () {
	$("#ss2").easySlider({
		auto: true,
		controlsShow: false,
		continuous: true,
		pause:	4000
	});
}
function startSmallSlider3 () {
	$("#ss3").easySlider({
		auto: true,
		controlsShow: false,
		continuous: true,
		pause:	4000
	});
}


$(window).load( function() {
	setTimeout( startSmallSlider1, 2100 );
	setTimeout( startSmallSlider2, 1800 );
	setTimeout( startSmallSlider3, 1500 );
});
*/

function assignPagerViewType()
{
	var v=$('#product_view_type').val();
	var m=$('#manufacturer_id').val();

	var g='';
	if(typeof(v) != 'undefined' && v!= '')
	{
		g += (g=='') ? '?' : '&';
		g += 'v=' + v;
	}

	if(typeof(m) != 'undefined' && m!= '')
	{
		g += (g=='') ? '?' : '&';
		g += 'm=' + m;
	}

	if(g)
	{
		$('.pager li a').each( function(ix)
							  {
								var s=String(this.href);
								s += g;
								this.href = s;

								//log(s);
							  }
							 );
	}
}

function submitManufacturer(manId)
{
	var v=$('#product_view_type').val();
	var sdPath=$('#sd_path').val();

	var s=sdPath + '?m='+manId;
	if(typeof(v) != 'undefined' && v!='')
		s += '&v=' + v;

	location.href = s;
}
