$(document).ready(function(){
	
/*
	if (Cookie.read('popup_live') != 'show') {
		openPopup(370, 220);
		var myCookie = Cookie.write('popup_live', 'show', { domain: 'loft.igmdevsite.com', duration: 99999 });
	}
*/
	
	$('.mailListButton').click(function(){
		openPopup(370, 220);
	});

	/* move arrow function */
   function moveArrow( to )
	{
		to = $("#splash .container .slider li").index(to);
		  
		if( to == $("#controllers li").length+1 ) to = 1;
		else if( to == -1 ) to = 2;
		
		$("#controllers ul li a").removeClass('current');
		$this = $("#controllers ul li a").eq(to-1);
		$this.addClass('current');
		$("#controllers .arrow").animate({ left: $this.position().left-2}, 250);
	}
	
	/* effects and animations */
	$("#menu ul li").append("<div class='right'></div>").children('a').click(function(){
		$("#menu ul li").removeClass('current');
		$(this).parent('li').addClass('current');
	});
	$("#help .hover").css({ opacity: 0, width: '1px'});

	$("#help").hover(function(){
	  $("#help .hover").stop().animate({ width: '150px', opacity: 1}, 250);
	}, function(){
	  $("#help .hover").stop().animate({ width: '1px', opacity: 0}, 500);
	});
	
	/* equal height to columsn */
	$(".columns").each(function(){
		$(this).children("div.left, div.right").css("height", $(this).height() + "px");
	});
	
	/* shop categories last item special */
	$("#categories > ul > li:last").css("background", "none");
	$("#categories > ul > li.current").next().slideDown();
	$("#categories ul li.parent a").click(function(){
			$("#categories ul li.parent li.current").removeClass('current');
			$(this).parent().addClass('current');
			if( $("#breadcrumb_cat strong").length == 1 )
			{
				$("#breadcrumb_cat").html("<a href='#'>" + $("#breadcrumb_cat strong").text() + "</a>/");
			}
			$("#breadcrumb_subcat").html("<strong>"+$(this).text()+"</strong>").slideDown(225);
	});
	$("#categories > ul > li a").click(function(){
		if( $(this).attr('href') == '#' && !$(this).parents('li').hasClass('parent') && $(this).parent().next('li.parent').length == 1 && !$(this).parent().hasClass('current') )
		{
			$("#categories > ul > li.parent").slideUp().prev().removeClass('current');
			$(this).parent().addClass('current').next('li.parent').slideToggle();
			$(this).addClass('current');
			$("#breadcrumb_cat").slideUp(225).next().slideUp(225);
			$this = $(this);
			setTimeout(function(){
				$("#breadcrumb_cat").html("<strong>"+$this.text()+"</strong>").slideDown(225);
			}, 250);
			return false;
		}
		return true;
	});
	
	
	/* breadcrumbs slide */
	var i = 0;
	$("#breadcrumbs a").hide().each(function(){
		var $this = $(this);
		setTimeout(function(){
			$this.slideDown(225);
		}, 250*i);
		i++;
	});
	
	/* carousel with text and caption | bæta við controllers hér: p6, p7, p8, p9 osfrv. */
	var buttons = ["#controllers ul li a.p1","#controllers ul li a.p2","#controllers ul li a.p3","#controllers ul li a.p4","#controllers ul li a.p5"];
	
	$("#slider .photos .handler").jCarouselLite({
		btnGo: buttons,
		speed: 750
	});
	$("#splash .container .slider").jCarouselLite({
		btnGo: buttons,
		vertical: true,
		speed: 750,
		beforeStart: function(e){
			moveArrow( e );
		}
	});
	
	/* overlay */
	$("#overlay").each(function(){
		$(this).height($(document).height());
	});
});

function openPopup($width, $height)
{
	var marginLeft = $width/2
	$("#overlay").show().animate({ opacity: 0.3 }, 300);
		setTimeout(function(){
		$("#popup_window").hide();
		$("#popup #close").hide()
		$("#popup").animate({'marginLeft': -marginLeft, 'width': $width, 'height': $height}, 1000);
		
		setTimeout(function(){
			$("#popup #close").show().stop().animate({ top: "-22px"}, 600);
		}, 1330);
		setTimeout(function(){
			$("#popup_window").fadeIn(600);
		}, 1430);
	}, 150);
		
	$("#popup #close, #popup #close a").live("click", function(){
		closePopup();
		return false;
	});
}

function closePopup()
{
	$("#popup").fadeOut(500);
	setTimeout(function(){
		$("#overlay").fadeOut(500);
	}, 250);
}

function sendProductRequest(productName, productImg) {
	openPopup(700, 330);
	
	$("#productRequestText").text(productName);
	$("#productRequestImg").attr('src', productImg);
	$("#popup textarea").text('Vöruheiti: '+productName+"\n");
}
