//Used for the carousel
video = 0;

$(document).ready(function() {

	initFoldableSubForms();
	initForms();
	initAjax();
	initAjaxLink();
	initConfirmSub();
	initShoutCollapse();
	initAjaxConfirm();
	initAjaxRecallSorting();
	initThumbSwitch();
	initUserLinks();
	initDeleteMessages();
	initUnreadMessages();
	initReadMessages();
	initRelated();
	//initTabMenus();
	checkFieldsYoutube();
	initCheckAllCheckboxes();
	initSubmitByRel();

	unFoldLoginForm();

	initShoutBoxes();
});

function initThumbSwitch()
{
	$('#partnerGoToThumb').click(function(){
		$('.partnerDetail').hide();
		$('.partnerThumb').show();
		$('#partnerGoToThumb').hide();
		$('#partnerGoToDetail').show();
		$('#partnerOpen').show();
		$('#partnerClose').hide();
		$('ul.partnerThumb').children('.seeMoreThumb').each(function(){
			$(this).hide();				
		});
		
		$('.sortingLink').each(function(){
			var href = $(this).attr("href");
			href = href.replace('/type/detail', '');
			$(this).attr("href", href + '/type/thumb');
		});
		$('.blogRecentItem a').each(function(){
			var href = $(this).attr("href");
			href = href.replace('/type/detail', '');
			$(this).attr("href", href + '/type/thumb');
		});
		
		return false;
	});
	
	$('#partnerGoToDetail').click(function(){
		$('.partnerDetail').show();
		$('.partnerThumb').hide();
		$('#partnerGoToThumb').show();
		$('#partnerGoToDetail').hide();
		$('#partnerOpen').show();
		$('#partnerClose').hide();
		$('ul.partnerDetail').children('.seeMoreDetail').each(function(){
			$(this).hide();				
		});
		
		$('.sortingLink').each(function(){
			var href = $(this).attr("href");
			href = href.replace('/type/thumb', '');
			$(this).attr("href", href + '/type/detail');
		});
		$('.blogRecentItem a').each(function(){
			var href = $(this).attr("href");
			href = href.replace('/type/thumb', '');
			$(this).attr("href", href + '/type/detail');
		});
		
		return false;
	});
	
	$('#relatedGoToThumb').click(function(){
		$('.relatedDetail').hide();
		$('.relatedThumb').show();
		$('#relatedGoToThumb').hide();
		$('#relatedGoToDetail').show();
		$('#relatedOpen').show();
		$('#relatedClose').hide();
		$('ul.relatedThumb').children('.seeMoreThumb').each(function(){
			$(this).hide();				
		});

		/*$('.sortingLink').each(function(){
			var href = $(this).attr("href");
			href = href.replace('/type/detail', '');
			$(this).attr("href", href + '/type/thumb');
		});*/
		
		return false;
	});
	
	$('#relatedGoToDetail').click(function(){
		$('.relatedDetail').show();
		$('.relatedThumb').hide();
		$('#relatedGoToThumb').show();
		$('#relatedGoToDetail').hide();
		$('#relatedOpen').show();
		$('#relatedClose').hide();
		$('ul.relatedDetail').children('.seeMoreDetail').each(function(){
			$(this).hide();				
		});

		/*$('.sortingLink').each(function(){
			var href = $(this).attr("href");
			href = href.replace('/type/thumb', '');
			$(this).attr("href", href + '/type/detail');
		});*/
		
		return false;
	});
}

function initRelated()
{
	$('#relatedLinkClick').click(function() 
	{
		$('#partnerTabContent').hide();
		$('#relatedTabContent').show();
		$('#partnerLink').removeClass('active');			
		$('#relatedLink').addClass('active');
	});

}

function initCheckAllCheckboxes()
{
	$(".checkAll").each(function() {
		$(this).click(function() {
			toggleCheckboxesByName($(this).attr("title"), $(this).attr("checked"));
		});
	});
}


function initDeleteMessages()
{
	$('.deleteLink').click(function()
	{
		var form = $(this).attr('rel');
		var sendForm = new Object();
		sendForm.type = "POST";
		sendForm.data = $("#" + form).serialize();
		
		sendForm.url = $(this).attr('href');
		sendForm.success = function (response) {
			$("#" + form).html(response);

			initForms();
		    initAjax();
		    initShoutBoxes();
		    initCheckAllCheckboxes();
			initDeleteMessages();
			initUnreadMessages();
			initReadMessages();
		};
		$.ajax(sendForm);
		return false;
	});
}

function initUnreadMessages()
{
	$('.unreadLink').click(function()
	{
		var form = $(this).attr('rel');
		var sendForm = new Object();
		sendForm.type = "POST";
		sendForm.data = $("#" + form).serialize();
		
		sendForm.url = $(this).attr('href');
		sendForm.success = function (response) {
			$("#" + form).html(response);
			
			initForms();
			initAjax();
			initShoutBoxes();
			initCheckAllCheckboxes();
			initDeleteMessages();
			initUnreadMessages();
			initReadMessages();
		};
		$.ajax(sendForm);
		return false;
	});
}

function initReadMessages()
{
	$('.readLink').click(function()
	{
		var form = $(this).attr('rel');
		var sendForm = new Object();
		sendForm.type = "POST";
		sendForm.data = $("#" + form).serialize();
		
		sendForm.url = $(this).attr('href');
		sendForm.success = function (response) {
			$("#" + form).html(response);
			
			initForms();
			initAjax();
			initShoutBoxes();
			initCheckAllCheckboxes();
			initDeleteMessages();
			initUnreadMessages();
			initReadMessages();
		};
		$.ajax(sendForm);
		return false;
	});
}

function initSubmitByRel()
{
	$(".submit").click(function() {
		if (this.rel != null)
		{
			var form = $("#" + this.rel);
			if (this.rel != null)
			{
				form.submit();
			}
		}

		return false;
	});
}

/*function initTabMenus()
{
	$('#tabMenu li a').each(function(){
		$(this).click(function(){
			tabMenu(this);
		});
	});
}*/

function initUserLinks()
{
	$("a.userlink").each(function() {
		$(this).click(function() {
			externalCall(this);

			return false;
		});
	});
}

function initFoldableSubForms()
{
	$("fieldset legend").each(function() {
		$(this).click(function() {
			toggleSubForms(this);
		});
	});
}

function initForms()
{
	var forms = Array('postForm', 'video-Form', 'channel-Form', 'partner-Form', 'addChannelVideo-form', 'channelvideo-Form', 'takeOfflineForm', 'takedownForm', 'notOkForm');

	for (i = 0; i < forms.length; i++)
	{
		form = $("#" + forms[i]);
		if (null != form.attr("id"))
		{
			form.submit(function() {

				if (this.id == 'notOkForm')
				{
					var container = $("#" + form.title + "-container");
					container.hide("slow");
				}

				if(this.id == 'addChannelVideo-form')
				{
					var ed = tinyMCE.get('addChannelVideoText');
					$('#addChannelVideoText').val(ed.getContent());
					//$('#add_channel').toggle();
				}

				formSubmit(this);
				
				if (this.id == 'notOkForm')
				{
					container.hide("show");
				}

				return false;
			});
		}
	}
}

function initAjax()
{
	$('a.ajax').unbind('click');
	$('a.ajax').click(function() {
		ajaxCall(this);

		return false; 
	});
}

function initAjaxLink()
{
	$('a.ajaxLink').click(function() {	
		var reformedRel = $(this).attr("rel").replace(' nofollow', '');
		ajaxHyperlink(reformedRel, $(this).attr("href"));

		return false; 
	});
}

function initConfirmSub()
{
	$('#unsubscribeLink').click(function()
	{
		
		var text = $('#messageUnsubscribe').html();

		var answer = confirm(text);
		if (answer)
		{
			ajaxHyperlink($(this).attr("rel"), $(this).attr("href"));
	    }
		return false;
	});
}


function initShoutCollapse()
{
	/** DROP DOWN/UP BUTTONS FOR SHOUTBOX **/
	$('.showMoreShouts').click(function()
	{
		ajaxHyperlink($(this).attr("rel"), $(this).attr("href"));

		var id = $(this).attr('rel').replace('shoutMessagesBox-', '');
		
		$('#showMoreShouts-' + id).hide();
		$('#showLessShouts-' + id).show();
		
		return false;
	});
	
	$('.showLessShouts').click(function()
	{
		var id = $(this).attr('rel').replace('shoutMessagesBox-', '');

		$('#showMoreShouts-' + id).show();
		$('#showLessShouts-' + id).hide();
		$('#shoutMessagesBox-' + id).slideUp();
		
		return false;
	});
}

function initAjaxConfirm()
{
	$('a.ajaxConfirm').click(function() {
		if(confirm(this.title) === true)
		{
			ajaxCall(this);
		}
		return false; 
	});
	
	$('button.ajaxConfirm').click(function() {
		if(confirm(this.title) === true)
		{
			formSubmit(this.attr('rel'));
		}
		return false; 
	});
}

function initAjaxRecallSorting()
{
	$('.sortContainer').children('li').each(function()
	{
		var className = $(this).attr('class');
		if(className != 'ajax')
		{
			$(this).children('a').each(function()
			{
				//var latest = $(this).attr('href').search('latest');
				var bestViewed = $(this).attr('href').search('bestViewed');
				var mostDiscussed = $(this).attr('href').search('mostDiscussed');
				
				if(bestViewed > 0)
				{
					$('.blog_item .views').each(function()
					{
						$(this).show();
					});
				}
				if(mostDiscussed > 0)
				{
					$('.blog_item .comments').each(function()
					{
						$(this).show();
					});
				}
				
				
			});
			
		}
		
	});
}

function unFoldLoginForm()
{
	var loginError = $("#loginError");
	if (loginError.attr("id") != undefined)
	{
		$("#menu-menuLogin").click();
	}
}

function initShoutBoxes()
{
	$("form.shoutbox").each(function ()
	{
		$(this).unbind("submit");
		$(this).submit(function()
		{
			ajaxFormSubmit(this, $(this).attr("title"));

			return false;
		});
	});

	$("a.comment").each(function ()
	{
		$(this).unbind("click");
		$(this).click(function ()
		{
			var commentIdParts = ("" + this.href).split("#");
			var targetIdParts = ("" + this.title).split("-");

			if (commentIdParts[1] != null && targetIdParts[1] != null)
			{
				var editCommentOn = $("#editCommentOn-" + targetIdParts[1]);
				var commentOn = $("#commentOn-" + targetIdParts[1]);
				var scrollDiv = $("#shouts-" + targetIdParts[1]);
				
				/*
				// Steven's todo
				$("#commentOn-" + targetIdParts[1]).hide();
				var newHtml = $("#partnerName-" + commentIdParts[1]).html() + ' ' + $("#postTime-" + commentIdParts[1]).html();
				$("#" + this.title).html(newHtml);
				$("#shouts-" + targetIdParts[1] + " input[name=commentOnShoutId]").val(commentIdParts[1]);
				$("#commentOn-" + targetIdParts[1]).show();
				*/
				
				editCommentOn.hide();
				$("#shouts-" + targetIdParts[1] + " input[name=editShoutId]").val("");

				commentOn.hide();
				$("#" + this.title).html($("#message-" + commentIdParts[1]).html());
				$("#shouts-" + targetIdParts[1] + " input[name=commentOnShoutId]").val(commentIdParts[1]);
				commentOn.show();
				$.scrollTo(scrollDiv, 500);
			}

			return false;
		});
	});

	$("a.undoComment").each(function ()
	{
		$(this).unbind("click");
		$(this).click(function ()
		{
			var shoutIdParts = ("" + this.href).split("#");
			var targetIdParts = ("" + shoutIdParts).split("-");

			if (targetIdParts[1] != null)
			{
				$("#quoteComment-" + targetIdParts[1]).html("");

				$("#shouts-" + targetIdParts[1] + " input[name=commentOnShoutId]").val("");
				$("#commentOn-" + targetIdParts[1]).hide("fast");
			}

			return false;
		});
	});
	
	$("a.editComment").each(function ()
	{
		$(this).unbind("click");
		$(this).click(function ()
		{
			var commentIdParts = ("" + this.href).split("#");
			var targetIdParts = ("" + this.title).split("-");

			if (commentIdParts[1] != null && targetIdParts[1] != null)
			{
				var comment = $("#message-" + commentIdParts[1]).html();
				var editCommentOn = $("#editCommentOn-" + targetIdParts[1]);
				var commentOn = $("#commentOn-" + targetIdParts[1]);

				commentOn.hide("fast")
				$("#shouts-" + targetIdParts[1] + " input[name=commentOnShoutId]").val("");
				
				editCommentOn.hide("fast");
				$("#shouts-" + targetIdParts[1] + " textarea[name=message]").val(comment);
				$("#" + this.title).html(comment);
				$("#shouts-" + targetIdParts[1] + " input[name=editShoutId]").val(commentIdParts[1]);
				
				editCommentOn.show("fast");
				$.scrollTo(editCommentOn, 500);
			}

			return false;
		});
	});

	$("a.undoEditComment").each(function ()
	{
		$(this).unbind("click");
		$(this).click(function ()
		{
			var shoutIdParts = ("" + this.href).split("#");
			var targetIdParts = ("" + shoutIdParts).split("-");
			if (targetIdParts[1] != null)
			{
				$("#editComment-" + targetIdParts[1]).html("");
				$("#shouts-" + targetIdParts[1] + " input[name=editShoutId]").val("");
				$("#editCommentOn-" + targetIdParts[1]).hide("fast");
				$("#shouts-" + targetIdParts[1] + " textarea#message").val('');
			}

			return false;
		});
	});

	$("a.removeComment").each(function ()
	{
		$(this).unbind("click");
		$(this).click(function ()
		{
			var commentIdParts = ("" + this.href).split("#");
			var targetIdParts = ("" + this.title).split("-");

			if (commentIdParts[1] != null && targetIdParts[1] != null)
			{
				if (confirm("Delete?"))
				{
					$("#shouts-" + targetIdParts[1] + " input[name=removeShoutId]").val(commentIdParts[1]);
					$("form#shouts-" + targetIdParts[1]).submit();
				}
			}

			return false;
		});
	});
}

function initAddChannelForm()
{
	$('#AdminOfChannels').change(function () {
		if ($('#AdminOfChannels :selected').text() == 'Zideo Blog')
		{
			$('#replaceChannelVideo').removeAttr('disabled');
		}
		else
		{
			$('#replaceChannelVideo').attr('disabled', 'disabled');
			$('#postTitle').removeAttr('disabled');
			$('#addChannelVideoText').removeAttr('disabled');
		}
	});
	
	$('#replaceChannelVideo').change(function () {

		if ($('#AdminOfChannels :selected').text() == 'Zideo Blog')
		{
			var playzideo = $(this).val().trim();
			if (playzideo != '' && playzideo != 0)
			{
				$('#postTitle').attr('disabled', 'disabled');
				$('#addChannelVideoText').attr('disabled', 'disabled');
			}
			else
			{
				$('#postTitle').removeAttr('disabled');
				$('#addChannelVideoText').removeAttr('disabled');
			}
		}
	});
	/** SELECT BOX SKINNING **/
	jQuery(document).ready(function() {
	    jQuery("#postAsUser").select_skin();
	    jQuery("#AdminOfChannels").select_skin();
	    jQuery("#replaceChannelVideo").select_skin();
	    jQuery("#categorie").select_skin();
	    $("a[rel^='prettyPhoto']").prettyPhoto();
	    $("#datetimepicker").datetimepicker();
	});
}

function checkFieldsYoutube()
{
	$('#resultBox').hide();
	if($('#ytData_title').length > 0)
	{
		$('#title').val($('#ytData_title').html());
	}
	
	if($('#ytData_description').length > 0)
	{
		$('#description').val($('#ytData_description').html());
	}
	
	if($('#ytData_duration').length > 0)
	{
		$('#duration').val($('#ytData_duration').html());
	}
	
	if($('#ytData_tags').length > 0)
	{
		var tags = '';
		
		$('.ytData_Array').each(function()
		{
			tags = tags + $(this).html() + ' ';
		});

		$('#tags').val(tags);
	}
	
}
