var ajax_loading_bar = "<img src='"+ base_url +"style/images/ajax_loading_bar.gif' alt='Loading..' title='يرجى الانتظار' />";
var tags = "hide";
function showKatebDetails(id)
{
	var avtive = $("#kottab .details ul li.active");
	if (avtive.length===0)
		$("#kottab .details ul li[rel="+id+"]").addClass("active").fadeIn();
	else
	$("#kottab .details ul li.active").removeClass("active").fadeOut(function(){
		$("#kottab .details ul li[rel="+id+"]").addClass("active").fadeIn();
	});
}
$(document).ready(function(){
	

	var wrap = $(".tags .wrap");
	var tags_height = wrap.height();
	wrap.css("height",'48px');
	if(tags_height < 48)
		$('.plus').hide();
	
	$('.plus').click(function(){
		if(tags == 'hide')
			wrap.animate({height: tags_height},200,function(){tags='show'});
		else
			wrap.animate({height: 48},200,function(){tags='hide'});
	});
	
	$('#kottab .large ul li').click(function(){		
		$("#kottab .details").show();
		var pos = $(this).position();
		var small_rel = $(this).attr("rel");
		var small = $("#kottab .small ul li[rel="+ small_rel +"]");
		$(this).css({position:'absolute',top:pos.top,left:pos.left});
		$(this).animate({top:67,left:223},{duration:500,queue:false,complete: function(){
			showKatebDetails(small_rel);
		}});
		$(this).addClass("active");
		small.hide();
		var others = $("#kottab .large ul li").not($(this));
		others.each(function(i){
			$(this).fadeOut();
		});
		$("#kottab .small").fadeIn(function(){
			$('#kottab .large ul li').css({position:'absolute',top:67,left:223});
		});
		
	});
	
	var kottab_lock = false;
	$('#kottab .small ul li').click(function(){
		if (kottab_lock) return false;
		
		$('#kottab .small ul li').show();
		$(this).hide();
		current = $("#kottab .large ul li.active");
		var large_rel = $(this).attr("rel");
		var large = $("#kottab .large ul li[rel="+ large_rel +"]");
		kottab_lock = true;
		current.fadeOut(function(){
			showKatebDetails(large_rel);
			large.fadeIn(function(){kottab_lock = false;});
			
		});
		current.removeClass("active");
		large.addClass("active");
	});
	
	
	
	
	
	
	
		
	$('.resultsTrigger').click(function(){
		var Poll_id = $(this).attr('rel');
		view_poll_results(Poll_id);		
		return false;
	});
	$('#comment_comment').keyup(function(){
		len = $(this).val().length;
		left = 200 - len;
		$('#descriptionCharLeft').html(left);
	});	
	showTabbedSection();
	$('.voteTrigger').click(function(){
		//alert("aa");
		var Poll_id = $(this).attr('rel');
		vote(Poll_id);		
	});	
	$('#tabbed .tabbednavbar ul li').click(function(){
		$('#tabbed .tabbednavbar ul li.active').removeClass('active');
		$(this).addClass('active');
		showTabbedSection();
	});	
	function showTabbedSection()
	{
		var section = $('#tabbed .tabbednavbar ul li.active').attr('rel');
		$('#tabbed .articles .section').hide();
		$('#tabbed .articles #' + section + 'Section').show();
	}
	//set speed
	var speed = 4;// in seconds
	speed = speed * 1000;
	//hiding graybox ( hide from css ya 7mar)
	$('#article-slider .grayBox').hide();
	//activate first paging li
	//activate($('#article-slider .paging ul li:first'));
	activate($('#article-slider .paging ul li:first'));//.addClass('active')
	$('#article-slider .grayBox').fadeIn('slow');
	//auto activate next li
	var interval = setInterval ( "rotata()", speed );
	$('#article-slider').mouseout(function(){$('#article-slider .paging .picker').stop(true, true)});
	$('#article-slider .paging ul li a').mouseover(function(){
		activate($(this).parent()); //grow parent li
		clearInterval(interval);		
	});	
	$('#article-slider .paging ul li a').mouseout(function(){
		//rotata();
		interval = setInterval ( "rotata()", speed );		
	});	
	var windowWidth = $('.article-gallery-box .window').width();
	var num_pages = $('.article-gallery-box .window').attr('rel');
	var reel_width = num_pages * windowWidth;
	$('.article-gallery-box .window .content').width(reel_width);
	$('.article-gallery-box .window .content').show();
	//$(".article-gallery-box .buttons ul li:first").addClass('active');
	var num_ran = $(".article-gallery-box .buttons ul li").length;
	num_ran;
	var randomnumber=Math.floor(Math.random()*num_ran);
	activateIdea($(".article-gallery-box .buttons ul li:eq("+randomnumber+")"));
	$(".article-gallery-box .buttons ul li").click(function(){
		activateIdea($(this));
	});
});


function activateIdea(target)
{
		
	deavtivateAllIdeas(); // remove active from other
	target.addClass('active');
	var idea_num = target.attr('rel')-1;
	var windowWidth = $('.article-gallery-box .window').width();
	var newLeft = (idea_num * windowWidth);
	$('.article-gallery-box .window .content').animate({left: -newLeft},500);
}
function deavtivateAllIdeas()
{
	$('.ideas-box .buttons li').each(function(i){
		$(this).removeClass('active');
	});
}
function rotata()
{
	//select next li
	var next = $('#article-slider .paging ul li.active').next();
	// if last, next is first
	if (next.length===0)
		next = $('#article-slider .paging ul li:first');
	activate(next);
}
function activate(target)
{
	var page = target.children('a').attr('rel') - 1;//index start at zero [half hour to figure this out :( ]
	var pickerTop = page * 81; //81 is pageing li hieght, plz make me get hight automatically!
	$('#article-slider .paging .picker').animate({top: pickerTop},{ duration: 200, queue: true,easing:'linear', complete: function(){
		deavtivateAll(); // remove active from other
		target.addClass('active');
		$('.grayBoxContainer').html(target.children('.intro').html());
	}});
	//fix = page*2;//more two pixels for each image
	//deside where to scroll?
	//top: page number * window height
	var windowHeight = $('#article-slider .window').height();
	var newTop = (page * windowHeight);// + fix;
	//alert(windowHeight);
	$('#article-slider .window .image-items').animate({top: -newTop},{ duration: 200, queue: false },function(){
		$('#article-slider .grayBox').fadeIn('slow');
	});	
}
function deavtivateAll()
{
	$('#article-slider .paging ul li').each(function(i){
		$(this).removeClass('active');
	});
	$('#article-slider .grayBox').fadeOut('slow');
}
function newsletterRegister()
{
	var email = $('#newsletterEmail').val();
	$('#newsletter .text').addClass('waiting');
	$.post(base_url + 'index.php/newsletter/register',{'email': email},function(data){
		$('#newsletter .text').removeClass('waiting');
		if(data=='1')
			$('#newsletter .container').animate({top:-93},300);
		if(data=='3')
			alert("لم تقم بإدخال البريد بشكل صحيح.");
		if(data=='2')
			alert('هذا البريد مسجل مسبقا في القائمة البريدية');
	});
	return false;	
}
function submitRegister()
	{
		var name = $('#popup_name').val();
		var email = $('#popup_email').val();
		var city = $('#city').val();
		var phone = $('#popup_phone').val();
		var time = new Date().getTime(); //time is dummy var, to stop cashing on stupid IE		
		$(".ajaxLoading").html("<img src='"+ base_url +"style/images/ajax-loader2.gif' alt='loading...' />");
		$.post(base_url + 'index.php/home/do_register/' + time,{'name': name,'email': email,'city': city,'phone': phone},function(data){
			if (data == "1")
			{
				//success
				alert("لقد تمت اضافة معلوماتك بنجاج");
				hideLightBox();
			}
			else if (data == "3")
			{
				alert("هذا البريد مسجل من قبل.");
			}
			else
			{
				//error
				alert("لم تقومي بتعبئة الحقول بشكل صحيح!");
			}
			$(".ajaxLoading").html("");
		});
		return false;
	}
function refereshVComments()
{
	$(".ajaxLoading").html("<img src='"+ base_url +"style/images/ajax-loader2.gif' alt='loading...' />");
	var Video_id = $('#comment_Video_id').val();
	$.get(base_url + "index.php/video/referesh_comments/" + Video_id,{},function(data){
		$(".comments").html(data);
		$(".ajaxLoading").html("");
	});
}
function refereshComments()
{
	$(".ajaxLoading").html("<img src='"+ base_url +"style/images/ajax-loader2.gif' alt='loading...' />");
	var Article_id = $('#comment_Article_id').val();
	$.get(base_url + "index.php/article/referesh_comments/" + Article_id,{},function(data){
		$(".comments").html(data);
		$(".ajaxLoading").html("");
	});
}
function submitVComment()
{
	var name = $('#comment_name').val();
	var email = $('#comment_email').val();
	var Video_id = $('#comment_Video_id').val();
	var comment = $('#comment_comment').val();
	var time = new Date().getTime(); //time is dummy var, to stop cashing on stupid IE
	$(".ajaxLoading").html("<img src='"+ base_url +"style/images/ajax-loader2.gif' alt='loading...' />");
	$.post(base_url + 'index.php/video/ajax_comment/' + time,{'name': name,'email': email,'comment': comment,'Video_id': Video_id},function(data){
		if (data == "1")
		{
			//success
			alert("تمت إضافة التعليق بنجاح.");
			refereshVComments();
			//refereshComments();
			$(':input','#comment_form')
				 .not(':button, :submit, :reset, :hidden')
				 .val('');
		}
		else if(data == "0") // added, but not published
		{
			alert("تمت إضافة التعليق بنجاح. سيتم مراجعة التعليق قبل نشره على الموقع.");
			refereshComments();
			$(':input','#comment_form')
				 .not(':button, :submit, :reset, :hidden')
				 .val('');
		}
		else
		{
			//error
			alert("لم تقومي بتعبئة الحقول بشكل صحيح!")
		}
		//alert(data);
		$(".ajaxLoading").html("");
	});
	return false;
}
function submitComment()
{
	var name = $('#comment_name').val();
	var email = $('#comment_email').val();
	var Article_id = $('#comment_Article_id').val();
	var comment = $('#comment_comment').val();
	var time = new Date().getTime(); //time is dummy var, to stop cashing on stupid IE
	$(".ajaxLoading").html("<img src='"+ base_url +"style/images/ajax-loader2.gif' alt='loading...' />");
	$.post(base_url + 'index.php/article/ajax_comment/' + time,{'name': name,'email': email,'comment': comment,'Article_id': Article_id},function(data){
		if (data == "1")
		{
			//success
			alert("تمت إضافة التعليق بنجاح.");
			refereshComments();
			//refereshComments();
			$(':input','#comment_form')
				 .not(':button, :submit, :reset, :hidden')
				 .val('');
		}
		else if(data == "0") // added, but not published
		{
			alert("تمت إضافة التعليق بنجاح. سيتم مراجعة التعليق قبل نشره على الموقع.");
			refereshComments();
			$(':input','#comment_form')
				 .not(':button, :submit, :reset, :hidden')
				 .val('');
		}
		else
		{
			//error
			alert("لم تقومي بتعبئة الحقول بشكل صحيح!");
		}
		$(".ajaxLoading").html("");
	});
	return false;
}
function view_poll_results(Poll_id)
{
	var box = $('.poll-homebox .answers');
	var target = base_url + "index.php/survey/results";
	box.html(ajax_loading_bar);
	$.post(target,{'Poll_id': Poll_id},function(data){
		box.html(data);
	});
}
function vote(Poll_id)
{
	var Answer_id = $("#voteForm input:checked").val();
	if(typeof Answer_id  == "undefined")
	{
		alert("لم تقم بإختيار اجابة");
		return;
	}
	var box = $('.poll-homebox .answers');
	box.html(ajax_loading_bar);
	var target = base_url + "index.php/survey/vote";
	$.post(target,{'Answer_id':Answer_id},function(data){
		view_poll_results(Poll_id);
	});
	return false;
}
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}
