/*--------------------------------------------------
home showreel
--------------------------------------------------*/
function homeShowreel(){
	$(".showreel").each(function(){
		var $this = $(this);
		var $left = $this.find(".arr_prev a");
		var $right = $this.find(".arr_next a");
		var $img = $this.find(".img");
		var $items = $this.find(".item");
		var $wrapper = $this.find(".wrapper");

		
		
		$right.click(function(){
			$this.find(".item:eq(0)").animate({"margin-left":-160},500,function(){
				$(this).appendTo($wrapper).css({"margin-left":10});
			});
			return false;
		});
		$left.click(function(){
			$this.find(".item:last").css({"margin-left":-160}).prependTo($wrapper).animate({"margin-left":10},500);
			return false;
		});
	});
}


/*--------------------------------------------------
home tabs
--------------------------------------------------*/
function homeTabs(){
	var tabs =	$(".home_tabs .tabs_link li").size();
	$(".home_tabs .tabs_link").addClass("tabs_link"+tabs);
	$(".home_tabs .tabs_link a").click(function(){
		$(".home_tabs .tabs_link a.on").removeClass("on");
		$(this).addClass("on");
		$(".home_tabs .tab").hide();
		$($(this).attr("href")).show();
		return false;
	});
	$(".home_tabs .tabs_link a:eq(0)").click();
}


/*--------------------------------------------------
carosel
--------------------------------------------------*/
function carosel1(){
	var s = $(".carosel").size();
	var j = 0;
	var x = s-1;
	
	// switch carosel
	function switchCarosel(i){
		j = j+1;
		$(".carosel:eq(" + i + ")").hide().css({"z-index":j}).fadeIn(2000);
	}
	
	// prev
	$("#carosel a.arr_prev").click(function(){
		if (x == s-1) {
			x = 0;
		} else {
			x++;
		}
		switchCarosel(x);
		return false;
	});
	
	// next
	$("#carosel a.arr_next").click(function(){
		if (x == 0){
			x = s-1;
		} else {
			x--;
		}
		switchCarosel(x);
		return false;
	});
}



/*--------------------------------------------------
carosel
--------------------------------------------------*/
function carosel(){
	var s = $(".carosel").size();
	var j = 0;
	var easing = "easeOutCubic";
	var loop;
	
	// carosel size
	$(".carosel").each(function(i){
		$(this).addClass("carosel_" + i);
	});
	$(".carosel").width($("#carosel").width());
	$(window).resize(function(){
		$(".carosel").width($("#carosel").width());
	});

	// pallini
	a = (s*15)/2;
	for (i = 0; i < s; i++) {
		$("#carosel .balls").css({"margin-left":"-" + a + "px"}).append('<div class="ball"><a href="#">' + i + '</a></div>');
	}
	$("#carosel .balls a").live("click",function(){
		t = $("#carosel .ball").index($(this).parent());
		moveOn(t);
		return false;
	});
	

	// arrows gestures
	$(".arr_next").click(function(){
		j = (j < (s-1)) ? j+1 : 0;
		moveOn(j);
		return false;
	});
	$(".arr_prev").click(function(){
		j = (j == 0) ? (s-1) : j-1;
		moveOn(j);
		return false;
	});
	
	function moveOn(c){
		j = c;
		$("#carosel .ball a").removeClass("on").eq(c).addClass("on");
		$(".carosel_wrap").animate({left:"-" + ($("#carosel").width()) * c + "px"},1000);
		clearTimeout(loop);
		loop = setTimeout(function(){
			j = (j < (s-1)) ? j+1 : 0;
			moveOn(j);
		},8000);
	}
	moveOn(0);
	
	
}


/*--------------------------------------------------
round items
--------------------------------------------------*/
function roundItems(){
	$(".item_popup").css({opacity:0});
	var t;
	if ($.browser.msie) {
		t = 1;
	} else {
		t = 500;
	}
	
	$(".item .link a").mouseover(function(){
		var itm = $(this).parents(".item");
		var img = itm.find("img").attr("src");
		$(".item_popup .tit").html(itm.find(".tit").html());
		$(".item_popup .desc").html(itm.find(".desc").html());
		$(".item_popup .img").html(itm.find(".img").html());

		var i = $(".item").index(itm);
		
		$(".item_popup").css({left:-50 + "px",top:-30+"px"});
		
		$(".home_showreel .arr_prev a, .home_showreel .arr_next a").css({"z-index":0});
		itm.css({"z-index":19});

		$(".item_popup").css({filter:""}).appendTo(itm).stop().animate({opacity:t},10,function(){
			$(this).css({filter:""}).find(".tit").show();
		});
	}).mouseout(function(){
		var itm = $(this).parents(".item");
		itm.css({"z-index":1});
		$(".item_popup").stop().animate({opacity:0},200,function(){
			$(this).find(".tit").hide();
		});
		$(".home_showreel .arr_prev a, .home_showreel .arr_next a").css({"z-index":10});
	});
}

/*--------------------------------------------------
BLOG
--------------------------------------------------*/
function blogReminderLink() {
    $(".blog_reminder .desc a").attr("target", "_blank")
}

/*--------------------------------------------------
INIT
--------------------------------------------------*/
$(function () {
    homeShowreel();
    homeTabs();
    carosel();
    roundItems();
    blogReminderLink();
});
