$(document).ready(function() {

	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, deepLinkHandler);
	initSearchProd();

	function fancyTitleFormat(title, currentArray, currentIndex, currentOpts) {
	    return '<div id="fancybox-title-over">' + (title && title.length ? '<b>' + title + '</b>' : '' ) 
				+ ' (' + (currentIndex + 1) + '/' + currentArray.length + ')'
				+ '<a class="dl-link" target="_blank" href="'+ $(this.orig).parent().next().attr('href') +'">PREUZMI</a>' 
				+ '</div>';
	}
	
	$("a.preview-dl").fancybox({
		'autoScale' : true,
	    'overlayShow' : true,
	    'zoomSpeedIn' : 600,
	    'zoomSpeedOut' : 500,
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'easingIn' : 'easeOutBack',
		'easingOut' : 'easeInBack',
		'titlePosition' : 'over',
		'overlayColor' : '#005BA6',
		'titleFormat' : fancyTitleFormat,
		'overlayOpacity' : 0.7,
		'onComplete' : function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").fadeIn();
			}, function() {
				$("#fancybox-title").fadeOut();
			});
		}		 
	});
	
	$("a.preview-video").fancybox({
		'autoScale' : true,
	    'overlayShow' : true,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'titlePosition' : 'inside',
		'overlayColor' : '#005BA6',
		'overlayOpacity' : 0.7		 
	});

	$("a.preview").fancybox({
		'autoScale' : true,
	    'overlayShow' : true,
	    'zoomSpeedIn' : 600,
	    'zoomSpeedOut' : 500,
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'easingIn' : 'easeOutBack',
		'easingOut' : 'easeInBack',
		'titlePosition' : 'over',
		'overlayColor' : '#005BA6',
		'overlayOpacity' : 0.7,
		'onComplete' : function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").fadeIn();
			}, function() {
				$("#fancybox-title").fadeOut();
			});
		}
	});

	var broj = $(".slider-items").children('li').length;
	if (broj < 5) {
		$('.line-holder').css('display', 'none');
	}

    $('.accordion').accordion({ active: true, header: 'h2', clearStyle: true, fillSpace: false, navigation: true, collapsible: true });

    // search box click
    var searchBoxField = $('.search-box .search-field');
    var searchBoxFieldDefault = "Pretraži";
    searchBoxField.focus(function() { if ($(this).attr("value") == searchBoxFieldDefault) $(this).attr("value", ""); $(this).addClass("active"); });
    searchBoxField.blur(function(e) { if ($(this).attr("value") == "") $(this).attr("value", searchBoxFieldDefault); $(this).removeClass("active"); });

    // disable form submit on enter key press
    //$("form").keypress(function(e) { if (e.keyCode == 13) return false; });

    //Add pointer class to .photo .item
    $(".photo .item").addClass("pointer");

    //Hover over photo fade in hidden span
    $(".photo .item").hover(
        function() {
            $(this).find("span").fadeIn();
        },
        function() {
            $(this).find("span").fadeOut();
        }
    );

	$(".slider-holder").each(function(e){ 
	    // product slider & preloader
	    var holder = $(this);
	    var item = $("li", $(holder));
		var list = $("ul", $(holder));
		holder.prepend('<div class="loader"> </div>');

		$(holder).onImgsLoaded(function(e){
		    var sliderOpts = {
		        animate: true,
		        max: (list.width() - $(holder).width()),
		        slide: function(e, ui) {
		            list.css("left", "-" + ui.value + "px");
		        },
		        stop: function(ev, ui) {
		            list.animate({ 'left': '-' + ui.value + 'px' }, 'normal', 'linear');
		        }
		    };
			//$(".slider-holder .slider").slider(sliderOpts);
			holder.find(".slider").slider(sliderOpts);
			holder.find(".loader").remove();
			list.fadeTo("slow", 1);
		});
	});
	
	
	// Nagradade i priznanja tooltip
	$(".award .award-bubble").css("top",70).hide();
	$('<img class="tooltip-bottom" src="/media/assets/bottom.png"/>').appendTo('.award .award-bubble:last-child');	
	$('.award').hover(
		function(){
        	$(this).find('.award-bubble').stop(true, true).animate({opacity: "show", top: "90"}, "slow");
        }, function(){
        	$('.award-bubble').animate({opacity: "hide", top: "70"}, "fast");
    });
    
	//file extension cheker
	$("a[href$='.pdf']").addClass("ico-pdf");
	$("a[href$='.doc']").addClass("ico-doc");
	$("a[href$='.zip']").addClass("ico-zip");
	$("a[href$='.xsl']").addClass("ico-xsl");
	$("a[href$='.xls']").addClass("ico-xsl");
	$("a[href$='.eps']").addClass("ico-eps");
	
});


jQuery.fn.encHTML = function() {
  return this.each(function(){
    var me   = jQuery(this);
    var html = me.html();
    me.html(html.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'));
  });
};
 
jQuery.fn.decHTML = function() {
  return this.each(function(){
    var me   = jQuery(this);
    var html = me.html();
    me.html(html.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>'));
  });
};

jQuery.fn.onImgsLoaded = function(_cb) { 
	return this.each(function() { 
		var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this), 
		_cont = this, 
		i = 0,
		_done = function() {
			if( typeof _cb === 'function' ) _cb(_cont);
		};
		
		if( $imgs.length ) {
			$imgs.each(function() {
				var _img = this,
			  	_checki=function(e) {
					if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
					{
						if( ++i===$imgs.length ) _done();
					}
					else if( _img.readyState === undefined )
					{
					  	$(_img).attr('src',$(_img).attr('src'));
					}
			  	};
			
			  $(_img).bind('load readystatechange', function(e){_checki(e);});
			  _checki({type:'readystatechange'});
			});
		} else _done();
	});
};
