/*
	-- Global javascript functions --
	
	-- Author: 		e950
	-- URL:			http://www.e950.lv/
	-- Modified: 	30.12.2008
*/

$(document).ready(function() {
	
	var base_url  = $("#base_url").text();
	
	$("#flash_logo").media({
		width:149,
		height: 137,
		src: base_url+"swf/logo.swf",
		params: {
			movie: base_url+"swf/logo.swf",
			scale: 'noscale',
			menu: 'false'
		}
	});
	
	$("#piedava").media({
		width:140,
		height: 90,
		src: base_url+"swf/piedava.swf",
		params: {
			movie: base_url+"swf/piedava.swf",
			scale: 'noscale',
			menu: 'false'
		}
	});
	
	$(".show_content, .show_comments").unbind('click').click(function(){
		var id = $(this).attr('rel');
		var mode = $(this).attr('class');
		
		$(".article_full, .comments").hide();
		$(".subfoot *, .show_content").show();
		
		var executed = false;
		$("#a"+id+", #c"+id).slideDown(function(){
			if(executed == false) {
				$(".b"+id+", .show_content[rel="+id+"], .show_comments[rel="+id+"]").hide();
				
				if(mode == 'show_content') {
					$.scrollTo("#article_"+id, 800);
				} else {
					$.scrollTo("#c"+id, 800);
				}
				
						
				if($("#loader_"+id).attr('id')) {
					$.getJSON("/ajax/load_comments/"+id+"/100/1/", function(data) {
						$("#loader_"+id).remove();
						
						var prepend = "";
						$.each(data, function(i, item) {
							prepend += '<div class="comment">\
								<div class="comment_side"><span class="comment_title">'+item.comment_title+'</span><span class="comment_date">'+item.comment_date+'</span></div>\
								<p class="comment_text">'+item.comment_text+'</p>\
							</div>';
						});
						
						$("#c"+id).prepend(prepend);
						
					});
				}
			}
			executed = true;
		});
		
		return false;
	});
	
	$("input[class^=add_comment_]").unbind('click').click(function(){
		var id = $(this).attr('class').substr(12);
		var error = false;
		
		if(! $("#comment_title_"+id).val()) {
			$("#comment_title_"+id).effect('highlight', {color: '#528EAB'}, 1200);
			error = true;
		}
		
		if(! $("#comment_text_"+id).val()) {
			$("#comment_text_"+id).effect('highlight', {color: '#528EAB'}, 1200);
			error = true;
		}
		
		if($("#comment_test"+id).val()) {
			error = true;
		}
		
		if(error == false) {
			$(this).hide();		
			$(".add_comment_"+id).before('<img class="comm_loader" style="border:0;" src="'+base_url+'img/frontend/design/loader.gif" alt="notiek ielāde..." style="float:right; clear:both;" />');
			
			var comment_title = $("#comment_title_"+id).val();
			var comment_text = $("#comment_text_"+id).val();
			
			$.post("/ajax/add_comment/"+id, {comment_title:comment_title, comment_text:comment_text}, function(data) {
				
				$("#c"+id+" .lightItalic").remove();
				
				$("#c"+id).prepend('<div class="comment"><div class="comment_side"><span class="comment_title">'+comment_title+'</span><br/>'+data.comment_date+'</div><p class="comment_text">'+data.comment_text+'</p></div>');
				
				$("#c"+id+" .comment:first").effect('highlight', {color: '#528EAB'}, 1200);
				$("#comment_title_"+id+", #comment_text_"+id).val("");
				$("#c"+id+" .comm_loader").remove();
				$("#c"+id+" input[class^=add_comment_]").show();		
					
			}, "json");
			
		}
		
	});
	
	$(".contacts").unbind('click').click(function(){
		$("#contacts_full").slideToggle();
		return false;
	});
	
	function change_entries() {
		$('#calendar_full .entries').html('<div style="width:100%;text-align:center;"><img src="'+base_url+'img/frontend/design/loader.gif" alt="loading" /></div>');
		
		var year = $("#calendar_years").val();
		var month = $(".months .calendar_months").val();
		
		$.getJSON("/ajax/get_calendar_titles/"+year+"/"+month, function(data) {
			var html = "";
			$.each(data, function(i, item) {
				html += '<a href="'+base_url+i+'/">'+item+'</a>'; 
			});
			$("#calendar_full .entries").html(html);
		});
	}
	
	$(".calendar").unbind('click').click(function(){
		$("#calendar_full").slideToggle();
		change_entries();
		return false;
	});
	
	$("#calendar_full #calendar_years").unbind('change').change(function(){
		$("#calendar_full .months").removeClass('months').addClass('monthsH');
		$("#m"+$(this).val()).removeClass('monthsH').addClass('months');
		change_entries();
	});
	
	$("#calendar_full .calendar_months").unbind('change').change(function(){
		change_entries();
	});

});