var _height;
function createHeights()
{	
	
	var _documentHeight = parseInt($(window).height());
	var _headerHeight   = parseInt($('#header').innerHeight());
	var _footerHeight   = parseInt($('#footer').innerHeight())+10;
	var  www =_headerHeight+_footerHeight;
	_height = _documentHeight-(_headerHeight+_footerHeight)+'px';
	
	//$('#content').height(_height);
	$('#content').css({"height":_height});
	//$('#main_content').css({"height":_height});
	$('#content').parent().css({"height":_height});

	$('#header').css({"width":"100%"});
	$('#content').css({"width":"100%"});
	$('#footer').css({"width":"100%"});
    //$('#content').width($('#content').innerWidth()-1+"px");
	
	if($(window).height()>700){
		//alert($('#right_coloumn').innerHeight());
		$('#main_content').addClass('right_coloumn_big_screen');
		
		//$('#main_content').height($('#right_coloumn').innerHeight());
		
	}else{
		$('#main_content').removeClass('right_coloumn_big_screen');
	}
	
	
}

function getTheBiggestHeight(){
	
	var big_height;
	if($('#left_coloumn').innerHeight() > $('#right_coloumn').innerHeight()){
		big_height = $('#left_coloumn').innerHeight();
	}else if($('#left_coloumn').innerHeight() < $('#right_coloumn').innerHeight()){
		big_height = $('#right_coloumn').innerHeight();
	}else{
		big_height = $('#left_coloumn').innerHeight();
	}
	if(big_height<$('#content').innerHeight()){
		big_height = $('#content').innerHeight();
	}
	$('#main_content').height(big_height);
	
}

function redirect(page_id,paging,title)
{
	if(!page_id){
		return;
	}
	//alert(paging);
	//alert(page_id);
	$("#main_content").html('');
	$("#main_content").load(
					'getContent.php?id='+page_id, 
					{page: paging} , 
					function(){  
					   
						$('#content').jScrollPane({animateTo:true}); 
						$('#main_content').hide();
						$('#main_content').slideDown('slow'); 
						$('.jScrollPaneContainer').css({'background-color' : $("#content").css('background-color')});
						$('#content')[0].scrollTo('#main_content');
						pageTracker._trackPageview("http://www.poualu.gr/index.php?page_id="+page_id );
						//alert("http://www.poualu.gr?page_id="+page_id);
					});

}


function checkFillInputs(forma){
	var inputs = forma.find(".check").get();
	
	var i=0;
	var empty=true;
	for(i=0; i<inputs.length; i++){
		if(jQuery.trim($(inputs[i]).val())==''){
			empty = false;
			$(inputs[i]).addClass("form_error");							
		}	
	}
	if(!empty){
		$('.error').show();
		$('.error_mgs').hide();
		$('#error_fill_form').show('slow');
	}
	return  empty;
}
function checkPasswords(){
		
	var empty=true;
	var pass_1 = $('#password').val();
	var pass_2 = $('#password2').val();

	if(pass_1!=pass_2){
		$('.error').show();
		$('.error_mgs').hide();
		$('#error_fill_passwords').show('slow');
		empty = false;
		
		
	}
	if(pass_1.length<=4 || pass_2.length<=4 ){
		$('.error').show();
		$('.error_mgs').hide();
		$('#error_fill_passwords_length').show('slow');
		empty = false;
	}
	
	return  empty;
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
$(document).ready(function(){
	
	
	if(gup('page_id')){
		createHeights();
		redirect(gup('page_id'),0);
	}else{
		createHeights();
		$("#main_content").load('getContent.php','',
						function(){
							$('#content').jScrollPane({animateTo:true});
							$('#main_content').hide(); 
							$('#main_content').slideDown('slow');  
						});
	}
	$(window).resize(function(){
		createHeights();
		
	});
	$(".redirection").live("mousedown", function(){
        
		createHeights(); 
		var page_id = $(this).attr("rev");
		var paging = $(this).attr("rel");
	    redirect(page_id, paging);
	}); 
//-----------------------video home page	
	$('#video_thumb').live("mousedown",function(){
		$('#pop_video').html($('#pop_content').html());
		$('#overlay').show();
		var arrPageSizes = ___getPageSize();
		var arrPageScroll = ___getPageScroll();
		$('#pop_video').css({
				top:	(arrPageSizes[3] / 10)-60,
				left:	(arrPageSizes[0] - $('#pop_video').outerWidth())/2
		}).show();
		return false;
	});

	$('#video_thumb_close').live("mousedown",function(){
		$('#overlay').hide();
		$('#pop_video').hide();
		$('#pop_video').html('');
		return false;
	});

	//-----------------------send to friend
	$('.send_popupcontainer_button').live("mousedown",function(){
		
		//$('#overlay').show();
		var arrPageSizes = ___getPageSize();
		var arrPageScroll = ___getPageScroll();
		$('#send_popupcontainer').css({
				top:	(arrPageSizes[3] / 10)-60,
				left:	(arrPageSizes[0] - $('#send_popupcontainer').outerWidth())/2
		}).show();
		return false;
	});

	$('#send_popupcontainer_close').live("mousedown",function(){
		$('#overlay').hide();
		$('#send_popupcontainer').hide();
		return false;
	});
	
//-----------------------login
	$("#login_button").live("mousedown", function(){
           	
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=login",
				   dataType: "html",
				   success: function(id){
					  if(id!=0){
							
							var user_data = id.split("|");
							
							//$("user_tools").html(''+$("user_tools").html());
							$("#global_user_name").html(user_data[1]);
							$("#log_out_button").attr({"rel":user_data[0]});
							$("#user_tools").show();
							$("#top_banner").hide();
	    					redirect(328,0);
					  }
					  else {
						  	$('.error').hide();
						    $('#error_no_user').show('slow');
					  }
				   }
			});
	}); 
//-----------------------forgot password
	$("#forg_pass_button").live("mousedown", function(){
	    $('.error').hide();
		var forma = $('#forma2');
        if(checkFillInputs(forma)==true){
			$('.error').hide();
			var str = $("#forma2").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=forg_pass",
				   dataType: "html",
				   success: function(id){  
					  if(id!=0){
						  $('.ok').show();
						  $('#form2').hide();
					  }
					  else{
						  $('#error_no_email').show(); 	
					  }
				   }
			});
		}else{
			$('.error').hide();
			$('#error_fill_form').show('slow'); 
		}
	});	
//-----------------------logout
	$("#log_out_button").live("mousedown", function(){
           	
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=logout",
				   dataType: "html",
				   success: function(id){ 
					  if(id){
							
							$("#global_user_name").html('');
							$("#log_out_button").attr({"rel":""});
							$("#user_tools").hide('slow');
							$("#top_banner").show();
	    					redirect(328,0);
					  }
				   }
			});
	}); 
//-----------------------signup
	$("#signup_button").live("mousedown", function(){
		//alert('sdfsdfsd');										   
		var forma = $('#forma');
		//
        if(checkFillInputs(forma)==true && checkPasswords()==true){
			
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=signup",
				   dataType: "html",
				   success: function(id){  
					  if(id!=0){
						  $('.error').hide();
						  $('.ok').show('slow');
						  $('#content')[0].scrollTo('#top_signup');
						  $('#form').hide();
					  }
					  else{
						  $('.error_mgs').hide();
						  $('.error').show();
						  $('#error_old_email').show('slow');
						  	
					  }
				   }
			});
		}else{
			
			$('#content')[0].scrollTo('#top_signup');
			
		}
		
	});
	
//-----------------------create post
	$("#create_post_button").live("mousedown", function(){
		//alert('sdfsdfsd');										   
		var forma = $('#forma');
		//
        if(checkFillInputs(forma)==true){
			
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=createpost",
				   dataType: "html",
				   success: function(id){
					  //alert(id);
					  if(id!=0){
						  //alert(id);
						  $('.error').hide();
						  //$('#form').hide();
						  redirect($('#parent').val(),0);
					  }
					  else{
						  $('.error_mgs').hide();
						  $('.error').show();
						  $('#error_fill_form').show('slow'); 	
					  }
				   }
			});
		}
		
	});	
	
//-----------------------post
	$("#answer_button").live("mousedown", function(){
		//alert('sdfsdfsd');										   
		var forma = $('#forma');
		//
        if(checkFillInputs(forma)==true){
			
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=sendAnswer",
				   dataType: "html",
				   success: function(id){
					  //alert(id);
					  if(id!=0){
						  //alert(id);
						  $('.error').hide();
						  //$('#form').hide();
						  redirect($('#page_id').val(),0);
					  }
					  else{
						  $('.error_mgs').hide();
						  $('.error').show();
						  $('#error_fill_form').show('slow'); 	
					  }
				   }
			});
		}
		
	});
	
	
	$('a.gotopost').live('mousedown',function(){
										  
		$('#content')[0].scrollTo('#form_post');
		var answer_page_id = $(this).attr("rel");
		var user_name = $(this).attr("rev");
		var post_type = $(this).attr("alt");
		
		
		$('#title_answer').html('Απάντησε');
		$('#answer_page_id').val(answer_page_id);
		
		
		if(!post_type){
			$('#title_answer').html('Απάντησε στον χρήστη <b>'+user_name+'</b>');
			var body_text = $(this).parent().prev().html();
			var body_text_details = $(this).parent().prev().prev().html();
			$('#prev_text').val(body_text_details+body_text);
			//alert(body_text);
		}
		
		
		$('#forma').show();
		
		return false;
	});
//-----------------------newsletter
	$("#newsletter_button").live("mousedown", function(){
		//alert('sdfsdfsd');										   
		var forma = $('#forma');
		//
        if(checkFillInputs(forma)==true){
			
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=signup",
				   dataType: "html",
				   success: function(id){  
					  if(id!=0){
						  $('.error').hide();
						  $('.ok').show('slow');
						  $('#content')[0].scrollTo('#top_newsletter');
						  $('#form').hide();
					  }
					  else{
						  $('.error_mgs').hide();
						  $('.error').show();
						  $('#error_old_email').show('slow');
						  	
					  }
				   }
			});
		}else{
			
			$('#content')[0].scrollTo('#top_newsletter');
			
		}
		
	});
//-----------------------help desk button
	$("#help_desk_button").live("mousedown", function(){										   
		var forma = $('#forma');
        if(checkFillInputs(forma)==true){
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=helpdesk",
				   dataType: "html",
				   success: function(id){  
					  if(id!=0){
						  $('.error').hide();
						  $('.ok').show('slow');
						  $('#form').hide();
					  }
				   }
			});
		}
	});	
	$(".open_close_button").live("mousedown", function(){
           
           if($(this).hasClass("open_button"))
           {
               $(this).removeClass("open_button");
               $(this).addClass("close_button");
			   if($(this).next().hasClass("answer")){
               		$(this).next().slideDown(); 
			   }
           }
           else if($(this).hasClass("close_button"))
           {
               $(this).removeClass("close_button");
               $(this).addClass("open_button");
			   if($(this).next().hasClass("answer")){
               		$(this).next().slideUp(); 
			   }
           }  
             
      });
	//-----------------------help desk button
	$("#contact_button").live("mousedown", function(){										   
		var forma = $('#forma');
        if(checkFillInputs(forma)==true){
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=contact",
				   dataType: "html",
				   success: function(id){ 
				      
					  if(id!=0){
						  $('.error').hide();
						  $('.ok').show('slow');
						  $('#form').hide();
					  }
				   }
			});
		}
	});	
	//-----------------------help desk button
	$("#ecard_button").live("mousedown", function(){										   
		var forma = $('#forma');
        if(checkFillInputs(forma)==true){
			var str = $("#forma").serialize();
			$.ajax({
				   type: "POST",
				   url: "actions.php",
				   data: str+"&a=ecard",
				   dataType: "html",
				   success: function(id){ 
				      
					  if(id!=0){
						  $('.error').hide();
						  $('.ok').show('slow');
						  $('#form').hide();
					  }
				   }
			});
		}
	});	

	
	
});

