jQuery(document).ready(function(){

	//TABS
	jQuery(function() {
		jQuery('#containerTabs').tabs({
			fxFade: true,
			fxSpeed: 'normal',
			onShow: function() {
				//fix for packaging data hover popup (make container higher to prevent the popup from going past footer)
				if (!jQuery('#packaging').hasClass('tabs-hide')) {
					getContainerHeight();
					jQuery('#containerContent').animate({height: oldHeight + 120}, 'fast');
				}
			},
			onHide: function() {
				//fix for packaging data hover popup (make container higher to prevent the popup from going past footer)
				//if (jQuery('#packaging').hasClass('tabs-hide')) {
					//jQuery('#containerContent').animate({height: oldHeight}, 'fast');
				//}
				jQuery('#containerContent').removeAttr('style');
			}			
		});
		var htmlStr = jQuery.trim(jQuery('#tab3 .tabs-content').text());
		if (htmlStr == '-') {jQuery('#tab3').css('display','none');jQuery('#tab3Li').css('display','none');}
		if (jQuery('#garnitura').length != 0) { //if garnitura
			//jQuery('#tabsLi').css('display','none');
			//jQuery('.tabs-content').css({'padding':'0px','width':'100%'});
			//jQuery('.tabs-container').css('width','100%');jQuery('#containerTabs').css('width','100%');
		}
	});
	//

	//NOT SURE IF USED
	jQuery('.openDown').click(function(){
		var divId = this.lang;
		jQuery(this).toggleClass('selected');
		jQuery('#'+divId).slideToggle();
	});
	//

	//LEGEND
		//get height of container
		var oldHeight = 0;
		function getContainerHeight() {
			oldHeight = jQuery('#containerContent').height();
		}
		
		//get height of image
		var legendaHeight = 0;
		var newImg = new Image();
		newImg.src = jQuery('.legenda:first').find('div img').attr('src');
		jQuery('#legendaImg').ready(function() {
			legendaHeight = newImg.height + 20;
		});
		
		//click handler to show legend
		jQuery('a.legendaLink').click(function(eh){
			var thisDivLegend = jQuery(this).parent().find('.legenda');
			if (thisDivLegend.hasClass('open')) {
				thisDivLegend.removeClass('open');
				slideUp(thisDivLegend);
			} else {
				thisDivLegend.addClass('open');
				getContainerHeight();
				slideDown(thisDivLegend);
			}
			eh.preventDefault();
		});
		
		//if we click on any tab, hide the legend
		jQuery('a.tab').click(function(){ 
			jQuery('a.legendaLink').each(function() { //check legends on all tabs and hide if visible
				var thisDivLegend = jQuery(this).parent().find('.legenda');
				if (thisDivLegend.hasClass('open')) {
					slideUp(thisDivLegend);
					thisDivLegend.removeClass('open');
				}
			});
		});
		
		//slide the legend down (show)
		function slideDown(div) {
			jQuery('#containerContent').animate({height: oldHeight + legendaHeight},'slow'); //animate container height
			div.slideToggle('slow');
			jQuery('html,body').animate({scrollTop: div.offset().top},'slow'); //animated scroll to top of legend
		}
		
		//slide the legend up (hide)
		function slideUp(div) {
			jQuery('#containerContent').animate( //animate container height
				{height: oldHeight},
				{
				duration:'slow',
				complete: function() { //on finish anim remove all inline styles
					jQuery(this).removeAttr('style');
				}
			});
			div.slideToggle('slow');
		}
	//
	
	//fix for category navigation for IE7 and under - add class to elements with two lines of text
	if (jQuery.browser.msie && jQuery.browser.version <= 7) {
		jQuery('#categoriesCatalogue li a').each(function(){
			var contentHeight = jQuery(this).height();
			
			if (contentHeight < 30) {
				jQuery(this).addClass('oneLine');
			} else {
				jQuery(this).addClass('twoLines');
			}
			
		});
	}
	//
	
	//hand tools - fix top margin on sidebar, if breadcrumbs are empty
	if (jQuery('#breadcrumbs').text().length == 0) jQuery('.sidebar').addClass('bigGap');
	//
	
	//CLICKABLE TR	
		//new products table
		jQuery('#newProducts table tr, .newProductsList table tr')
			.mouseover(function(){
				jQuery(this).find('td').addClass('active');
			})
			.mouseout(function(){
				jQuery(this).find('td').removeClass('active');
			}).superLink();
	//
	
	//add class last to last tr in newProductsList table
	jQuery('.newProductsList table tr:last-child').addClass('last');
	
	//CUSTOM TABS FOR NEW PRODUCTS
    jQuery('.customTabs').customTabs();
	//
	
	//add class last to last li in navigation
	jQuery('ul#navigation li:last').addClass('last');
	//
	
	//add classes to table td's on packaging data table
	jQuery('table.packaging tr:last-child').addClass('last');
	jQuery('table.packaging tr td:first-child').addClass('first');
	jQuery('table.packaging tr td:last-child').addClass('last');
	jQuery('table.packaging tr').find('td.izdTabNaslovMaloprodajna:first, td.izdTabNaslovTransportna:first, td.maloprodajna:first, td.transportna:first').addClass('borderLeft');
	//
	
	//add classes to table td's on packaging data table and make img wrappers to show on hover
	jQuery('table.packaging tr').not(':first')
		.each(
			function(tabIndex, tabEl) {
				var $imgContent = jQuery(this).find('td:last div.images');
				var $parentRow = jQuery(this);
				var $prodCode = jQuery.trim($parentRow.find('td:first-child').text());
				$parentRow.attr('id', 'prod'+$prodCode);
				
				//make a new div with images and append it to document body
				if ($imgContent.length) {
					$imgContent.contents().appendTo(jQuery('body')).wrapAll('<div class="imgContainer" id="prod' + $prodCode + 'img" />');
					$imgContent.remove();
				}
			})
		.hover(
			function() {
				//handle hover on table row
				jQuery(this).addClass('hover');
				jQuery(this).next().addClass('hoverNext');
				
				//position overlay div with images
				var imageContainer = jQuery('#' + jQuery(this).attr('id') + 'img');
				imageContainer.css({
					top: jQuery(this).offset().top + jQuery(this).outerHeight() - 1,
					left: jQuery(this).find('td.maloprodajna:first').offset().left
				});
				imageContainer.show();
			}, function(){
				jQuery(this).removeClass('hover');
				jQuery(this).next().removeClass('hoverNext');
				var imageContainer = jQuery('#' + jQuery(this).attr('id') + 'img');
				imageContainer.hide();
	});
	//
	
	function positionPackagingDataElements() {
		//position packaging type label above table
		var maloprodajnaWidth = -1;
		var transportnaWidth = -1;
		
		jQuery('table.packaging tr:last').find('td.maloprodajna').each(function(tabIndex, tabEl) {
			maloprodajnaWidth += jQuery(this).outerWidth();
		});
		jQuery('table.packaging tr:last').find('td.transportna').each(function(tabIndex, tabEl) {
			transportnaWidth += jQuery(this).outerWidth();
		});
		
		jQuery('.packagingType span.maloprodajna').css('right', maloprodajnaWidth/2 + transportnaWidth - jQuery('.packagingType span.maloprodajna').outerWidth()/2);
		jQuery('.packagingType span.transportna').css('right', transportnaWidth/2 - jQuery('.packagingType span.transportna').outerWidth()/2);
		//
		
		//set container height
		jQuery('.packagingType').height(jQuery('.packagingType span.maloprodajna').outerHeight());
		
		//set overlay image container widths
		jQuery('div.imgContainer div.maloprodajna').css('width', maloprodajnaWidth);
		jQuery('div.imgContainer div.transportna').css('width', transportnaWidth);
		
			// --> fix too small a width of overlay image container in FF and IE8+
			var imageContainerWidth = jQuery('table.packaging tr:last').find('td.maloprodajna:first').offset().left - jQuery('table.packaging tr').offset().left + jQuery('div.imgContainer:first').outerWidth();
			var widthDifference =  jQuery('table.packaging tr:last').outerWidth() - imageContainerWidth;
			
			if (widthDifference != 0) {
				//alert('yes, overlay div is too small by ' + widthDifference + 'px');
				jQuery('div.imgContainer div.transportna').css('width', transportnaWidth + widthDifference);					
			}
		//
	}
	
	//make hidden div temp visible to browser in order to get dimensions
	jQuery('#packaging').addClass('temp-show');
	
	//get dimensions and position elements
	positionPackagingDataElements();
	
	//hide hidden div again
	jQuery('#packaging').removeClass('temp-show');

});

jQuery.fn.customTabs = function(options) {

    return this.each(function(index, el) {

        jQuery(el).find('.tabs-nav-alt a').each(function(tabIndex, tabEl) {

            jQuery(tabEl).click(function(e) {
				
				e.preventDefault();
				
				var currTab = jQuery(el).find('.tabs-content-alt').not('.tabs-hide');
				var currTabIndex = jQuery(el).find('.tabs-content-alt').index(jQuery(currTab));
				
				if (tabIndex != currTabIndex) {
					//ie fix opacity on div with image and slow animation
					jQuery('table tr td div.linkWrapper').css('position', 'static');
					jQuery('table tr td div.linkWrapper a').css('display', 'none');
					
					jQuery(currTab).animate({
						opacity: 0
					  }, 'normal', function() {
						//animation complete, do all these actions
						jQuery(this).addClass('tabs-hide');
						jQuery(el).find('.tabs-nav-alt a:eq('+currTabIndex+')').removeClass('active');
						
						jQuery(tabEl).addClass('active');
						jQuery(el).find('.tabs-content-alt:eq('+tabIndex+')').removeClass('tabs-hide');
						jQuery(el).find('.tabs-content-alt:eq('+tabIndex+')').css('opacity','0')
						jQuery(el).find('.tabs-content-alt:eq('+tabIndex+')').animate({opacity: 1}, 'normal', function() {
							//ie fix opacity on div with image and slow animation
							jQuery('table tr td div.linkWrapper').css('position', 'relative');
							jQuery('table tr td div.linkWrapper a').css('display', 'block');
						});
					  });
				}
				
                return false;
            });

            jQuery(tabEl).attr('tabindex', 0).keypress(function(ev) {
			
                if (ev.keyCode == 13) jQuery(this).click();
				
            });
			
        });
		
		//on first load open first tab
		if (!jQuery(el).find('.tabs-content-alt:has(.active)').length) {
		
			jQuery(el).find('.tabs-content-alt').not(':first').each(function(tabIndex, tabEl) {
				jQuery(tabEl).addClass('tabs-hide');
			});
			
			jQuery(el).find('.tabs-nav-alt a:first').addClass('active');
			
        }
		
    });
};
