$(document).ready(function() {
	carousel_init();
});

var max_length;
var max_diff = 194;
var left_diff = 582;
var curr_div = 3;
var sh_div = 3;
var in_animation_hover = false;
htmn2 = '';
function carousel_init()
{
	htmn = '';
	j = 0;
	for (i in mycarousel_itemList) {
		itmie = mycarousel_itemList[i];
		htmn += '<li class='+(parseInt(j)+1)+'>';
		htmn += '<a href="video.php?front_id='+itmie.front_id+'"><div style="background:url(\''+base_path+'images/medium_back_image.png\') no-repeat; height:108px; width:180px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:108px; width:180px; position : relative"><div class="play"></div><div class="content"><div class="">'+itmie.title+'</div><div>'+itmie.sub_title+'</div></div></div></div></a>';
		htmn += '</li>';
		if  (j == 0) {
			fhtmn = '<li class='+(parseInt(j)+1)+'>';
			fhtmn += '<a href="video.php?front_id='+itmie.front_id+'"><div style="background:url(\''+base_path+'images/medium_back_image.png\') no-repeat;  height:108px; width:180px"><div style="background:url(\''+itmie.url+'\') no-repeat;  height:108px; width:180px; position : relative"><div class="play"></div><div class="content"><div class="">'+itmie.title+'</div><div>'+itmie.sub_title+'</div></div></div></div></a>';
			fhtmn += '</li>';
		}
		if  (j == 1) {
			fhtmn2 = '<li class='+(parseInt(j)+1)+'>';
			fhtmn2 += '<a href="video.php?front_id='+itmie.front_id+'"><div style="background:url(\''+base_path+'images/medium_back_image.png\') no-repeat; height:108px; width:180px"><div style="background:url(\''+itmie.url+'\') no-repeat;  height:108px; width:180px; position : relative"><div class="play"></div><div class="content"><div class="">'+itmie.title+'</div><div>'+itmie.sub_title+'</div></div></div></div></a>';
			fhtmn2 += '</li>';
		}
		if  (j == 2) {
			fhtmn3 = '<li class='+(parseInt(j)+1)+'>';
			fhtmn3 += '<a href="video.php?front_id='+itmie.front_id+'"><div style="background:url(\''+base_path+'images/medium_back_image.png\') no-repeat; height:108px; width:180px"><div style="background:url(\''+itmie.url+'\') no-repeat;  height:108px; width:180px; position : relative"><div class="play"></div><div class="content"><div class="">'+itmie.title+'</div><div>'+itmie.sub_title+'</div></div></div></div></a>';
			fhtmn3 += '</li>';
		}
		
		
		if (j == (mycarousel_itemList.length - 3)) {
			lhtmn3 = '<li class='+(parseInt(j)+1)+'>';
			lhtmn3 += '<a href="video.php?front_id='+itmie.front_id+'"><div style="background:url(\''+base_path+'images/medium_back_image.png\') no-repeat; height:108px; width:180px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:108px; width:180px; position : relative"><div class="play"></div><div class="content"><div class="">'+itmie.title+'</div><div>'+itmie.sub_title+'</div></div></div></div><a>';
			lhtmn3 += '</li>';
		}
		if (j == (mycarousel_itemList.length - 2)) {
			lhtmn2 = '<li class='+(parseInt(j)+1)+'>';
			lhtmn2 += '<a href="video.php?front_id='+itmie.front_id+'"><div style="background:url(\''+base_path+'images/medium_back_image.png\') no-repeat; height:108px; width:180px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:108px; width:180px; position : relative"><div class="play"></div><div class="content"><div class="">'+itmie.title+'</div><div>'+itmie.sub_title+'</div></div></div></div></a>';
			lhtmn2 += '</li>';
		}
		if (j == (mycarousel_itemList.length - 1)) {
			lhtmn = '<li class='+(parseInt(j)+1)+'>';
			lhtmn += '<a href="video.php?front_id='+itmie.front_id+'"><div style="background:url(\''+base_path+'images/medium_back_image.png\') no-repeat; height:108px; width:180px"><div style="background:url(\''+itmie.url+'\') no-repeat;  height:108px; width:180px; position : relative"><div class="play"></div><div class="content"><div class="">'+itmie.title+'</div><div>'+itmie.sub_title+'</div></div></div></div></a>';
			lhtmn += '</li>';
		}
		j++;
	}
	wdt = mycarousel_itemList.length * max_diff;
	nylft =  (3 * 194);
	new_html = '<div class="main_carousel"><div class="left-nav"><!--[if IE 6]><img src="'+base_path+'images/medium_left_arrow.png"/><![endif]--><img src="'+base_path+'images/medium_left_arrow.png"/></div><div class="inner_carousel" style="top : 0px; left : -'+nylft+'px;"><ul>'+lhtmn3+lhtmn2+lhtmn+htmn+fhtmn+fhtmn2+fhtmn3+'</ul></div><div class="right-nav"><!--[if IE 6]><img src="'+base_path+'images/medium_right_arrow.png"/><![endif]--><img src="'+base_path+'images/medium_right_arrow.png"/></div></div>';
	$('#mycarousel').html(new_html);
	max_length = wdt;
	wdt = wdt + (6 * 194);
	$('.inner_carousel ul').css({'width' : wdt+'px'});
	
	$('div.left-nav').hover(show_nav, hide_nav);
	$('div.right-nav').hover(show_nav, hide_nav);
	$('div.left-nav').bind('click', nav_left);
	$('div.small-left-nav').bind('click', nav_left);
	$('div.right-nav').bind('click', nav_right);
	$('div.small-right-nav').bind('click', nav_right);
	for(j=1; j<=counter_count;j++) {
		$('div.nav-block-'+j).bind('click', nav_goto);
	}
	show_div_init();
	
	$('.inner_carousel').find('li').hover(show_div,hide_div);
	//$('.inner_carousel').find('li:eq(3)').trigger('mouseover');
}

function show_nav()
{
	if ($(this).hasClass('hovered')) {
		return;
	}
	$(this).addClass('hovered');
	$(this).find('img').fadeIn(250);
	/*if ($(this).hasClass('left-nav')) {
		nav_left();
	} else {
		nav_right();
	}*/
}

function hide_nav()
{
	if (!$(this).hasClass('hovered')) {
		return;
	}
	$(this).removeClass('hovered');
	$(this).find('img').fadeOut(150);
}

function nav_left()
{	
	if (in_animation_hover) {
		return;
	}
	if ($('div.left-nav').hasClass('hovered')) {
		eleft = $('.inner_carousel').css('left');
		eleft = eleft.replace('px', '');
		new_left = parseInt(eleft) + max_diff;
		if (new_left > 313) {
			return;
		}
		hide_div();
		$('.nav-block-'+(curr_div)).find('img').attr('src', base_path+'images/image-noselected.png');
		$('.nav-block-'+(curr_div)).removeClass('current_image');
		curr_div = curr_div - 1;
		$('.nav-block-'+curr_div).addClass('current_image');
		$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
		in_animation_hover = true;
		$('.inner_carousel').animate({'left' : new_left+'px'}, 1000, function () {
			if (curr_div <= 0) {
				curr_div = mycarousel_itemList.length;
				$('.nav-block-'+curr_div).addClass('current_image');
				$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
				nylft = max_length;
				$('.inner_carousel').css({'left' : '-'+nylft+'px'});
			}
			in_animation_hover = false; 
			//show_div(); 
			//nav_left(); 
		});
	}
	if ($(this).hasClass('small-left-nav')) {
		eleft = $('.inner_carousel').css('left');
		eleft = eleft.replace('px', '');
		new_left = parseInt(eleft) + max_diff;
		if (new_left > 313) {
			return;
		}
		hide_div();
		$('.nav-block-'+(curr_div)).find('img').attr('src', base_path+'images/image-noselected.png');
		$('.nav-block-'+(curr_div)).removeClass('current_image');
		curr_div = curr_div - 1;
		$('.nav-block-'+curr_div).addClass('current_image');
		$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
		in_animation_hover = true;
		$('.inner_carousel').animate({'left' : new_left+'px'}, 1000, function () {
			if (curr_div <= 0) {
				curr_div = mycarousel_itemList.length;
				$('.nav-block-'+curr_div).addClass('current_image');
				$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
				nylft = max_length ;
				$('.inner_carousel').css({'left' : '-'+nylft+'px'});
			}
			in_animation_hover = false; 
			//show_div(); 
			//nav_left(); 
		});
	}
}

function nav_right()
{
	if (in_animation_hover) {
		return;
	}
	if ($('div.right-nav').hasClass('hovered')) {
		eleft = $('.inner_carousel').css('left');
		eleft = eleft.replace('px', '');
		new_left = parseInt(eleft) - max_diff;
		if (new_left + (5 * 194) < (left_diff - max_length)) {
			return;
		}
		hide_div();
		$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-noselected.png');
		$('.nav-block-'+curr_div).removeClass('current_image');
		curr_div = parseInt(curr_div) + 1;
		$('.nav-block-'+curr_div).addClass('current_image');
		$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
		in_animation_hover = true;
		$('.inner_carousel').animate({'left' : new_left+'px'}, 1000, function () {
			if (curr_div > mycarousel_itemList.length) {
				curr_div = 1;
				$('.nav-block-'+curr_div).addClass('current_image');
				$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
				nylft = 194 ;
				$('.inner_carousel').css({'left' : '-'+nylft+'px'});
			}
			in_animation_hover = false; 
			//show_div(); 
			//nav_right();
		});
	}
	if ($(this).hasClass('small-right-nav')) {
		eleft = $('.inner_carousel').css('left');
		eleft = eleft.replace('px', '');
		new_left = parseInt(eleft) - max_diff;
		if (new_left + (5 * 194) < (left_diff - max_length)) {
			return;
		}
		hide_div();
		$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-noselected.png');
		$('.nav-block-'+curr_div).removeClass('current_image');
		curr_div = parseInt(curr_div) + 1;
		$('.nav-block-'+curr_div).addClass('current_image');
		$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
		in_animation_hover = true;
		$('.inner_carousel').animate({'left' : new_left+'px'}, 1000, function () {
			if (curr_div > mycarousel_itemList.length) {
				curr_div = 1;
				$('.nav-block-'+curr_div).addClass('current_image');
				$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-selected.png');
				nylft = 194 ;
				$('.inner_carousel').css({'left' : '-'+nylft+'px'});
			}
			in_animation_hover = false; 
			//show_div(); 
			//nav_right();
		});
	}
}
	

function nav_goto()
{
	if ($(this).hasClass('current_image')) {
		return;
	}
	cclass = $(this).attr('class');
	cclass = cclass.replace('nav-block-', '');
	if (cclass > mycarousel_itemList.length) {
		return;
	}
	hide_div();
	$('.nav-block-'+curr_div).find('img').attr('src', base_path+'images/image-noselected.png');
	$('.nav-block-'+curr_div).removeClass('current_image');
	curr_div = cclass;
	cclass = parseInt(cclass) - 1;
	image_left = parseInt(parseInt(cclass) * max_diff);
	
	if (image_left == 0) {
		new_left = - 194;
	} else {
		new_left = - 194 - image_left;
	}
	
	$(this).addClass('current_image');
	$(this).find('img').attr('src', base_path+'images/image-selected.png');
	$('.inner_carousel').animate({'left' : new_left+'px'}, 1000);
}

function show_div()
{	
	$(this).find('.play').fadeIn(500);
	$(this).find('.content').fadeIn(500);
	if (in_animation_hover) {
		return;
	}
	$('.alive').hide();
	$('.right_main_div').removeClass('alive');
	sh_div = $(this).attr('class');
	in_animation_hover = true;
	$('#content_'+sh_div).fadeIn(500,function () {
		in_animation_hover = false; 
		
	});
	$('#content_'+sh_div).addClass('alive');
		
}

function show_div_init()
{
	$('#content_'+sh_div).fadeIn(500);
	$('#content_'+sh_div).addClass('alive');
}

function hide_div()
{
	sh_div = $(this).attr('class');
	$(this).find('.play').hide();
	$(this).find('.content').hide();	
}

