var current_opened_form = '';
var current_category = '0';	
var current_sub_category = '0';
function openPerformanceLightbox() {
	$('#performance_link').trigger('click');
}

$(document).ready(function () {
	$('body').pngFix();
	
	$.trigger_current_form = function() {		
		$('#' + current_opened_form).trigger('click');
	}

	$('.lightbox').lightBox();

	$('#performance_link').lightBox({type: 'html'});

	$('.print_comment').click(function() {
		var currentTime = new Date();
		var name = 'prFrm'+ currentTime.getSeconds() + currentTime.getMinutes()  + currentTime.getHours();		
		var framebox = $('<iframe style="display:none;"></iframe>');
		$(framebox).css('position', 'absolute');
		$(framebox).css('top', '-1200px');
		$(framebox).css('left', '-1200px');		
		$(framebox).attr('src', $(this).attr('href'));
		$(framebox).attr('name', name);
		$(framebox).appendTo('body');	
		$(framebox).show();		
		return false;
	});

	$('.add_tag_form').bind('submit', function(){

		value = $(this).find('input:first').val();

		if(value)
			window.location.href = $(this).attr('action') + encodeURIComponent($(this).find('input:first').val());

		return false;
	})

	String.prototype.trim = function() {
		a = this.replace(/^\s+/, '');
		return a.replace(/\s+$/, '');
	};

	RegExp.escape = function(text) {
	  if (!arguments.callee.sRE) {
		var specials = [
		  '/', '.', '*', '+', '?', '|',
		  '(', ')', '[', ']', '{', '}', '\\'
		];
		arguments.callee.sRE = new RegExp(
		  '(\\' + specials.join('|\\') + ')', 'g'
		);
	  }
	  return text.replace(arguments.callee.sRE, '\\$1');
	}

	function makeArray(obj) {
		var a = new Array();
		for(t=0; t < obj.length; t++)
		{
			value = $(obj[t]).val();
			name = 	$(obj[t]).attr('name');
			if(!$(obj[t]).attr("disabled"))
			{
				switch($(obj[t]).attr("type")){
					case 'checkbox':
						if($(obj[t]).attr("checked"))
							a.push({name: name, value: value});
						break;
					default:
						a.push({name: name, value: value});
				}
			}		
		}

		a.push({name: 'ajax_request', value: 1});

		return a;
	}

	$.returnMenuClickEvent = function() {
		$('.menu_link').bind('click', function() {
			window.location.href = $(this).attr('href');
		});		
	}

	$.setMenuEditNews = function(post_id) {
		
		current_category = $('#new_post_category_id').val();
		current_sub_category = $('#new_post_sub_category_id').val();
		$('.menu_link').removeClass('on');		
		if($('#post_sub_category_id_' + post_id).val()!='0') {
			$('#'+$('#post_sub_category_id_' + post_id).val()).find('td a:first').addClass('on');
		} else
			$('#'+$('#post_category_id_' + post_id).val()).find('td a:first').addClass('on');

		$('.menu_link').bind('click', function() {
			$('.menu_link').removeClass('on');
			row = $(this).parent().parent();

			if(row.attr('rel')!='0') {
				$('#post_category_id_' + post_id).val(row.attr('rel'));
				$('#post_sub_category_id_' + post_id).val(row.attr('id'));
			} else {
				$('#post_category_id_' + post_id).val(row.attr('id'));
				$('#post_sub_category_id_' + post_id).val('0');
			}

			$(this).addClass('on');

			return false;
		});
	}

	$.setMenuAddNews = function() {
		
		current_category = $('#new_post_category_id').val();
		current_sub_category = $('#new_post_sub_category_id').val();		

		$('.menu_link').bind('click', function() {
			$('.menu_link').removeClass('on');
			row = $(this).parent().parent();

			if(row.attr('rel')!='0') {
				$('#new_post_category_id').val(row.attr('rel'));
				$('#new_post_sub_category_id').val(row.attr('id'));
			} else {
				$('#new_post_category_id').val(row.attr('id'));
				$('#new_post_sub_category_id').val('0');
			}

			$(this).addClass('on');

			return false;
		});
	}

	$.unsetMenuAddLink = function() {				
		$('#new_post_category_id').val(current_category);
		$('#new_post_sub_category_id').val(current_sub_category);

		$('.menu_link').removeClass('on');		

		if(current_sub_category!='0') {
			$('#'+current_sub_category).find('td a:first').addClass('on');
		} else
			$('#'+current_category).find('td a:first').addClass('on');

		$('.menu_link').unbind('click', $.returnMenuClickEvent());
	}

	$('#logout_user').click(function() {
		$('#logout_user_form').attr('action', '/ajax/logoutuser/');
		$('#logout_user_form').trigger('submit');	
		return false;
	});

	$('.cancel_post').click(function() {
			$.unsetMenuAddLink();
			$('#update_post_form_' + $(this).attr('rel')).hide();
			$('#post_' + $(this).attr('rel')).show();	
			current_opened_form = '';
	});
	
	$('#cancel_add_post').click(function() {		
		$('#add_post').trigger('click');
	});

	$('#cancel_add_comment').click(function() {			
		$('#add_comment_form').slideUp();
		current_opened_form = '';
	});	

	$('.cancel_comment').click(function() {
		$('#update_comment_form_' + $(this).attr('rel')).hide();
		$('#comment_' + $(this).attr('rel')).show();	
		current_opened_form = '';
	});

	$('#add_post').click(function() {	
		if((current_opened_form!= $(this).attr('id'))) $.trigger_current_form(); 

		if($('#add_post_form').is(':visible')) {
			$.unsetMenuAddLink();
			$('#add_post_form').slideUp();
			current_opened_form = '';
		} else {
			$.setMenuAddNews();
			$('#add_post_form').slideDown();
			current_opened_form = $(this).attr('id');
		}		
		return false;
	});

	$('#add_comment').click(function() {		
		if((current_opened_form!= $(this).attr('id'))) $.trigger_current_form(); 

		if($('#add_comment_form').is(':visible')) {
			$('#add_comment_form').slideUp();			
			current_opened_form = '';
		} else {
			$('#add_comment_form').slideDown();		
			current_opened_form = $(this).attr('id');
		}		
		return false;
	});

	$('.update_post').click(function() {		
		 if((current_opened_form!= $(this).attr('id'))) $.trigger_current_form(); 
		// $('.update_post_form').hide();

		// $('.post').not('#post_' + $(this).attr('rel')).show();

		if($('#post_' + $(this).attr('rel')).is(':visible')) {
			$.setMenuEditNews($(this).attr('rel'));
			$('#update_post_form_' + $(this).attr('rel')).fadeIn();
			$('#post_' + $(this).attr('rel')).hide();			
			current_opened_form = $(this).attr('id');
		} else {
			$.unsetMenuAddLink();
			$('#update_post_form_' + $(this).attr('rel')).hide();
			$('#post_' + $(this).attr('rel')).show();			
			current_opened_form = '';
			
		}		
		return false;
	});


	$('.update_comment').click(function() {
				
		if((current_opened_form!= $(this).attr('id'))) $.trigger_current_form(); 

		$('.update_comment_form').hide();

		$('.comment').not('#comment_' + $(this).attr('rel')).show();

		if($('#comment_' + $(this).attr('rel')).is(':visible')) {
			$('#update_comment_form_' + $(this).attr('rel')).fadeIn();
			$('#comment_' + $(this).attr('rel')).hide();
			current_opened_form = $(this).attr('id');
		} else {
			$('#update_comment_form_' + $(this).attr('rel')).hide();
			$('#comment_' + $(this).attr('rel')).show();
			current_opened_form = '';
		}

		return false;
	});
	

	$('.delete_post').click(function() {
		if(confirm('Are you sure yout want to delete post?')) {
			$('#delete_post_form').attr('action', '/ajax/deletepost/' + $(this).attr('rel') + '/');
			$('#delete_post_form').trigger('submit');
		}
		return false;
	});

	$('.delete_comment').click(function() {
		if(confirm('Are you sure yout whant to delete comment?')) {
			$('#delete_comment_form').attr('action', '/ajax/deletecomment/' + $(this).attr('rel') + '/');
			$('#delete_comment_form').trigger('submit');
		}
		return false;
	});

	$('#delete_category').click(function() {
		if(confirm('Are you sure yout want to delete cateogory?')) {
			$('#delete_category_form').attr('action', '/ajax/deletecategory/' +  $(this).attr('rel') + '/');
			$('#delete_category_form').trigger('submit');
		}
		return false;
	});

	$('#menu_item_link').click(function() {		

		if((current_opened_form!= $(this).attr('id'))) $.trigger_current_form(); 

		if($('#add_menu_item').is(':visible')) {
			window.location.href = window.location.href.replace(/\#.*$/, '');			
			$('#add_menu_item').slideUp();
			current_opened_form = '';
		} else {
			$('#add_menu_item').slideDown();			
			$.editMenuList();			
			current_opened_form = $(this).attr('id');
		}		
		return false;
	});

	/*
	$('.td_left').hover(function(e) {
		$('.dragHandle').addClass('dragicon');
	}, function() {		
		$(this).find('.dragHandle').removeClass('dragicon');
	});
	*/

	$('.list').hover(function() { 
		$(this).addClass('list_on');
	}, function() {
		$(this).removeClass('list_on');
	});
	$('.ajax_form').bind('submit', function(e) {		
		error_message = '';
		send = true;
		if(typeof tinyMCE != "undefined"){
			tinyMCE.triggerSave(true,true);
		}
		$(this).find('.required').removeClass('error_input');
		$(this).find('.required').each(function(i){			
			if(($(this).attr("value")=='' || $(this).attr("value")=='http://' || ($(this).attr("value")==$(this).attr('defaultValue') && !$(this).hasClass('nodef'))) && !$(this).attr("disabled") ) {
				$(this).addClass('error_input');
				error_message = 'Fill all required fields!<br/>';
				send = false;
			}
		});
		
		re_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i;		

		$(this).find('.validate_email').each(function() {
			if(!$(this).hasClass('error_input') && !$(this).attr("value").match(re_email)) {
				$(this).addClass('error_input');
				error_message = 'Wrong email!<br/>';
				send = false;
			}
		});

		if(!send) {
		   $('#jmessage').html(error_message);
		   $('#jmessage').show();

		} else {							
			form_array = $(this).map(function(){ return makeArray(this.elements); });			

			 $.ajax({
				type: "POST",
				async: false,
				url: $(this).attr('action'),
				dataType: 'json',
				data: form_array,
				success: function(data) {									
				   if(data.result) {
						if(data.message) {
							$('#jmessage').html(data.message);
							$('#jmessage').show();
						}
						
						if(data.redirect) {
							window.location.href = data.redirect;
						} else if(data.refresh) { 
							window.location.href = window.location.href.replace(/\#.*$/, '');
						} else {							
							if(data.reload_link) {
								$('#' +$('#'+data.reload_link).attr('rel')).load($('#'+data.reload_link).attr('href') + '?ajax_request=1');
							}
					   }

				   } else {					   
						error_message = data.message;
						$('#jmessage').html(data.message);
						$('#jmessage').show();
				   }
				},
				error: function(data){
					error_message = 'Form not sent!';					
				}
			 });			
		}
		
		if(error_message!='') {
			$('#jmessage').html(error_message);
			$('#jmessage').show();
		}

		setTimeout(function() {
			$("#jmessage").fadeOut();
		}, 5000);

		return false;

	});

	$('#edit_performances').click(function() {
		if($('.gridinput:last').is(':visible')) {
			$('.gridtxt').show();
			$('.gridinput').hide();
		} else {
			$('.gridtxt').hide();
			$('.gridinput').show();
		}		
	});

	$('#cancel_performances').click(function() {
		$('#edit_performances').trigger('click');
	});

	setTimeout(function() {
	    $(".timeblock").slideUp(1000);
    }, 2500);
	
	$.showMessageBox = function(text){
		$('#loading_bg').height($(document).height() + "px");
		$('#loading_bg').css({opacity: 0.4});
		$('#loading_bg').fadeIn();
		$('#jmessage').html(text);
		$('#jmessage').show();
	}

	$('#jmessage').ajaxStart(function() {	
		$.showMessageBox('Loading...');
	});

	$('#jmessage').ajaxStop(function() {		
		$('#loading_bg').fadeOut();
		if($(this).html()=='Loading...') {
			$(this).fadeOut();			
		}
	});
/*
	$('.print').click(function() {
		window.print(); 
		return false;
	});
*/
	$('#send_to_friend_button').click(function() {		
		$('#login_bg').height($(document).height() + "px");
		$('#login_bg').css({opacity: 0.4});
		$('#login_bg').fadeIn();
		$('#send_to_friend_div').fadeIn();
		return false;
	});
	

	$('#login_bg').click(function() {
		$('.login_form').hide();
	});

	$('#cancel_send_to_friend').click(function(){
		$('#login_bg').trigger('click');
	});

});

