$(function(){
	var gcount = 0;
	var imgcount = $('#gallery img').hide().size();
	$('#gallery img').eq(0).show();
	$('#gchange li').removeClass('active');
	$('#gchange li').eq(0).addClass('active');
	function slideshow(){
		slideshowInterval = setInterval(function(){
			$('#gallery img').eq(gcount).fadeOut(600);
			gcount++;
			if (gcount == imgcount){
				gcount = 0;
			}
			$('#gchange li').removeClass('active');
			$('#gallery img').eq(gcount).fadeIn(600);
			$('#gchange li').eq(gcount).addClass('active');
		}, 4000);
	}
	slideshow();
	$('#gchange li').click(function(){
		if (!$(this).hasClass('active')){
			$('#gchange li').removeClass('active');
			$(this).addClass('active');
			$('#gallery img').eq(gcount).fadeOut(600);
			var num = $(this).text() - 1;
			$('#gallery img').eq(num).fadeIn(600);
			gcount = num;
			clearInterval(slideshowInterval);
			slideshow();
		}
	});
	
	//keep here the initial count of votes when page loded first time.

	
	var pollstatus = true;
	var last_id="";
	//var init_votes[][];
	$('#pollopt li a').click(function(){
	var pool_id=this.id.split('^');
	pool_id[1]=document.getElementById("poll_id").value;
		if (pollstatus){
			var adata=document.getElementById("last_id").innerHTML.split('^');
			if(adata[1]!="0")
			{
			var pt=document.getElementById(adata[0]+"^id");
			vote = $(pt).siblings('span').text().split(' ');
			vote[0]--;
			nText = vote[0] + ' ' + vote[1];
			$(pt).siblings('span').text(nText);
			}
		vote = $(this).siblings('span').text().split(' ');
			last_id=this.id;
		
			vote[0]++;
			nText = vote[0] + ' ' + vote[1];
			$(this).siblings('span').text(nText);
			$('#pollopt li > span').addClass('vote_on');
			$('#pollopt li a').addClass('complete');
			$(this).addClass('selected');
			var pollcount = $('#pollopt li').size();
			var maxWidth = 0;
			for (j=0; j<pollcount; j++){
				temp = parseInt($('#pollopt li .vote').eq(j).text());
				if (temp > maxWidth){
					maxWidth = temp;
				}
			}
			for (j=0; j<pollcount; j++){
				var temp = parseInt($('#pollopt li').eq(j).children('span').text());
				pwidth = 75 + 135*temp/maxWidth;
				$('#pollopt li a').eq(j).animate({
					width: pwidth + 'px'
				}, 800);
			}
		
			poll_vote(pool_id[1],pool_id[0],last_id);
		}
		else {
		
			/////////////mevoing last enty data////////////////////
			var pt=document.getElementById(last_id);
			vote = $(pt).siblings('span').text().split(' ');
			vote[0]--;
			nText = vote[0] + ' ' + vote[1];
			$(pt).siblings('span').text(nText);
			$('#pollopt li > span').removeClass('vote_on');
			$('#pollopt li a').removeClass('complete');
			$('#pollopt li a').removeClass('selected');
			//pool_vote_remove(pool_id[2],pool_id[1]);					
			////////////////////////////////////////////
			
			vote = $(this).siblings('span').text().split(' ');
			vote[0]++;
			nText = vote[0] + ' ' + vote[1];
			$(this).siblings('span').text(nText);
			$('#pollopt li > span').addClass('vote_on');
			$('#pollopt li a').addClass('complete');
			$(this).addClass('selected');
			
			var pollcount = $('#pollopt li').size();
			var maxWidth = 0;
			for (j=0; j<pollcount; j++){
				temp = parseInt($('#pollopt li .vote').eq(j).text());
				if (temp > maxWidth){
					maxWidth = temp;
				}
			}
			for (j=0; j<pollcount; j++){
				var temp = parseInt($('#pollopt li').eq(j).children('span').text());
				pwidth = 75 + 135*temp/maxWidth;
				$('#pollopt li a').eq(j).animate({
					width: pwidth + 'px'
				}, 800);
			}
			//alert('Your vote has been cast. Thanks !');
			last_id=this.id;
			poll_vote(pool_id[1],pool_id[0],last_id);
		}
		pollstatus = false;
	});

	$('#sidebar2 .tabs a').click(function(){
		if (!$(this).hasClass('current')){
			$cols = $('#content1 > div');
			$cols.height('auto');
			$('#sidebar2 .tabs a').removeClass('current').removeAttr('href');
			$('#sidebar2 .links').fadeOut("slow").css('display', 'none');
			$(this).addClass('current');
			list = '#sidebar2 #' + $(this).attr('rel');
			$(list).fadeIn("slow").css('display', 'block');
			var colht = 0;
			for (i=0; i<3; i++){
				if ($cols.eq(i).height() > colht){
					colht = $cols.eq(i).height();
				}
			}
			$cols.height(colht);
		}
	});
	
	$('#authors .detail img').mouseover(function(){
		$(this).siblings('.thumbover').addClass('thumbover_on');
	});
	$('#authors .opt').removeAttr('href');
	$('#authors .opt').click(function(e){
		e.stopPropagation();
		$('#authors .opt').not($(this)).removeClass('opt_on');
		$('#authors .opt').not($(this)).parent().removeClass('thumbover_on');
		$('#authors .opt').not($(this)).parents('.profile').removeClass('profile_on');
		$('#authors .opt').not($(this)).siblings('.options').removeClass('options_on');
		$(this).parents('.profile').addClass('profile_on');
		optstat = $(this).siblings('.options').hasClass('options_on');
		if (optstat){
			$(this).removeClass('opt_on');
			$(this).siblings('.options').removeClass('options_on');
		}
		else {
			$(this).addClass('opt_on');
			$(this).siblings('.options').addClass('options_on');
		}
	});
	$(document).click(function(){
		$('.profile').removeClass('profile_on');
		$('.thumbover').removeClass('thumbover_on');
		$('.thumbover').children('.opt').removeClass('opt_on');
		$('.thumbover').children('.options').removeClass('options_on');
		$('.results h4').addClass('on');
		$('.results .detail').css('display', 'none');
	});
	$('.thumbover').mouseleave(function(){
		if (!$(this).children('.opt').hasClass('opt_on')){
			$(this).removeClass('thumbover_on');
		}
	});
	$('.results h4').click(function(e){
		e.stopPropagation();
		status = $(this).hasClass('on');
		if (status) {
			$(this).removeClass('on');
			$(this).siblings('.detail').css('display', 'block');
		}
		else {
			$(this).addClass('on');
			$(this).siblings('.detail').css('display', 'none');
		}
	});
	
	$('.lblink').click(function(){
		$('#overlay').fadeIn(800);
		box = '#' + $(this).attr('rel');
		marginTop = -$(box).height()/2;
		$(box).fadeIn(800).css('marginTop', marginTop);
	});
	$('#overlay').click(function(){
		$(this).fadeOut(800);
		$('.lightbox').fadeOut(800);
	});
	$('.lightbox .close').click(function(){
		$('.lightbox, #overlay').fadeOut(800);
	});
	$('#overlay').click(function(){
		$(this).fadeOut(800);
		$('.clightbox').fadeOut(800);
	});
	$('.clightbox .close').click(function(){
		$('.clightbox, #overlay').fadeOut(800);
	});
});

