
var popupStatus = 0;
var current_cls = false;
var current_obj = false;
function loadPopup(){
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$('#video-container').css({visibility: "hidden"});
		
		$("#backgroundPopup").fadeOut("slow");
		//$('div[id^="video-container-"]').hide();
		//$('div[id^="video-box-"]').hide();
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
		setTimeout(function() {$('#video-container').html('')},500);
	}
}

function centerPopup(cls, obj){
	current_cls = cls;
	current_obj = obj;
	$('#video-box').hide();
	//alert(cls);
	/*$('div[id^="video-box-"]').hide();
	$('div[id^="video-container-"]').hide();
	$('div[id^="audio-box-"]').hide();
	$('div[id^="audio-container-"]').hide();*/
	$('.overlay-loader').hide();
	if(cls=='btn-video') {
		var id = $(obj).attr('id');
		LoadAudioVideo($(obj).attr('rel2'));
		$('#video-box').show();
		$('#video-container').show();
		$('#video-container').css({visibility: "visible"});
		$('#login-box').hide();
		$('#mail-box').hide();
		$('#forgot-box').hide();
		$('#video-speaker').html('Video');
	} else if(cls=='btn-audio') {
		var id = $(obj).attr('id');
		LoadAudioVideo($(obj).attr('rel2'));
		$('#video-box').show();
		$('#video-container').show();
		$('#video-container').css({visibility: "visible"});
		
		$('#login-box').hide();
		$('#mail-box').hide();
		$('#forgot-box').hide();
		$('#video-speaker').html('Audio');
	} else if(cls=='btn-mail') {
		$('#forgot-box').hide();
		$('#login-box').hide();
		if(!$('#mail-box')[0]) {
			$('.overlay-loader').show();
			LoadOverlayMod('mail');
		} else {
								
			var url_arr = obj.href.replace('mailto:','').split('?');
			var url = url_arr[0];
			$('#mailer-to').html(url);
			$('#email_send_to').val(url);
			var getArr= url_arr[1].split("&");
			getArr_len = getArr.length;
			var arr_mailer = new Array();
			for(var i=0; i<getArr_len; i++) {
				var keyval=getArr[i].split("=");
				arr_mailer[keyval[0]]=keyval[1];
			}
			$('#txt_email_address').val('');
			$('#error_msg_email_send_form').html('');
			$('#success_msg_email_send_form').html('');
			$('#mailer-subject').html(unescape(arr_mailer['subject']));
			$('#subject_email_send').val(unescape(arr_mailer['subject']));
			$('#mailer-message').val(unescape(arr_mailer['body']));
			$('#mailer-captcha').val('');
			$('#mailer-from').val('');
			var c_url = $('#mailer-captcha-img').attr('src');
			
			c_url = c_url.split('?');
			$('#mailer-captcha-img').attr('src',c_url[0]+'?'+uniqid());
			
			$('#login-box').hide();
			$('#mail-box').show();
			$('#forgot-box').hide();
		}
	}else if(cls=='forgot'){
		
		$('#mail-box').hide();
		if(!$('#login-box')[0]) {
			$('.overlay-loader').show();
			LoadOverlayMod('login');
		} else {
			$('#login-box').hide();
			$('#forgot-box').show();
			$('#txt_forgot_user_name').val('');
			$('#txtcaptchacode_forgot').val('');
			$('#error_msg_forgot_form').html('');
			reloadcaptcha('forgot-captcha-img','txtcaptchacode_forgot',false);
		}
	}else {
		$('#mail-box').hide();
		if(!$('#login-box')[0]) {
			$('.overlay-loader').show();
			LoadOverlayMod('login');
		} else {
			$('#login-box').show();
			$('#forgot-box').hide();
			$('#txt_user_name').val('');
			$('#txt_user_password').val('');
			$('#error_msg_login_form').html('');
		}
	}
	
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	
	var leftPos = ((windowWidth - popupWidth) / 2);                   // this is used to get the center position of the screen
	var topPos = ((windowHeight - popupHeight) / 2);
	
	var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
	
	$("#popupContact").css({
		"position": pos,
		"top": topPos,
		"left": leftPos
	});
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}
function reloadcaptcha(objimg, objcodefld, setfocus){	
	$("#"+objimg).attr("src", SITE_URL+'captcha-user/'+uniqid());
	$('#'+objcodefld).value='';
	if(setfocus){
		if($('#'+objcodefld))
			$('#'+objcodefld).focus();
	}
}
function uniqid()
{
	var newDate = new Date;
	return newDate.getTime();
}
function LoadAudioVideo(url) {
	var url_arr = url.split('?');
	var url_var = url_arr[1].split('&');
	var url_var_val = url_var[0].split('=');
	var video_url = unescape(url_var_val[1]);
	if(video_url.indexOf('youtube.com')!=-1) {
		if(video_url.indexOf('v=')!=-1) {
			video_url_arr = video_url.split('v=');
			video_url = 'http://www.youtube.com/v/'+video_url_arr[1];
		}
		video_url = video_url.replace('embed','v');
		$('#video-box #video-container').html('<object height="420" width="500"><param name="movie" value="'+video_url+'"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed src="'+video_url+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="420" width="500"></object>');
	} else {
		$('#video-box #video-container').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="420" id="video" align="middle">\
		<param name="allowScriptAccess" value="sameDomain" />\
		<param name="movie" value="'+url+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="'+url+'" quality="high" bgcolor="#ffffff" width="500" height="420" name="video" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\
		</object>');
	}
}
$(document).ready(function(){
	$("a[rel=popup-button]").click(function(){
		//centering with css
		centerPopup(this.className, this);
		//alert(this.className);
		//centerPopup(this.className);
		//load popup
		loadPopup();
	});
	$("a[href^=mailto]").click(function(){
		//centering with css
		var url_arr = this.href.replace('mailto:','').split('?');
		if(url_arr.length>1) {
			centerPopup('btn-mail',this);
			//load popup
			loadPopup();
			return false;
		}
	});
	$("#popupContactClose").click(function(){
		disablePopup();
	});
});
function uniqid()
{
	var newDate = new Date;
	return newDate.getTime();
}
function LoadOverlayMod(mod){	
	var param = 'act='+mod+'-overlay';
	ajax_action = SITE_URL+'login';
	$.get(
		ajax_action,
		param,
		function(data) {
			//alert(data);
			$('#overlay-content-box').append(data);
			centerPopup(current_cls,current_obj);
			loadPopup();
		}		
	);	
}
