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

var max_length;
var max_diff = 372;
var left_diff = 681;
var curr_div = 4;
var in_animation_hover = false;
htmn2 = '';
function carousel_init()
{
	htmn = '';
	j = 0;
	for (i in mycarousel_itemList) {
		itmie = mycarousel_itemList[i];
		htmn += '<li>';
		htmn += '<div style="background:url(\''+base_path+'images/shadow.png\') no-repeat; height:260px; width:322px;padding:8px; 9px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:258px; width:320px; position : relative"><a href="video.php?front_id='+itmie.front_id+'"><div class="play"></div></a><div class="content"><div class="firstname">';
		
		if ((itmie.first_name_image) == ''){
		
		} else {
		htmn	+='<img src="upload/images/'+itmie.first_name_image+'"/>&nbsp;';
		}
		if ((itmie.last_name_image)== ''){
		
		} else {
		htmn += '<img src="upload/images/'+itmie.last_name_image+'"/>';
		}
		htmn += '</div><div class="teaser">'+itmie.teaser+'</div></div></div></div>';
		htmn += '</li>';
		if  (j == 0) {
			fhtmn = '<li>';
			fhtmn += '<div style="background:url(\''+base_path+'images/shadow.png\') no-repeat; height:260px; width:322px;padding:8px; 9px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:258px; width:320px; position : relative"><a href="video.php?front_id='+itmie.front_id+'"><div class="play"></div></a><div class="content"><div class="firstname">';
			if ((itmie.first_name_image) == ''){
		
			} else {
			fhtmn	+='<img src="upload/images/'+itmie.first_name_image+'"/>&nbsp;';
			}
			if ((itmie.last_name_image)== ''){
			} else {
			fhtmn += '<img src="upload/images/'+itmie.last_name_image+'"/>';
			}
			fhtmn += '</div><div class="teaser">'+itmie.teaser+'</div></div></div></div>';
			fhtmn += '</li>';
		}
		if  (j == 1) {
			fhtmn2 = '<li>';
			fhtmn2 += '<div style="background:url(\''+base_path+'images/shadow.png\') no-repeat; height:260px; width:322px;padding:8px; 9px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:258px; width:320px; position : relative"><a href="video.php?front_id='+itmie.front_id+'"><div class="play"></div></a><div class="content"><div class="firstname">';
			if ((itmie.first_name_image) == ''){
		
			} else {
			fhtmn2	+= '<img src="upload/images/'+itmie.first_name_image+'"/>&nbsp;';
			}
			if ((itmie.last_name_image)== ''){
			
			} else {
			fhtmn2 += '<img src="upload/images/'+itmie.last_name_image+'"/>';
			}
			fhtmn2 += '</div><div class="teaser">'+itmie.teaser+'</div></div></div></div>';
			fhtmn2 += '</li>';
		}
		if (j == (mycarousel_itemList.length - 2)) {
			lhtmn2 = '<li>';
			lhtmn2 += '<div style="background:url(\''+base_path+'images/shadow.png\') no-repeat; height:260px; width:322px;padding:8px; 9px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:258px; width:320px; position : relative"><a href="video.php?front_id='+itmie.front_id+'"><div class="play"></div></a><div class="content"><div class="firstname">';
			if ((itmie.first_name_image) == ''){
		
			} else {
			lhtmn2	+='<img src="upload/images/'+itmie.first_name_image+'"/>&nbsp;';
			}
			if ((itmie.last_name_image)== ''){
			
			} else {
			lhtmn2 += '<img src="upload/images/'+itmie.last_name_image+'"/>asdfg';
			}
			lhtmn2 += '</div><div class="teaser">'+itmie.teaser+'</div></div></div></div>';
			lhtmn2 += '</li>';
		}
		if (j == (mycarousel_itemList.length - 1)) {
			lhtmn = '<li>';
			lhtmn += '<div style="background:url(\''+base_path+'images/shadow.png\') no-repeat; height:260px; width:322px;padding:8px; 9px"><div style="background:url(\''+itmie.url+'\') no-repeat; height:258px; width:320px; position : relative"><a href="video.php?front_id='+itmie.front_id+'"><div class="play"></div></a><div class="content"><div class="firstname">';
			if ((itmie.first_name_image) == ''){
		
			} else {
			lhtmn	+='<img src="upload/images/'+itmie.first_name_image+'"/>&nbsp;';
			}
			if ((itmie.last_name_image)== ''){
			
			} else {
			lhtmn += '<img src="upload/images/'+itmie.last_name_image+'"/>';
			}
			lhtmn += '</div><div class="teaser">'+itmie.teaser+'</div></div></div></div>';
			lhtmn += '</li>';
		}
		j++;
	}
	wdt = mycarousel_itemList.length * max_diff;
	nylft = 807 + (2 * 372);
	new_html = '<div class="main_carousel"><div class="left-nav"><!--[if IE 6]><img src="'+base_path+'images/scroll-left.gif"/><![endif]--><img src="'+base_path+'images/scroll-left.png"/></div><div class="inner_carousel" style="top : 30px; left : -'+nylft+'px;"><ul>'+lhtmn2+lhtmn+htmn+fhtmn+fhtmn2+'</ul></div><div class="right-nav"><!--[if IE 6]><img src="'+base_path+'images/scroll-right.gif"/><![endif]--><img src="'+base_path+'images/scroll-right.png"/></div></div>';
	$('#mycarousel').html(new_html);
	max_length = wdt;
	wdt = wdt + (4 * 372);
	$('.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();
}

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 + 63;
				$('.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 + 63;
				$('.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 + (3 * 372) < (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 = 372 + 63;
				$('.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 + (3 * 372) < (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 = 372 + 63;
				$('.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 = - 435;
	} else {
		new_left = - 435 - 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();});
}

function show_div()
{
	sdi = 1;
	$.each($('.inner_carousel ul li'), function () {
		if (sdi == (parseInt(curr_div) + 2)) {
			$(this).find('.play').fadeIn(750);
			$(this).find('.content').fadeIn(750);
		}
		sdi++;
	});
}

function hide_div()
{
	hdi = 1;
	$.each($('.inner_carousel ul li'), function () {
		if (hdi == (parseInt(curr_div) + 2)) {
			$(this).find('.play').hide();
			$(this).find('.content').hide();
		}
		hdi++;
	});
}