/*$.preLoadImages(
	[	
	SITE_URL+"/assets/images/loading.gif",
	SITE_URL+"/assets/images/bg_all_repeat.jpg"
	],
	
	
	function() {
  $('<img />').attr({
  	src:SITE_URL+"/assets/images/loading1.gif",
  	id:'loading'
  }).prependTo('#loader');
	$('#loader').show();

 $.preLoadCSSImages(function() {
 		$('#wrapper').show();
		$('#loader').hide('slow');
  })
});*/
var lastElem;
$(function() {
	
	/*$(".quote-box").live("mouseenter", function(){
		$(this).find(".hiddenDetails").show("slow");
	}).live("mouseout", function(){
		$(this).find(".hiddenDetails").hide("slow");
	})*/
	
	$(".quote-box").live("mouseenter", function(){
		$(this).find(".hide-close").show();
	}).live("mouseleave", function(){
		if ($(this).find(".hide-close span").hasClass("close"))
			if (!$.defaultOpen) $(this).find(".hide-close").hide();
	})
	
	$("#settings_menu").click(function(){
		$("#sticky_menu").hide();
		$(this).animate({height: '115px', width: '180px'}, 500, function() {
			$("#setting_menu_seetings").show();
		});
	})
	
	$("#save-settings").click(function(e){
		e.preventDefault();
		$("#setting_menu_seetings").hide();
		
		$.defaultExpand = $("#details_autoopen").is(":checked");
		$.defaultExpandMaxioms = $("#details_autoopenmaxioms").is(":checked");
		$.defaultOpen = $("#details_show_open").is(":checked");
		
		$.post(SITE_URL + "/ajax/call.php", { defaultExpand: $.defaultExpand, defaultExpandMaxioms: $.defaultExpandMaxioms, defaultOpen: $.defaultOpen, action: "settings" });
		
		$("#settings_menu").animate({height: '14px', width: '100px'}, 500, function() {
			if ($("#sticky_menu li").length > 3)  $("#sticky_menu").show();
			$.fn.settingsMaxioms();
		});
		return false;
	});
	
	$(".hide-close span").live("click", function(){
		/*var elems = $(this).parent().find(".hiddenDetails");
		if ($(this).hasClass("close")) {
			elems.show("slow");
			$(this).removeClass("close").addClass("open");
		}*/
		$(this).parents(".quote-box").find(".hiddenDetails").toggle(($(this).hasClass("close")?true:false)).end().find("span").toggleClass("close");
		//$(this).parents(".quote-box").find("p."+($(this).hasClass("close")?'long':'short')).trigger("click");
		if ($.defaultExpandMaxioms) {
			$(this).parents(".quote-box").find("p.short").hide().end().find("p.long").show();
		} else {
			$(this).parents(".quote-box").find("p.long").hide().end().find("p.short").show();
		}
		$(this).text($(this).hasClass("close")?openL:closeL);
	})
	
	$.fn.settingsMaxioms();
	
	$("#top_login_form").validationEngine({
			success : function() {
				p = hash_str($("#passwd_top").val());
				$("#password_top").val(p);
				$("#loginHashToken_top").val("");
				$("#passwd_top").val("")
				$("#top_login_form").submit();
			}
		});
	
	$(".sign-in").click(function() {
		$("#sign-in-over").toggle();
	});
	
	$(".select-category em").click(function() {
		$("#hidden-div-categories").toggle();
	});
	
	$("#cat_name").keyup(function(){
		var val = $(this).val();
		if (val.length > 1) {
			$.post(SITE_URL + "/ajax/call.php", { value: val, action: "search" },
			 function(data){
				 $(".hidden-categories-content ul").html(data);
				 $("#hidden-div-categories").show();
			 });
		}
	});
	
	$("#author_select").keyup(function(){
		var elem = $(this);
		var val = elem.val();
		if (val.length > 1) {
			$.post(SITE_URL + "/ajax/call.php", { value: val, action: "author_select" },
			 function(data){
				 if (data)
				 	elem.next().html(data).show();
			 });
		}
	});
	
	$("#category_select").keyup(function(){
		var elem = $(this);
		var val = elem.val();
		var type = $("#type").val();
		if (val.length > 1) {
			$.post(SITE_URL + "/ajax/call.php", { value: val, action: "category_select", type: type },
			 function(data){
				 if (data)
					 elem.next().html(data).show();
			 });
		}
	});
	
	$("#checkMaxiom").click(function(){
		var val = $("#quote").val();
		$.post(SITE_URL + "/ajax/call.php", { value: val, action: "checkMaxiom" },
			 function(data){
				 var res = data.split("|#");
				 $("#maxioms").html(res[0]);
				 if (res[1] == '0') {
					 $('#submit').show()
				 }
				 	
			 });
	});
	
	$("#authors a, #categories a").live("click", function(){
		var elem = $(this);
		elem.parents(".div_input").find("input").val(elem.text());
		elem.parents("ul").hide();
		return false;
	});
	
	$("#authors em, #categories em").live("click", function(){
		$(this).parents("ul").hide();
	});
	
	/*$("#more").click(function(){//[class!=list_more]
		elem = $(this);
		elem.children(".text").hide();
		elem.children(".image").show();
			$.post(SITE_URL + "/ajax/call.php", { page: $.page, q : $.key, catIDs : $.categories, quoteID : $.quoteID, action: $.action },
			 function(data){
				 var res = data.split("|");
				 $.page++;
				 if (res[1] != "0") {
						$("#settings_menu").animate({height: '14px', width: '100px'}, 500, function() {
							$(".latest-quotes").append(res[0]);
							$('#quotesNo').html(res[1]);
							$('#sticky_menu ul').append("<li><a href='#page" + ($.page - 1) +"'>- Page " + ($.page - 1) +"</a></li>").parent().show();
							$.fn.settingsMaxioms();
						});
				 } else {
					 elem.hide();
				 }
				 elem.children(".image").hide();
				 elem.children(".text").show();
			 });
	});*/
	
	$(".ajaxSend").die("click");
	$(".ajaxSend").live("click", function(){
			elem = $(this);
			$.post(elem.attr("href"), { action: 'admin' },
			 function(data){
				 document.location = document.location;
			 });
			 return false;
	});

	$("#subscribe-newsletter").validationEngine({
		success : function() {
			data = $("#subscribe-newsletter").serialize();
			$.post(SITE_URL + "/ajax/call.php", { data: data },
			 function(data){
				 $("#dialog").html("<p>You Have Been Successfully Subscribed.</p>").animate({"opacity": "toggle"}, {duration: 3000, complete: function() { $(this).animate({"opacity": "toggle"}, {duration: 3000})
				 }});
			 });
		}
	});
	
	
	$("a.rep").die("click");
	$("a.rep").live("click", function() {
		var elem = $(this);
		$.fancybox({
					'href' : elem.attr("href"),
					'width'				: 565,
					'height'			: 430,
					'autoScale'     	: true,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'type'				: 'iframe'
				});
		return false;
	});
	
	$("a.feedback").die("click");
	$("a.feedback").live("click", function() {
		var elem = $(this);
		$.fancybox({
					'href' : elem.attr("href"),
					'width'				: 565,
					'height'			: 290,
					'autoScale'     	: true,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'type'				: 'iframe'
				});
		return false;
	});
	
	$("a.login").die("click");
	$("a.login").live("click", function() {
		lastElem = $(this);
		if ($.isLoggedIn == 0) {
			$.fancybox({
						'href' : SITE_URL+'/login_register.php',
						'width'				: 700,
						'height'			: 320,
						'autoScale'     	: true,
						'transitionIn'		: 'none',
						'transitionOut'		: 'none',
						'type'				: 'iframe'
					});
			return false;
		}
	});
	$("a.fav").die("click");
	$("a.fav").live("click", function() {
		var elem = $(this);
		lastElem = elem;
		$.get(elem.attr("href"), function(res) {
			if (res == 't') {
				$("#dialog").html("<p>The quote was added to your favorites.</p>").animate({"opacity": "toggle"}, {duration: 3000, complete: function() { $(this).animate({"opacity": "toggle"}, {duration: 3000})
				/*if ($.reload == 1) {
					location.reload(true);
				}*/
				}});
			} else {
				$.fancybox({
						'href' : SITE_URL+'/login_register.php',
						'width'				: 700,
						'height'			: 320,
						'autoScale'     	: true,
						'transitionIn'		: 'none',
						'transitionOut'		: 'none',
						'type'				: 'iframe'
					});
			}
	 });
	 return false;
	})
	
	$("a.remfav").die("click");
	$("a.remfav").live("click", function() {
		var elem = $(this);
		lastElem = elem;
		$.get(elem.attr("href"), function(res) {
			if (res == 't') {
				elem.parents(".content-latest-quotes").remove();
				$("#dialog").html("<p>The quote was removed from your favorites.</p>").animate({"opacity": "toggle"}, {duration: 3000, complete: function() { $(this).animate({"opacity": "toggle"}, {duration: 3000})
				}});
			} else {
				$.fancybox({
						'href' : SITE_URL+'/login_register.php',
						'width'				: 700,
						'height'			: 320,
						'autoScale'     	: true,
						'transitionIn'		: 'none',
						'transitionOut'		: 'none',
						'type'				: 'iframe'
					});
			}
	 });
	 return false;
	})
	
	$("a.rate").die("click");
	$("a.rate").live("click", function() {
		var elem = $(this);
		$.get(elem.attr("href"), function(res) {
			if (res) {
				if (elem.hasClass("up"))
					elem.next().html(res);
				else
					elem.prev().html(res);
			}
				
			$("#dialog").html("<p>Thank you for your vote.</p>").animate({"opacity": "toggle"}, {duration: 3000, complete: function() { $(this).animate({"opacity": "toggle"}, {duration: 3000})
			}});
	 });
	 return false;
	})
	
	$("p.short").live("click",function(){
		$(this).hide();
		$(this).next().show();
	});
	
	$("p.long").live("click",function(){
		$(this).hide();
		$(this).prev().show();
	});
	
	$("#search-quote").titleInValue();	
	/*
	$(function() {
			var offset = $("#page-content").offset();
			$("#sticky_menu").css({left :  $("body").width() / 2 + 479, top : ($(window).height()-$("#sticky_menu").height()-100 ) + 'px' } );
			var topPadding = 0;
			$(window).scroll(function() {
				var offsetS = $("#sticky_menu").offset();
				alert((offsetS.top+$("#sticky_menu").height()) + " " + ($("body").height()-375));alert($(window).scrollTop() +  $(window).height() - $("#sticky_menu").height()-20);
					if ((offsetS.top+$("#sticky_menu").height()) < $("body").height()-375) {alert('x');//$(window).scrollTop() > 300 &&
							$("#sticky_menu").stop().animate({
									top:$(window).scrollTop() +  $(window).height() - $("#sticky_menu").height()-20
							});
					} else {// if ($(window).scrollTop() <= offset.top) {alert('y');
							$("#sticky_menu").stop().animate({
									top : $("body").height()-457//($("body").height()-$("#sticky_menu").height()-100 )
							});
					}/* else {alert('z');
							$("#sticky_menu").stop().animate({
									top: $("body").height()-400
							});
					}
			});
	});*/
	
	$("a.tab").click(function(i){
		var parent = $(this).parent();
		var tab = $(this).data("tab");
		parent.find("p.tab").hide();
		parent.find("p[data-tab='"+tab+"']").show();
		parent.find("a.tab").removeClass("active");
		parent.find("a[data-tab='"+tab+"']").addClass("active");
		return false;
	});
	
	$('.edit_text').editable(SITE_URL + "/ajax/call.php", { 
			 type      : 'textarea',
			 cancel    : 'Cancel',
			 submit    : 'Save',
			 indicator : '<img src="'+SITE_URL+'/assets/images/indicator.gif">',
			 tooltip   : 'Apasa pentru a edita...',
			 rows : 5,
			 submitdata : function(value, settings) {
					 return {action: "editQuote", quoteID : $(this).closest("div.quote").children("input").val()}
			 }
	 });
	
});

function jqCheckAll( id, name, flag )
{
	 if (flag == 0)
	 {
		$("INPUT[type='checkbox']").attr('checked', false);
	 }
	 else
	 {
		$("INPUT[type='checkbox']").attr('checked', true);
	 }
}
function checkUncheckAll(){
	if($('#catIDs_all:checked').val() == null){ 
		jqCheckAll('search_form', 'search_form', 0); 
		$('#cat_name').val('category');
	}else{ 
		jqCheckAll('search_form', 'search_form', 1); 
		$('#categoryID').val('-1');
		$('#cat_name').val('All Categories'); 
	}
}
function checkUncheckAll2(){
	 if($('#catIDs_all:checked').val() == null){ 
		jqCheckAll('search_form', 'search_form', 1); 
		$('#categoryID').val('-1');
		$('#cat_name').val('All Categories');  
	}else{ 
		jqCheckAll('search_form', 'search_form', 0);
		$('#cat_name').val('category');
	}  
}
function checkOne(catID,catName){
	$('#categoryID').val($('#categoryID').val().replace(catID + ',' ,''));
	$('#categoryID').val($('#categoryID').val().replace(',' + catID,''));
	$('#categoryID').val($('#categoryID').val().replace(catID,''));
	
	if($('#categoryID').val() == '' || $('#categoryID').val() == '-1'){
		if($('#catIDs_' + catID + ':checked').val() == null){
			$('#categoryID').val($('#categoryID').val().replace(catID + ',' ,''));
			$('#categoryID').val($('#categoryID').val().replace(',' + catID,''));
			$('#categoryID').val($('#categoryID').val().replace(catID,''));
			$('#cat_name').val('category');
		}else{
			$('#categoryID').val(catID);
			$('#cat_name').val(catName);
		}
	}else{
		if($('#catIDs_' + catID + ':checked').val() == null){
			$('#categoryID').val($('#categoryID').val().replace(catID + ',' ,''));
			$('#categoryID').val($('#categoryID').val().replace(',' + catID,''));
			$('#categoryID').val($('#categoryID').val().replace(catID,''));
			$('#cat_name').val('category');
		}else{
			$('#categoryID').val($('#categoryID').val() + ',' + catID);
			$('#cat_name').val(catName);
		}
	}
}

$.fn.extend({
	titleInValue: function(options) {
		var form=this;
		this.find('input[type="text"],textarea').each(function(i) {
			var title=$(this).attr('title');
			if ($(this).val()=='' || $(this).val()==title) {
				$(this).addClass('waiting').val(title)
					.blur(function(){
						$(this).removeClass('active');
						if ($(this).val()=='' || $(this).val()==title) $(this).addClass('waiting').val(title);
					})
					.focus(function(){
						$(this).removeClass('waiting').addClass('active');
						if ($(this).val()==title) $(this).val('');
					});
				//
			}
			else
			{
				$(this).unbind('click');
			}
		});
		// clear the titles on submit
		$(this).unbind('submit');
		$(this).bind('submit',function(){
			form.find('input[type="text"],textarea').each(function(i){
				if ($(this).val()==$(this).attr('title')) $(this).val('');
			});
			return true;
		});
	}
	
});

$.fn.settingsMaxioms = function(options) {
	if ($.defaultExpand) {
		$(".hiddenDetails").hide();
		$(".hide-close span").addClass("close").trigger("click").parent().show();
	} else {
		$(".hide-close span").removeClass("close").trigger("click").parent().hide();
	}
	if ($.defaultOpen) {
		$(".hide-close").show();
	} else {
		if (!$.defaultExpand)
			$(".hide-close").hide();
	}
}
