var SlideshowHome = {
	pos: 1,
	ids: [ ],
	current: 1,
	timeout: 0,
	auto: function(mode) {
		if (mode) {
			var f = function() {
				SlideshowHome.click(++SlideshowHome.current);
				if (SlideshowHome.current>=SlideshowHome.ids.length)
					SlideshowHome.current = 0;
			}
			this.timeout = setInterval(f, 3000);
		} else
			clearTimeout(this.timeout);
	},
	click: function(to) {
		$(".images-list ul .active").removeClass("active");
		$(".images-list ul li:eq("+(to-1)+")").addClass("active");
		
//	    $(".home-image").css("background", "url("+$(".home-image img").attr("src")+") no-repeat");
//	    $(".home-image .big").css("opacity",0).css("filter","alpha(opacity=0)").attr("src", $(".images-list ul li:eq("+(to-1)+") a").attr("href"));
	    $(".home-image .description a").html($(".images-list ul li:eq("+(to-1)+") a").find("img").attr("alt")).attr("href", $(".images-list ul li:eq("+(to-1)+") a").find("img").attr("rel"));
	    $(".home-image .description span").html($(".images-list ul li:eq("+(to-1)+") a").find("img").attr("content")).attr("href", $(".images-list ul li:eq("+(to-1)+") a").find("img").attr("content"));
	    $(".home-image .mlink").attr("href", $(".images-list ul li:eq("+(to-1)+") a").find("img").attr("rel"));
	    $(".home-image .description-back").css("height", $(".home-image .description ").height() + 13); 
	
		$(".slideshow .big").attr("src", $(".images-list ul li:eq("+(to-1)+") a").attr("href"));
	  
		this.scroll(to);
	},
	scroll: function(to) {
		var len = $(".images-list ul li").length;
		if (to<1)
			to = 1;
		if (to>len)
			to = len;

		this.pos = to;
		var x;
		switch (parseInt(to)) {
			case 1: x = 0; break;
			case 2: x = 0; break;
			case len: x = -(to-4)*105; break;
			case len-1: x = -(to-3)*105; break;
			default: x = -(to-2.5)*105; break;
		}

		var x1 = parseInt($(".images-list ul").css("left"));
		var step = (x-x1)/10;
		var go = function() {
			x1 += step;
			for (var i=1; i<=10; i++)
				$(".images-list ul").css("left", x1+"px");
			return Math.abs(x1-x)>Math.abs(step);
		}
		
		var timeout = function() {
			if (go())
				setTimeout(timeout, 20);
			else
				$(".images-list ul").css("left", x+"px");
		}
		setTimeout(timeout, 20);
		
		return false;
	}
}

var Slideshow = {
	pos: 1,
	ids: [ ],
	current: 1,
	timeout: 0,
	auto: function(mode) {
		if (mode) {
			var f = function() {
				Slideshow.click(++Slideshow.current);
				if (Slideshow.current>=Slideshow.ids.length)
					Slideshow.current = 0;
			}
			this.timeout = setInterval(f, 2000);
		} else
			clearTimeout(this.timeout);
	},
	click: function(to) {
		$(".slideshow .big").attr("src", $(".images-list ul li:eq("+(to-1)+") a").attr("href"));
		$(".images-list ul .active").removeClass("active");
		$(".images-list ul li:eq("+(to-1)+")").addClass("active");
		this.scroll(to);
	},
	scroll: function(to) {
		var len = $(".images-list ul li").length;
		if (to<1)
			to = 1;
		if (to>len)
			to = len;

		this.pos = to;
		var x;
		switch (parseInt(to)) {
			case 1: x = 0; break;
			case 2: x = 0; break;
			case len: x = -(to-4)*105; break;
			case len-1: x = -(to-3)*105; break;
			default: x = -(to-2.5)*105; break;
		}

		var x1 = parseInt($(".images-list ul").css("left"));
		var step = (x-x1)/10;
		var go = function() {
			x1 += step;
			for (var i=1; i<=10; i++)
				$(".images-list ul").css("left", x1+"px");
			return Math.abs(x1-x)>Math.abs(step);
		}
		
		var timeout = function() {
			if (go())
				setTimeout(timeout, 20);
			else
				$(".images-list ul").css("left", x+"px");
		}
		setTimeout(timeout, 20);
		
		return false;
	}
}


// Submenus timer
function subMenuTimer() {
	if (!$('.menu li').hasClass('hover') && !$('.submenus').hasClass('hover')) {
		$('.submenus').remove();
	}
	else if (!$('.menu li').hasClass('hover') && !$('.submenu-special').hasClass('hover')) {
		$('.submenu-special').remove();
	}
}

$(document).ready(function() {
	// Submenus
	$('.menu li').hover(
		function() {
			var curClasses = $(this).attr('class');
			var currentMenu = $(this);
			
			if (curClasses.indexOf('universe-') != -1) {
				var curId = parseInt(curClasses.substr(9));
				
				$.getJSON('/submenus', 'id=' + curId, function(data) {
					var items = [];
					var inMenu = false;
					
					$('.submenus').remove();
					$('.submenu-special').remove();
					if (curId > 0) {
						$.each(data, function(key, val) {
							if (val['parent'] == true) {
								if (inMenu) {
									items.push('</ul>');
								}
								items.push('<ul class="submenu"><li class="title"><a href="' + val['url'] + '">' + val['name'] + '</a></li>');
								inMenu = true;
							}
							else {
								items.push('<li><a href="' + val['url'] + '">' + val['name'] + '</a></li>');
							}
						});
						
						if (inMenu) {
							items.push('</ul>');
						}
						$('.menu').after('<div class="submenus">' + items.join('') + '</div>');
						if(curId==32)
							$('.submenus').css('margin-left', '20px');
						if(curId==30)
							$('.submenus').css('margin-left', '80px');
						if(curId==35)
								$('.submenus').css('margin-left', '250px');

						// Set height
						var maxHeight = 0;
						$('.submenus .submenu').each(function(index) {
							maxHeight = ($(this).height() > maxHeight) ? $(this).height() : maxHeight;
						});
						$('.submenus .submenu').each(function(index) {
							$(this).css('height', maxHeight + 'px');
						});
						
						$('.submenus').hover(
							function() {
								$(this).addClass('hover');
							},
							function() {
								$(this).remove();
							}
						);
					}
					else {
						// Special static submenu
						
						items.push('<ul>');
						
						$.each(data, function(key, val) {
							var itemHtml = '<li><a href="' + val['url'] + '"';
							if (val['target']) {
								itemHtml += ' target="' + val['target'] + '"';
							}
							itemHtml += '>' + val['name'] + '</a></li>';
							
							items.push(itemHtml);
						});
						
						items.push('</ul>');
						
						$('.menu').after('<div class="submenus submenu-special" style="left: ' + currentMenu.offset().left + 'px; top: ' + parseInt(currentMenu.offset().top + 27) + 'px;">' + items.join('') + '</div>');
						
						$('.submenu-special').hover(
							function() {
								$(this).addClass('hover');
							},
							function() {
								$(this).remove();
							}
						);
					}
				});
			}
		},
		
		function() {
			window.setTimeout(subMenuTimer, 200);
		}
	);
	
	$('#product-tab-dates .prices-short tr.date').hover(
		function() {
			$(this).css('background-color', '#d7f1f6');
		},
		function() {
			$(this).css('background-color', '#ffffff');
		}
	);
	
	$('.other-products li').hover(
		function() {
			$(this).css('background-color', '#d7f1f6');
		},
		function() {
			$(this).css('background-color', '#ffffff');
		}
	);
	
	// 4-images panel
	$('.right4ads a').hover(function() {
		var img = $(this).find('img');
		
		var size = img.attr('src').substr(8);
		size = size.substr(0, size.indexOf('/'));
		
		var name = img.attr('src').substr(8);
		name = name.substr(name.indexOf('/') + 1);
		
		var link = $(this).attr('href');
		var target = $(this).attr('target');
		target = (target) ? target : '_self';
		
		if (size == '146x82') {
			$('.right4ads a').css('display', 'none');
			
			$('.right4ads').append('<a class="bigimage" href="' + link + '" target="' + target + '"><img src="/images/300x168/' + name + '" /></a>');
		}
	});
	
	$('.right4ads').hover(
			function() {},
			
			function() {
				$('.right4ads .bigimage').remove();
				$('.right4ads a').css('display', 'block');
			}
	);
	
	// Partner
	var tPartner = 0;
	var partnerIn = false;
	var partnerSpeed = 0;
	var partnerWait = 2000;
	
	function partnerTimer()
	{
		$('.partner .is-current img').animate({
			left: '-=210'
		}, partnerSpeed, 'linear', function() {
			var next = $('.partner .is-hidden').first();
			var lastIsCurrent = false;
			var done = false;
			$('.partner a').each(function(index) {
				if (!done) {
					if (lastIsCurrent) {
						next = $(this);
						done = true;
					}
					
					if ($(this).hasClass('is-current')) {
						lastIsCurrent = true;
					}
				}
			});
			
			$('.partner .is-current').addClass('is-hidden').removeClass('is-current');
			next.addClass('is-current').removeClass('is-hidden');
			
			next = next.find('img');
			next.css('left', '290px');
			
			next.animate({
				left: '0'
			}, partnerSpeed, 'linear', function() {
				if (!partnerIn) {
					window.clearTimeout(tPartner);
					tPartner = window.setTimeout(partnerTimer, partnerWait);
				}
			});
		});
	}
	
	tPartner = window.setTimeout(partnerTimer, partnerWait);
	
	$('.partner').hover(
		function() {
			window.clearTimeout(tPartner);
			partnerIn = true;
		},
		
		function() {
			window.clearTimeout(tPartner);
			tPartner = window.setTimeout(partnerTimer, 500);
			partnerIn = false;
		}
	);
	
	// Product Photos
	var tPhoto = 0;
	var photoIn = false;
	var photoSpeed = 0;
	var photoWait = 2000;
	
	function photoTimer()
	{
		$('.photo .is-current img').animate({
			left: '-=290'
		}, photoSpeed, 'linear', function() {
			var next = $('.photo .is-hidden').first();
			var lastIsCurrent = false;
			var done = false;
			$('.photo a').each(function(index) {
				if (!done) {
					if (lastIsCurrent) {
						next = $(this);
						done = true;
					}
					
					if ($(this).hasClass('is-current')) {
						lastIsCurrent = true;
					}
				}
			});
			
			$('.photo .is-current').addClass('is-hidden').removeClass('is-current');
			next.addClass('is-current').removeClass('is-hidden');
			
			next = next.find('img');
			next.css('left', '305px');
			
			next.animate({
				left: '-=290'
			}, photoSpeed, 'linear', function() {
				if (!photoIn) {
					window.clearTimeout(tPhoto);
					tPhoto = window.setTimeout(photoTimer, photoWait);
				}
			});
		});
	}
	
	tPhoto = window.setTimeout(photoTimer, photoWait);
	
	$('.photo').hover(
		function() {
			window.clearTimeout(tPhoto);
			photoIn = true;
		},
		
		function() {
			window.clearTimeout(tPhoto);
			tPhoto = window.setTimeout(photoTimer, 500);
			photoIn = false;
		}
	);	
});
