// JavaScript Document
base = document.getElementsByTagName('base')[0].href || '/';
var RecaptchaOptions = {theme : 'clean'};
String.prototype.hsc = function(){
	return this.replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;'); //"
}

function initMashbord(){
	//RSS link in news bin
	$('#rss_btn').attr('target', '_blank');
	//Mashbord title edit event
	$("#edit_mashbord_title").click(function(){
		$(".mashbord_title_wraper").hide();
		$(".mashbord_title_input").attr("value", ""+$("#edit_mashbord_title").html()+"");
		$(".mashbord_title_edit").show();
		$("#cancel_changes_btn").show();
	});
	//Cancel mashbord title edit button event
	$('.mashbord_title_edit #cancel_changes_btn').click(function(){
		cancelMashbordTitle();
	});
	//Save mashbord title event
	$('.mashbord_title_edit #save_changes_btn').click(function(){
		var mashbord_id = $('#mashbord_id').val();
		saveMashbordTitle(mashbord_id);
	});
	//Save mashbord title on ENTER press
	$('#mashbord_title_input').keypress(function(event){
		var mashbord_id = $('#mashbord_id').val();
		chekForEnterPressed(mashbord_id, event, 'saveMashbordTitle');
	});
	//Main bin title edit event
	$('#main_bin_title_wraper #main_bin_title').click(function(){
		var main_bin_id = $('#main_bin_id').val();
		changeBinTitle(main_bin_id);
	});
	//Cancel main bin title edit event
	$('#cancel_mb_changes_btn').click(function(){
		cancelMainBinTitle();
	});
	//Save main bin title event
	$('#save_mb_changes_btn').click(function(){
		var main_bin_id = $('#main_bin_id').val();
		saveMainBinTitle(main_bin_id);
	});
	//Save main bin title on ENTER press
	$('#main_bin_title_input').keypress(function(event){
		var main_bin_id = $('#main_bin_id').val();
		chekForEnterPressed(main_bin_id, event, 'saveMainBinTitle');
	});
	//Vert Bin title edit event
	$('.bin h3 a, .bin_empty h3 a').click(function(){
		var bin_id = $(this).attr('id').replace(/vertical_bin_title_/i, '');
		changeVertBinTitle(bin_id);
	});
	$('.cancel_vbin_title_btn').click(function(){
		var bin_id = $(this).attr('id').replace(/cancel_vbin_title_btn_/i, '');
		cancelVertBinTitle(bin_id);
	});
	//Save bin title on button click
	$('.save_vbin_title_btn').click(function(){
		var bin_id = $(this).attr('id').replace(/save_vbin_title_btn_/i, '');
		saveVertBinTitle(bin_id);
	});
	//Save bin title on ENTER press
	$('.v_bin_input').keypress(function(event){
		var bin_id = $(this).attr('id').replace(/v_bin_title_input_/i, '');
		chekForEnterPressed(bin_id, event, 'saveVertBin');
	});
	
	//Site ico tooltip
	$('.bin_vertical_sites IMG.site_ico, .main_bin_sites IMG.site_ico').hover(function(e){
		$('.bubble').remove();
		$('body').append('<div class="bubble"><div class="top"></div><div class="middle"><div class="content"></div></div><div class="bottom"></div>');
		$('.bubble .content').html('<strong>'+$(this).attr('alt')+'</strong>'+'<p>'+$(this).parent().attr('rel')+'</p>');
		$('.bubble').show();
		$('.bubble').css('top',($(this).offset().top-$('.bubble').height()+0)+'px').css('left',($(this).offset().left+60)+'px');
	
	},function(){
		$('.bubble').remove();
	});
	//Del button tooltip
	$('.bin_vertical_sites IMG.delete_site, .main_bin_sites IMG.delete_site').hover(function(e){
		$('.bubble').remove();
		$('body').append('<div class="bubble"><div class="top_red"></div><div class="middle_red"><div class="content_red"></div></div><div class="bottom_red"></div>');
		$('.bubble .content_red').html('<strong>'+$(this).attr('alt')+'</strong>'+'<p>'+$(this).parent().attr('rel')+'</p>');
		$('.bubble').show();
		$('.bubble').css('top',($(this).offset().top-$('.bubble').height()+9)+'px').css('left',($(this).offset().left+20)+'px');
	
	},function(){
		$('.bubble').remove();
	});
	//Edit button tooltip
	$('.bin_vertical_sites IMG.edit_site, .main_bin_sites IMG.edit_site').hover(function(e){
		$('.bubble').remove();
		$('body').append('<div class="bubble"><div class="top_edit"></div><div class="middle_edit"><div class="content_edit"></div></div><div class="bottom_edit"></div>');
		$('.bubble .content_edit').html('<strong>'+$(this).attr('alt')+'</strong>'+'<p>'+$(this).parent().attr('rel')+'</p>');
		$('.bubble').show();
		$('.bubble').css('top',($(this).offset().top-$('.bubble').height()+9)+'px').css('left',($(this).offset().left+20)+'px');
	
	},function(){
		$('.bubble').remove();
	});
	//Show delete and edit site buttons
	$('.main_bin_list LI, .bin_vertical_sites LI').hover(function(){
		var elID = $(this).attr('id').replace(/site_/i, '');
		$('#del_site_'+elID).show();
		$('#edit_site_'+elID).show();
	},
	function(){
		$('img.delete_site').hide();
		$('img.edit_site').hide();
	});
}

$(document).ready(function(){
	//Init Carousel
	$('.main_bin_list').jcarousel({
		scroll: 5
	});
	$('.vertical_carousel').jcarousel({
		vertical: true
	});
	$.fn.nyroModal.settings.closeButton = '';
	initMashbord();
});

function chekForEnterPressed(value, event, action)
{
	if(event.keyCode == 13){
		if(action == 'saveVertBin'){
			saveVertBinTitle(value);
		}else if(action == 'saveMashbordTitle'){
			saveMashbordTitle(value);
		}else if(action == 'saveMainBinTitle'){
			saveMainBinTitle(value);
		}
	}
}
//Display vert bin edit input
function changeVertBinTitle(binID)
{
	$("#vertical_bin_"+binID).hide();
	$("#add_new_vsite_btn_"+binID).hide();
	$(".bin_full_text_"+binID).hide();
	$("#vertical_bin_edit_"+binID).show();
	$("#save_vbin_title_btn_"+binID).show();
	$("#cancel_vbin_title_btn_"+binID).show();
}
//Hide vert bin edit input
function cancelVertBinTitle(binID)
{
	$("#vertical_bin_edit_"+binID).hide();
	$("#save_vbin_title_btn_"+binID).hide();
	$("#cancel_vbin_title_btn_"+binID).hide();
	$("#add_new_vsite_btn_"+binID).show();
	$(".bin_full_text_"+binID).show();
	$("#vertical_bin_"+binID).show();
}
//Save vert bin title
function saveVertBinTitle(binID)
{
	var newTitle = $("#v_bin_title_input_"+binID).attr("value");
	if(newTitle != ''){
		$.post(base+"ajax.php", {f: "saveBinTitle", binTitle: newTitle, binID: binID});
		
		$("#vertical_bin_edit_"+binID).hide();
		$("#save_vbin_title_btn_"+binID).hide();
		$("#cancel_vbin_title_btn_"+binID).hide();
		$("#vertical_bin_title_"+binID).html($("#v_bin_title_input_"+binID).attr('value').hsc());
		$("#add_new_vsite_btn_"+binID).show();
		$(".bin_full_text_"+binID).show();
		$("#vertical_bin_"+binID).show();
	}
}
//Display input field for changing binTitle
function changeBinTitle(binID)
{
	$("#main_bin_title_wraper").hide();
	$(".main_bin_title_input").attr("value", $("#main_bin_title").html());
	$("#main_bin_title_edit").show();
}
//Save new main bin title
function saveMainBinTitle(binID)
{
	var newTitle = $(".main_bin_title_input").attr("value");
	if(newTitle != ''){
		$("#save_mb_changes_btn, #cancel_mb_changes_btn").hide();
		$("#mainBinAjaxLoad").show();
		
		$.post(base+"ajax.php", {f: "saveBinTitle", binTitle: newTitle, binID: binID},
		function(data){
			$("#main_bin_title").html($(".main_bin_title_input").attr("value").hsc());
			$("#main_bin_title_wraper").show();
			$("#main_bin_title_edit").hide();
			$("#save_mb_changes_btn, #cancel_mb_changes_btn").show();
			$("#mainBinAjaxLoad").hide();
		});
	}
}
//Cancel main bin title changes
function cancelMainBinTitle()
{
	$("#main_bin_title_wraper").show();
	$("#main_bin_title_edit").hide();
	$("#save_mb_changes_btn").show();
	$("#cancel_mb_changes_btn").show();
	$("#mainBinAjaxLoad").hide();	
}
//Save changes in mashbord title
function saveMashbordTitle(mashbordID)
{
	var newTitle = $(".mashbord_title_input").attr("value");
	if(newTitle != ''){
		$("#save_changes_btn, #cancel_changes_btn").hide();
		$("#titleAjaxLoad").show();
			
		$.post(base+"ajax.php", {f: "saveMashbordTitle", mashbordTitle: newTitle, mashbordID: mashbordID},
		function(data){
			$(".mashbord_title_edit").hide();
			$("#edit_mashbord_title, #mashb_menu_item_"+data).html($(".mashbord_title_input").attr("value").hsc());
			$(".mashbord_title_wraper, #save_changes_btn").show();
			$("#titleAjaxLoad").hide();
		});
	}
}
//Cancel changes in mashbord title
function cancelMashbordTitle()
{
	$(".mashbord_title_edit").hide();
	$(".mashbord_title_wraper").show();
	$("#save_changes_btn").show();
	$("#titleAjaxLoad").hide();
}

//Add new site in bookmarks or save changes
function saveSite(action)
{
	var add_errors = 0;
	var cBinID = $("#binID").attr("value");
	var cSiteURL = $("#newSiteURL").attr("value");
	var cSiteTitle = $("#newSiteTitle").attr("value");
	var cSiteDescription = $("#newSiteDescription").attr("value");
	var cBinType = $("#bin_type_"+cBinID).attr('value');
	//If edit existing bookmark
	if(action == 'edit'){
		var cSiteID = $("#siteID").attr("value");
		var cAction = 'edit';
	}else{
		var cAction = 'add';
	}
	
	if(cSiteURL == ''){
		$("#newSiteURL").addClass("error");
		errors = 1;
	}else{
		$("#newSiteURL").removeClass("error");
	}
	
	var isValidURL = $.ajax({url: base+'ajax.php', type: "POST", data: 'f=validateURL&url='+cSiteURL, async: false}).responseText;
	if(isValidURL == 0){
		add_errors = 1;
		$("#newSiteURL").addClass("error");
		$('#not_valid_url_msg').show();
	}else{
		$("#newSiteURL").removeClass("error");
		$('#not_valid_url_msg').hide();
	}
	
	if(cSiteTitle == ''){
		$("#newSiteTitle").addClass("error");
		add_errors = 1;
	}else{
		$("#newSiteTitle").removeClass("error");
	}
	
	if (add_errors == 0){

		$("#add_site_form, #head_bin_title").hide();
		$("#ajax_loading").show();
				
		$.post(base+"ajax.php",
			{f: "saveSiteToBin", binID: cBinID, siteURL: isValidURL, siteTitle: cSiteTitle, siteDescription: cSiteDescription, binType: cBinType, siteID: cSiteID, action: cAction},
			function(data){
				if (cBinType == 'vertical bin'){
					$('#bin_wrap_'+cBinID).html("");
					$('#bin_wrap_'+cBinID).html(data);
					$('.vertical_carousel').jcarousel({
						vertical: true
					});
				} else {
					$('.site_list_'+cBinID).html("");
					$('.site_list_'+cBinID).html(data);
					if ($('#mainBinSitesNumber').val() >= 10){
						$('#add_new_site_btn').hide();
						$('.main_bin_full').show();
						$.nyroModalRemove(); $('a.nyroModal').nyroModal();
					}else{
						$('.main_bin_full').hide();
						$('#add_new_site_btn').show();
					}
					$('.main_bin_list').jcarousel({
						scroll: 5
					});
				}
				$("#ajax_loading").hide();
				if(action == 'edit'){
					$.nyroModalRemove();
					$('a.nyroModal').nyroModal();	
				}else{
					$('#added_site_title').html(cSiteTitle);
					$("#site_added").show();
					$('a.nyroModal').nyroModal();
				}
				initMashbord();
			}
		);
	}
}

//Show add bookmark form
function addNextBookmark()
{
	$("#site_added").hide();
	$("#newSiteURL").attr("value", "http://www.url-of-the-site.com");
	$("#newSiteTitle").attr("value", "Title of page");
	$("#newSiteDescription").attr("value", "This is the description of the mashbord for display in the browse or search listings - 70 words.");
	$("#head_bin_title").show();
	$("#add_site_form").show();
}

//Delete bookmark
function deleteBookmark(delSiteID)
{
	var cBinID = $("#del_site_bin").attr("value");
	var cBinType = $("#bin_type_"+cBinID).attr('value');
	
	$("#ajax_loading").ajaxStart(function(){
		$("#del_site_form").hide();
		$(this).show();
	});
	
	$.post(base+"ajax.php",
		{f: "deleteBookmark", siteID: delSiteID, binID: cBinID, binType: cBinType},
		function(data){
			if (cBinType == 'vertical bin'){
				$('#bin_wrap_'+cBinID).html("");
				$('#bin_wrap_'+cBinID).html(data);
				$('.vertical_carousel').jcarousel({
					vertical: true
				});
			} else {
				$('.site_list_'+cBinID).html("");
				$('.site_list_'+cBinID).html(data);
				if ($('#mainBinSitesNumber').val() < 10){
					$('.main_bin_full').hide();
					$('#add_new_site_btn').show();
				}else{
					$('#add_new_site_btn').hide();
					$('.main_bin_full').show();
				}
				$('.main_bin_list').jcarousel({
					scroll: 5
				});
			}
		}
	);
	
	$("#ajax_loading").ajaxStop(function(){
		$.nyroModalRemove();
		$('a.nyroModal').nyroModal();
		initMashbord();
	});
}
//Registration form
$(document).ready(function(){
	//User login form submit
	$('.header #go').click(function(){
		$('.header #loginForm').submit();
	});
	//On form submit event
	$('#reg_form').submit(function(){
		var errors = 0;
		if($('#signup_email').size()>0){
			if($('#signup_email').val() == ''){
				var errors = 1;
				$('#useremail_err_msg').html('Required');
				$('#email_field_error').show();
			}
			if($('#signup_name').val() == ''){
				var errors = 1;
				$('#username_err_msg').html('Required');
				$('#username_field_error').show();
			}
			if($('#signup_psw').val() == ''){
				var errors = 1;
				$('#password_field_error').show();
			}else{
				$('#password_field_error').hide();
			}

			if (errors == 1 || login_error != '' || email_error != ''){
				return false;
			}			
		}
	});
	//Check user login
	login_error = '';
	$('#signup_name').change(function(){
		var user_login = $(this).val();
		$.post(base+'ajax.php', {f: "checkIfUserLoginExist", userLogin: user_login},
			function(data){
				if(data != ''){
					login_error = '-error-';
					$('#username_err_msg').html(data);
					$('#username_field_error').show();
				}else{
					login_error = '';
					$('#username_field_error').hide();
				}
			}
		);
	});
	//Check user email
	email_error = '';
	$('#signup_email').change(function(){
		var user_email = $(this).val();
		$.post(base+'ajax.php', {f: "checkIfUserEmailValid", userEmail: user_email},
			function(data){
				if(data != ''){
					email_error = '-error-';
					$('#useremail_err_msg').html(data);
					$('#email_field_error').show();
				}else{
					email_error = '';
					$('#email_field_error').hide();
				}
			}
		);
	});
	
});

function initAddForm()
{
	urlFieldNeedClear = true;
	titleFieldNeedClear = true;
	descriptionFieldNeedClear = true;
	$('#newSiteURL').focus(function(){
		if(urlFieldNeedClear){
			$('#newSiteURL').attr('value', 'http://');
			urlFieldNeedClear = false;
		}
	});
	$('#newSiteTitle').focus(function(){
		if(titleFieldNeedClear){
			$('#newSiteTitle').attr('value', '');
			titleFieldNeedClear = false;
		}
	});
	$('#newSiteDescription').focus(function(){
		if(descriptionFieldNeedClear){
			$('#newSiteDescription').attr('value', '');
			descriptionFieldNeedClear = false;
		}
	});
	//On change URL
	$('#newSiteURL').change(function(){
		//Get site info on url changed
		var siteUrl = $(this).val();
	
		$('#newSiteTitle').val('');
		$('#newSiteDescription').val('');
		$('#newSiteTitle').addClass('input_loading_32');
		$('#newSiteDescription').addClass('input_loading_64');
		
		$.post(base+'ajax.php', {f: 'validateURL', url: siteUrl},
		function(data){
			if(data != 0){
			$("#newSiteURL").removeClass("error");
			$('#not_valid_url_msg').hide();
			
			$.getJSON(base+'ajax.php', {f: 'getAddSiteInfo', url: data},
				function(data){
					$('#newSiteTitle').removeClass('input_loading_32');
					$('#newSiteDescription').removeClass('input_loading_64');
					$('#newSiteTitle').val(data.newSiteTitle);
					$('#newSiteDescription').val(data.newSiteDescription);
					//If default title text
					if(data.deleteTitleOnFocus == 1){
						titleFieldNeedClear = true;
					}else{
						titleFieldNeedClear = false;
					}
					//If default descr text
					if(data.deleteDescrOnFocus == 1){
						descriptionFieldNeedClear = true;
					}else{
						descriptionFieldNeedClear = false;
					}
				});
			}else{
				$("#newSiteURL").addClass("error");
				$('#not_valid_url_msg').show();
				$('#newSiteTitle').removeClass('input_loading_32');
				$('#newSiteDescription').removeClass('input_loading_64');
			}
		});
	});
	$('#add_another_btn').click(function(){
		urlFieldNeedClear = true;
		titleFieldNeedClear = true;
		descriptionFieldNeedClear = true;
		addNextBookmark();
	});
}
