/**
 * @author danve
 */

//js_vars is set in index.php as var and pass through index.tpl.html

/////////////////////////////////////////// video functions ////////////////
function loadVideoPlayer(video_path)
{
	$('body').append("<div id='video_player' class='video_player'><div class='video_player_header'>Close <img id='kill_video' src='" + js_vars.app_url + "app_images/close_btn.jpg' /></div><div id='player_holder'></div></div>");
	$("#video_player").css("background-color",'red');
	$("#video_player").css("position",'absolute');
	$("#video_player").css("left",0);
	$("#video_player").css("top",0);
	//$("#video_player").css("width","400px");
	//$("#video_player").css("height","300px");
	
	$("#video_player").hide();
	
	$("#kill_video").click(function(){
		removeVideoPlayer();
	});
	
	$.post(
			js_vars.app_url + "flash_player/player.php",
			{video:video_path},
			function(data)
			{
				$('#player_holder').html(data);
				showItCentral($("#video_player"));
			}
		);	
}


function removeVideoPlayer()
{
	$('#video_player').remove();
	removeModalBackground();
}


/**
 * this is enabling the video buttons to play their video
 **/
function enableVideoPlayer()
{
	$(".video_link").click(
						   function()
						   {
								loadVideoPlayer($(this).attr('link'));
						   });
}

/////////////////////////////////////////// end video functions ////////////////




//kill the modal object
function KillModal()
{
	$.unblockUI();
	setTimeout( function(){
		$('#modal_box_content').html('');
		$("#modal_box").remove();
		}, 1000);
	
	
}

/**
 * this takes as parameter an DOM element and position it in the middle and center of page;
 * also, it showsup an semitransparent MODAL background
 **/
function ShowUICentral(_object)
{
	//showModalBackground(); //this is not used here; this function is called with $.blockUI() function;
	
	_object.css('display','block');
	
	var object_width = $(_object).width();
	var object_height = $(_object).height();
	top_scroll = $(document).scrollTop();

//	alert($(document).scrollTop());

	var left_position = ($(window).width() - object_width ) /2;
	var top_position = ($(window).height() - object_height ) /2;
	
	if(top_position < 0)
		top_position = 0;
	if(top_scroll > 0)
		top_position += top_scroll;
	
	if(left_position < 0)
		left_position = 0;
		
		
	_object.css('position','absolute');
	_object.css('z-index','1000');
	_object.css('top',top_position);
	_object.css('left',left_position);
	
		$(window).resize(function(){
		if(_object.css('display') == "block"){
			ShowItCentral(_object);
		}
	});
	
}

/**
 * this shows up the modal semitransparent background
 **/
function showModalBackground()
{
	$('body').append("<div id='back_dark' class='back_dark'>&nbsp;</div>");
	//$("#back_dark").css("background-color",'red');
	$("#back_dark").css("position",'absolute');
	$("#back_dark").css("z-index",1000000);
	$("#back_dark").css("left",0);
	$("#back_dark").css("top",0);
	$("#back_dark").css("width",$(document).width());
	$("#back_dark").css("height",$(document).height());
	
	
	$(window).resize(function(){
		if($("#back_dark").css('display') == "block"){
			$("#back_dark").css("width",$(document).width());
			$("#back_dark").css("height",$(document).height());
		}
	});
	
	//hide nay given selects
}


/**
 * remove the modal background;
 **/
function removeModalBackground()
{
	$('#back_dark').remove();
}

/**
 * this takes as parameter an DOM element and position it in the middle and center of page;
 * also, it showsup an semitransparent MODAL background
 **/
function showItCentral(_object)
{
	showModalBackground();
	
	_object.css('display','block');
	
	var object_width = $(_object).width();
	var object_height = $(_object).height();
	top_scroll = $(document).scrollTop();

//	alert($(document).scrollTop());

	var left_position = ($(window).width() - object_width ) /2;
	var top_position = ($(window).height() - object_height ) /2;
	
	if(top_position < 0)
		top_position = 0;
	if(top_scroll > 0)
		top_position += top_scroll;
	
	if(left_position < 0)
		left_position = 0;
		
		
	_object.css('position','absolute');
	_object.css('z-index','1000001');
	_object.css('top',top_position);
	_object.css('left',left_position);
	
	//_object.show();
	
	$(window).resize(function(){
		showItCentral(_object);
	});
	
}




function enableThumbZoom(cssclass)
{
	var id;
	$("."+cssclass).mouseover(function(){
		//append larg image to body	
		var img = "<img id='large_"+ $(this).attr('id') +"' src = '" + $(this).attr('image') + "' />";
		id = "large_"+ $(this).attr('id');

		$('body').append(img);		
	});
	
	//remove images
	$("."+cssclass).mouseout(function(){
		//$("body").remove($("#"+id));
		($("#"+id)).remove();
	});
	
	$("."+cssclass).mousemove(function(e){
		var img_height = $("#"+id).height();
		var top;
		if( (e.pageY + img_height) > $(window).height() )
			top = e.pageY - img_height;
		else
			top = e.pageY + 10;
		
		
		$("#"+id).css('position','absolute');
		$("#"+id).css('border','solid 1px #FFFFFF');
		$("#"+id).css('top',top);
		$("#"+id).css('left',e.pageX+10);

	});
}





//fucntion for submiting contact form
function submitContactForm()
{
	var hasError = false;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

	var authorVal = $("#author").val();
	if(authorVal == '') {
		//$("#subject").after('<span class="error">You forgot to enter the subject.</span>');
		alert('You forgot to enter the Name');
		hasError = true;
		$("#author").focus();
		return false;
	}
	
	var emailFromVal = $("#author_email").val();
	if(emailFromVal == '') {
		//$("#author_email").after('<span class="error">You forgot to enter the email address to send from.</span>');
		alert('You forgot to enter the email address to send from.');
		hasError = true;
		$("#author_email").focus();
		return false;
		
	} else if(!emailReg.test(emailFromVal)) {
		//$("#author_email").after('<span class="error">Enter a valid email address to send from.</span>');
		alert('Enter a valid email address to send from');
		hasError = true;
		$("#author_email").focus();
		return false;
	}
	
	
	
	var commentVal = $("#comment").val();
	if(commentVal == '') {
		//$("#message").after('<span class="error">You forgot to enter the message.</span>');
		alert('You forgot to enter the message.');
		hasError = true;
		$("#comment").focus();
		return false;
	}
	
	return true;
}

