var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -1;
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_mac = navigator.platform.toLowerCase().indexOf('mac') > -1;
var is_linux = navigator.platform.toLowerCase().indexOf('linux') > -1;
var ie6 = false;
var ie7 = false;
var ie8 = false;


$(document).ready(function() {

	$('#menu-header li').first().addClass('first-child');
	$('#menu-categories li').first().addClass('first-child');

	var all_links = $('a');
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();

	all_links.map(function(i, el){
		if($(el).attr('href')) {
			if($(el).attr('href').indexOf('http://') != -1 && $(el).attr('href').indexOf(hostname) == -1) {
				$(el).attr('target', '_blank');
			}
			
			if($(el).attr('href').indexOf('.pdf') != -1 || $(el).attr('href').indexOf('.doc') != -1 || $(el).attr('href').indexOf('.docx') != -1) {
				$(el).attr('target', '_blank');
			}
		}
	});

	if(typeof slideshow_handler == 'object') {
		slideshow_handler.setup();
	}
	
	$("#single-product-item a[rel=product-photo]").fancybox({
		//'width'			: 460,																									
		'href'			: this.href,
		'overlayOpacity' : 0.5,
		'overlayColor': '#000',
		'titlePosition': 'over',
		'padding': 15,
		'centerOnScroll': true,
		'onClosed': function() {
		},
		'onCleanup': function() {
		}					
	});

	if(is_safari) {
		$('body').addClass('browser-safari');
	}

	if(is_opera) {
		$('body').addClass('browser-opera');
	}

	$('.default-txt tr').map(function(i, el){
		if(i%2==0) {
			$(el).addClass('odd');
		} else {
			$(el).addClass('even');
		}
    })
	
	if(typeof header_slideshow == 'object') {
		 header_slideshow.setup();
	}	

	if(typeof content_slideshow == 'object') {
		 content_slideshow.setup();
	}
	
	
	//var jcarousel_wrap = (ie6 || ie7 || ie8) ? 'both' : 'circular';
	var jcarousel_wrap = 'circular';
	
	$('#products-listing .jcarousel-category-carousel').map(function(i, el){
		var items_no = $('li', el).length;

		if(items_no <=5) {
			jQuery(el).jcarousel({itemFallbackDimension: 181});
		} else {
			jQuery(el).jcarousel({
				scroll: 5,
				wrap: jcarousel_wrap,
				itemVisibleInCallback: {
					onAfterAnimation:  function() {
					}
				},
				itemVisibleOutCallback: {
					onBeforeAnimation: function(){
					},
					onAfterAnimation:  function(){
					}
				},
			
				itemFallbackDimension: 181
			});
		}	

	});
	
	
});



var header_slideshow = {
    container: null,
    total_items: 4,
    last_item: null,
    delay: 6500,
    
    setup: function()  {
		setTimeout(function(){ header_slideshow.changePic(); }, header_slideshow.delay);
		last_item = 0;
		this.total_items = $('#header-slideshow .photo').length;
    },
    
    changePic: function() {
		var last_pic = $('#header-slideshow .active');
		var next_index = this.last_item + 1;
		if(next_index == this.total_items) {
			next_index = 0;
		}
		
		var last_pic = $('#header-slideshow .active');
		$(last_pic).animate({
			opacity: 0
		  }, 1000, function() {
			// Animation complete.
		});
		setTimeout(function(){$(last_pic).removeClass('active')}, header_slideshow.delay);
	
		var next_pic = $('#header-slideshow .photo')[next_index];
		$(next_pic).css({'opacity':'0'});
		$(next_pic).addClass('active');
		$(next_pic).animate({
			opacity: 1
		  }, 1000, function() {
			// Animation complete.
		});
		
		this.last_item = next_index;
		setTimeout(function(){ header_slideshow.changePic(); }, header_slideshow.delay);
    }
}


var content_slideshow = {
    container: null,
    total_items: 4,
    last_item: null,
    delay: 6500,
    
    setup: function()  {
		setTimeout(function(){ content_slideshow.changePic(); }, content_slideshow.delay);
		last_item = 0;
		this.total_items = $('#content-slideshow .photo').length;
    },
    
    changePic: function() {
		var last_pic = $('#content-slideshow .active');
		var next_index = this.last_item + 1;
		if(next_index == this.total_items) {
			next_index = 0;
		}
		
		var last_pic = $('#content-slideshow .active');
		$(last_pic).animate({
			opacity: 0
		  }, 1000, function() {
			// Animation complete.
		});
		setTimeout(function(){$(last_pic).removeClass('active')}, content_slideshow.delay);
	
		var next_pic = $('#content-slideshow .photo')[next_index];
		$(next_pic).css({'opacity':'0'});
		$(next_pic).addClass('active');
		$(next_pic).animate({
			opacity: 1
		  }, 1000, function() {
			// Animation complete.
		});
		
		this.last_item = next_index;
		setTimeout(function(){ content_slideshow.changePic(); }, content_slideshow.delay);
    }
}


