﻿//-------------------popup-------------------------
//0 disabled; 1 enabled;
var popupStatus = 0;
function loadPopup(background,popup){
	//loads popup only if it is disabled
	if(popupStatus===0){
		$(background).css({
			"opacity": "0.7"
		});
		$(background).fadeIn("fast");
		$(popup).fadeIn("fast");
		popupStatus = 1;
	}
}
function disablePopup(background,popup){
	if(popupStatus==1){
		$(background).fadeOut("fast");
		$(popup).fadeOut("fast");
		popupStatus = 0;
	}
}
function centerPopup(background,popup){
	var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	//var windowWidth = $("#left_menu").width()+$("#main_сontent").width();
	var windowHeight = $("#main").height();
	var popupHeight = $(popup).height();
	var popupWidth = $(popup).width();
	//centering
	$(popup).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	/*$(background).css({
		"height": windowHeight
	});*/
}
//closing popup
function closePopup(background,popup,close){
	//click the x event!
	$(close).click(function(){
		disablePopup(background,popup);
		if((popup=='.login_popup' || popup=='.registration_popup') && getCookie('user_id')===null)
		{
			if(window.location.href.split("/")[3]=='blog' || window.location.href.split("/")[3]=='activities')
			{
				window.location='http://kent.com.ua/';
			}
			else if(window.location.href.split("/")[3]=='news' && window.location.href.split("/")[4]=='view')
			{
				window.location='http://kent.com.ua/news/';
			}
			else if(window.location.href.split("/")[3]=='games' && navigator.appName=='Microsoft Internet Explorer')
			{
				window.location='http://kent.com.ua/';
			}
		}
	});
	//click out event!
	$(background).click(function(){
		disablePopup(background,popup);
		if((popup=='.login_popup' || popup=='.registration_popup') && getCookie('user_id')===null)
		{
			if(window.location.href.split("/")[3]=='blog' || window.location.href.split("/")[3]=='activities')
			{
				window.location='http://kent.com.ua/';
			}
			else if(window.location.href.split("/")[3]=='news' && window.location.href.split("/")[4]=='view')
			{
				window.location='http://kent.com.ua/news/';
			}
			else if(window.location.href.split("/")[3]=='games' && navigator.appName=='Microsoft Internet Explorer')
			{
				window.location='http://kent.com.ua/';
			}
		}
	});
	/*press escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup(background,popup);
		}
	});
	*/
}
//-------------------popup-------------------------


function syncMail(val)
{
	var phone = $("input[name=phone]").val();
	$.post('/ajax/user_sync.php',{pnumber: 1, email:val, phone: phone},function(data){
		//alert('dd');
	});
	return false;
}



function unlock(){
	$(this).removeClass().addClass("unlocked");
	temparray=$(this).attr('id').split("_");
	id="#"+temparray[0]+"_check";
	$(id).attr('checked', true);
}
	
function lock(){
	$(this).removeClass().addClass("locked");
	temparray=$(this).attr('id').split("_");
	id="#"+temparray[0]+"_check";
	$(id).attr('checked', false);
}

//-------------------user_bar-------------------------

   function getCookie(szName)
		{
			var szValue = null;
			if(document.cookie)	   //only if exists
			{
				var arr = document.cookie.split((escape(szName) + '=')); 
				if(2 <= arr.length)
				{
					var arr2 = arr[1].split(';');
					szValue  = unescape(arr2[0]);
				}
			}
			return szValue;
		}
		
function show_panel()
{
	$("#panel_right").hide();
	$("#user_panel").show("slide",{},"slow");
	//$("#load_panel").unbind('click');
	//$("#load_panel").click(hide_panel);
	document.cookie = 'show_panel=true; path=/';
}

function hide_panel()
{
  	//$("#load_panel").unbind('click');
	//$("#load_panel").click(show_panel);
	$("#user_panel").hide("slide",{},"slow");
	$("#panel_right").fadeIn(4000);
	document.cookie = 'show_panel=false; path=/';
}

function loadCompleate()
{
	if (document.location.href.split('/')[3]!=='')
	{		
		$("#flash_pr").css("opacity","1"); $("#flash_pr").css("filter"," ");
	}
	$("#flash_pr").css({
						height:'350px',
						width:'291px',
						top:'228px'							
						
						});

	
	$("#flash_pr").addClass('flash_pr_1');
	$("#status_bar").show();
	
	if (document.location.href.split('/')[3]=='news' || document.location.href.split('/')[3]=='blog')
	{
	$.get("/ajax/flash_overlay.php",function(data){
				$("body").append(data);
				$("body").oneTime(1000, "hide", function() {
															$("#main_сontent").fadeIn(500);
															$('#news').jScrollPane({
																showArrows:false,
																scrollbarMargin:0, 
																scrollbarWidth:3 
															});
															if ($(".jScrollPaneTrack").length!==0)
															{
																$("#main_сontent").addClass("news_scroll");
															}
															$("#flash_overlay").fadeOut(500);
															});
				});
	
	}
	
}


//begin changed

var timers_set=0;

var hints;

var appear_long;

var bubble_size;

var animation_played;

var appear_played=0;

var counter=0;

var default_play_times=3;

function get_animation_list()
{
	var data_hints=$("#hints_div").html();
			if (data_hints!='null')
				hints=$.parseJSON(data_hints);
			else
				hints='null';
}

function get_appear()
{
	var data_appear=$("#appear_div").html();
	var app=$.parseJSON(data_appear);
	appear_long=app.appear;
}

function animationPlayed(name)
{
	var timeout=2000;
	get_animation_list();
	if (timers_set==0 && hints!='null')
	{
		
		$(document).oneTime(3000, "animation_3", function() {
			var i=0;
			for(i=0; i<=hints.length-2; i++)
			{
				var indexes=new Array();
				timeout=timeout+parseInt(hints[i].timeout);
				if(i>0)
					timeout=timeout+7000;

				$(document).oneTime(timeout, i, function() {
											var hover_anim=animation_played;
											var movie = window.document.flash_pr_ob;
											var animation_type;
											$(document).everyTime(2, "show_anim_planned_"+i, function() {
												if (hover_anim!=animation_played)
												{
													if (hints[counter].text.length>85)
														bubble_size="big";	
													else
														bubble_size="small";	
													if (hints[counter].type=='alert')
														animation_type="bbl_form_note_slow";
													/*
													else
														animation_type="bbl_form_question_slow";*/
													movie.startAnimation(animation_type, hints[counter].text, bubble_size, '', '');
													$(document).stopTime("show_anim_planned_"+i);
													counter++;
												}
											});
										});
											
			}
			timers_set=1;
		});
	}

	timeout=0;
	if (name=="bbl_form_note_slow")
		timeout=7000;
    else if(name=="bbl_form_question_slow")
    {
        timeout=14000;
    }
    else if(name=="bbl_form_disappear_1" || name=="bbl_form_disappear_2")
		return;
	else
		timeout=0;
	


	$(document).oneTime(timeout, "animation_1", function() 
		{
			var movie = window.document.flash_pr_ob;

			if (name=="bbl_form_note_slow")
			{
				movie.startAnimation("bbl_form_note_reverse","",bubble_size, '', '');
				default_play_times=0;
				return;
			}
			/*
			if (name=="bbl_form_question_slow")
			{
				movie.startAnimation("bbl_form_question_slow_reverse","",bubble_size, '', '');
				default_play_times=0;
				return;
			}
			*/
			
			if (document.location.href.split("/")[4]=='play')
				{
					try
					  {
						//var movie = window.document.flash_pr_ob;
						var rand=Math.random();
						var animation;
						if (rand<0.5)
							animation="bbl_form_disappear_1";
						if (rand>0.5)
						animation="bbl_form_disappear_2";
						movie.startAnimation(animation,"","small", '', '');
						$(document).stopTime("show_anim_hide");
						$("body").oneTime(1500,"detach_anim",function(){
							$("#flash_pr").remove();
						});
						
					  }
				  catch(err)
					{
					}
				}
			else	
			{
						//var movie = window.document.flash_pr_ob;
						var animation;
						var rand=Math.random();
						//alert(appear_played);
						if (appear_played===0)
						{
							get_appear();			
							if (appear_long=="long")
							{
								if (rand<0.5)
								animation="bbl_form_appearence_1";
								if (rand>0.5)
								animation="bbl_form_appearence_2";
							}
							if (appear_long=='short')
							{
								if (rand<0.33)
								animation="bbl_form_appearance_short_1";
								if (rand>0.33 && rand<0.66)
								animation="bbl_form_appearance_short_2";
								if (rand>0.66)
								animation="bbl_form_appearance_short_3";
							}
							appear_played=1;
							var special_delay=0;
							if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) special_delay=100;
							$(document).oneTime(special_delay, function()
							{
								movie.startAnimation(animation,"","big", '', '');
							});
							default_play_times=0;
							animation_played=name+Math.random();
							return;
						}
						
						else
						{
							if (default_play_times!=0)
							{
								$(document).oneTime(timeout, "animation_5", function() 
								{
									movie.startAnimation("bbl_form_source", "", "big", '', '');
									default_play_times=default_play_times-1;
									return;
								});
							}
							animation=name;
							while(animation==name)
							{
								if (rand<0.33)
								animation="bbl_form_active_1";
								if (rand>0.33 && rand<0.66)
								animation="bbl_form_active_2";
								if (rand>0.66)
								animation="bbl_form_active_3";
								rand=Math.random();
							}
							movie.startAnimation(animation,"","big", '', '');
							default_play_times=0;
							animation_played=name+Math.random();
							return;						
						}
			}
		}
	);
}

function LoadAuth(){
	$("#load_login").trigger("click");
}

function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}
function alarmbanned(){
	load_ok_popup('Ваш акаунт було заблоковано у зв’язку з недотриманням умов сайту.','','','',0.7);
}

function userAnswer(answer){
    
    var movie = window.document.flash_pr_ob;
    movie.startAnimation('bbl_form_question_slow_reverse','', 'huge', '', '');

    $.post('/questions/questions.php',{heyho: 'idontcare', answer: answer},function(data){
        return false;
    });
}

function getRandomInt(min, max)
{
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

//----------------actions-----------------------------
$(document).ready(function(){
		
	var winodwheight=getWindowHeight();
	if (screen.height>800 || winodwheight>800) $("#site_footer").css('bottom','0px');
	
	$(window).bind('resize',function(){
	var winodwheight=getWindowHeight();
	if (screen.height<800 || winodwheight<800)
		$("#site_footer").css('position','static');
	else
		$("#site_footer").css('position','absolute');
	});
	
	document.cookie = 'show_panel=false; path=/';
	$(".locked").live('click',unlock);
	$(".unlocked").live('click',lock);
	
	$(".custom").click(function(){
	$(this).prev().html($(this).val());
	});

	//--------------bans------------------
	
	$("#add_record_button_banned").click(function(){
		load_ok_popup('Ваш акаунт було заблоковано у зв’язку з недотриманням умов сайту.','','','',0.7);
	});
	
	$(".comm_button_banned").click(function(){
		load_ok_popup('Ваш акаунт було заблоковано у зв’язку з недотриманням умов сайту.','','','',0.7);
	});
	
	$("#add_record_button_profile_banned").click(function(){
		load_ok_popup('Ваш акаунт було заблоковано у зв’язку з недотриманням умов сайту.','','','',0.7);
	});
	
	$("#add_blog_button_profile_banned").click(function(){
		load_ok_popup('Ваш акаунт було заблоковано у зв’язку з недотриманням умов сайту.','','','',0.7);
	});	
	
	//---------------feedback--------------
	$("#send_feedback").click(function(){
	
	  var name = $("input[name=name]").val();
	  var mail = $("input[name=mail]").val();
	  var subject = $("input[name=subject]").val();
	  var msg = $("textarea[name=msg]").val();
		
	  var dataString = 'name='+name+'&mail='+mail+'&subject='+subject+'&msg='+msg;
		//alert (dataString);
	  $.ajax({
		type: "POST",
		url: "/ajax/feedback.php",
		data: dataString,
		success: function(msg){
			var output=msg.replace(/\|/g, "\n");
			if (output=='done')
			{
				load_ok_popup('Дякуємо!<br />Ваше повідомлення було надіслане.','','','',0.7);
				disablePopup(".feedback_background",".feedback_popup");
			}
			else load_ok_popup(output,'','','',0.7);
			
	  	}
	  });
	  return false;
	});
	
	$("#load_feedback").click(function(){
		$('#feedbackContact').addClass('feedback_popup');
		$('#feedbackBackground').addClass('feedback_background');
		centerPopup(".feedback_background",".feedback_popup");
		loadPopup(".feedback_background",".feedback_popup");
	});
	
	closePopup(".feedback_background",".feedback_popup",".feedbackContactClose");
	
	//---------------feedback--------------
	
	//---------------conditions--------------
	
	$("#load_conditions").click(function(){
		$('#conditionsContact').addClass('conditions_popup');
		$('#conditionsBackground').addClass('conditions_background');
		centerPopup(".conditions_background",".conditions_popup");
		loadPopup(".conditions_background",".conditions_popup");
	});
	
	closePopup(".conditions_background",".conditions_popup",".conditionsContactClose");
	
	//---------------conditions--------------
	
	
	//-----------------------------------------registration--------------------------------------
	$("input[name=rem_lock_inp]").attr('checked',false);

	$("#remember_lock").click(function(){
										$("#rem_lock_inp").trigger("click");
										});
	
	$("input[name=iagree]").attr('checked',true);
	
										
	$("#load_registration").click(function(){
		$('#registrationContact').addClass('registration_popup');
		$('#registrationBackground').addClass('registration_background');
		centerPopup(".registration_background",".registration_popup");
		loadPopup(".registration_background",".registration_popup");
	});
	
	closePopup(".registration_background",".registration_popup",".registrationContactClose");
	
	//submit form by ajax
	$("#send_registration").click(function(){
	  var login = $("input[name=login]").val();
	  var name = $("input[name=name_user]").val();
	  var email = $("input[name=email]").val();
	  var phone = $("input[name=phone]").val();
	  var password = $("input[name=password]").val();
	  var password_again = $("input[name=password_again]").val();
	  var iagree=$("input[name=iagree]").attr('checked');
		
	  var dataString = 'login='+login+'&name='+name+'&email='+email+'&phone='+phone+'&password='+password+'&password_again='+password_again+'&iagree='+iagree;
	  //alert (dataString);
	  $.ajax({
		type: "POST",
		url: "/ajax/registration.php",
		data: dataString,
		success: function(msg){
			//var output=msg.replace("||", "<br />");
			var output=msg;
			if (output=='done')
			{
				disablePopup(".registration_background",".registration_popup");
				load_ok_popup('Дякуємо за реєстрацію!','','','',0.7);
					
				//login
				  var dataString = 'login='+login+'&password='+password;
				  $.ajax({
					type: "POST",
					url: "/ajax/login.php",
					data: dataString,
					success: function(msg){
						//var output=msg.replace(/\|/g, "\n");
						var output=msg.replace('||', "<br />").split('_');
						if (output[0]=='done')
						{
							if(output[1]=='old')
							{
								window.location=document.location.href;
							}
							else
							{
								window.location='http://kent.com.ua/profile/';
							}
						}
						else load_ok_popup(output[0],'body',250,"center",0.5);
					}
				  });
				  return false;				
			}
			else load_ok_popup(output,'','','',0.7);
			
	  	}
				
	  });
	  return false;
	});
	//-----------------------------------------registration--------------------------------------
	
	//-----------------------------------------login--------------------------------------
	
	function load_login_window()
	{
		$('#loginContact').addClass('login_popup');
		$('#loginBackground').addClass('login_background');
		centerPopup(".login_background",".login_popup");
		loadPopup(".login_background",".login_popup");
		closePopup(".login_background",".login_popup",".loginContactClose");
	}
	
	$("#load_login").click(load_login_window);
	$("#add_record_button_show_login").click(load_login_window);	

	//submit form by ajax
	$("#send_login").click(function(){
	  var login = $("input[name=l_login]").val();
	  var password = $("input[name=l_password]").val();
	  var remember = $("input[name=rem_lock_inp]").attr('checked');
		
	  var dataString = 'login='+login+'&password='+password+'&remember='+remember;
	  
	  $.ajax({
		type: "POST",
		url: "/ajax/login.php",
		data: dataString,
		success: function(msg){
			//var output=msg.replace(/\|/g, "\n");
			var output=msg.replace('||', "<br />").split('_');
			if (output[0]=='done')
			{
				if(output[1]=='old')
				{
					window.location=document.location.href;
				}
				else
				{
					window.location='http://kent.com.ua/profile/';
				}
			}
			else load_ok_popup(output[0],'body',250,"center",0.5);
	  	}
	  });
	  return false;
	});
	
	$("#close_me_and_open_regi").click(function(){
		disablePopup(".login_background",".login_popup");
		$('#registrationContact').addClass('registration_popup');
		$('#registrationBackground').addClass('registration_background');
		centerPopup(".registration_background",".registration_popup");
		loadPopup(".registration_background",".registration_popup");
	});
	
	closePopup(".registration_background",".registration_popup",".registrationContactClose");

	//--------------------------------------pass restore---------------------------------------
	$("#pass_recover_load").click(function(){
		disablePopup(".login_background",".login_popup");
		$('#pass_recoverContact').addClass('pass_recover_popup');
		$('#pass_recoverBackground').addClass('registration_background');
		centerPopup(".pass_recover_background",".pass_recover_popup");
		loadPopup(".pass_recover_background",".pass_recover_popup");
	});
	
	closePopup(".pass_recover_background",".pass_recover_popup",".registrationContactClose");
	
	$("#recover_send").click(function(){
	  var email = $("input[name=email_recover]").val();	
	  var dataString = 'email='+email;
	  
	  $.ajax({
		type: "POST",
		url: "/ajax/pass_recovery.php",
		data: dataString,
		success: function(msg){
			//var output=msg.replace(/\|/g, "\n");
			var output=msg.replace('||', "<br />").split('_');
			if (output=='done')
			{
				load_ok_popup('Дякуємо. Новий пароль надіслано<br/>на Вашу поштову скриньку.','','','',0.55);
			}
			else if(output=='error1')
			{
				load_ok_popup('Вкажіть e-mail у правильному форматі.','','','',0.5);
			}
			else
			{
				load_ok_popup('Невірний e-mail.','','','',0.5);
			}
	  	}
	  });
	  return false;
	});	

	//-----------------------------------------login--------------------------------------
	
	$("#load_auth").click(function(){
		$('#authContact').addClass('auth_popup');
		$('#authBackground').addClass('auth_background');
		centerPopup(".auth_background",".auth_popup");
		loadPopup(".auth_background",".auth_popup");
	});
	
	closePopup(".auth_background",".auth_popup",".authContactClose");
	//-----------------------------------------user_bar--------------------------------------
	/*var show_panel_cookie = getCookie("show_panel");
	if (show_panel_cookie)
	{
		switch(show_panel_cookie)
		{
			case 'true':
			  $("#load_panel").click(hide_panel);
			  break;
			case 'false':
			  $("#load_panel").click(show_panel);
			  break;
		}
	}
	else
	{
		$("#load_panel").click(hide_panel);
	}*/
	show_bar=0;
	
	$('body').bind('mousemove',function(mEvent){
		
		if (mEvent.srcElement)
		{
			var element = mEvent.srcElement;
		  }
		// Netscape and Firefox
		else if (mEvent.target)
		{ 
			var element = mEvent.target;
		}
		if (element.id=='load_panel')
		{
			show_panel();
			element.id='load_panel_1';
			show_bar=1;
		}
		if (show_bar==1)
		{
			if (element.className!='st_bar' && $("#display_or_no").attr("class")=="nope")
			{
				hide_panel();
				document.getElementById('load_panel_1').id='load_panel';
				show_bar=0;
			}
		}
	});

//questions
try
{
    var hover_anim=animation_played;
    $(document).everyTime(2, 'show_question', function() {
        if (hover_anim!=animation_played)
		{
        $.post('/questions/questions.php',{heyho: 'letsgo'},function(data){
            if(data!='nope')
            {
                var question=$.parseJSON(data);
                if(question != null)
                {
                    var q_delay = getRandomInt(10000, 120000);
                    $(document).oneTime(q_delay, function()
                    {
                        var movie = window.document.flash_pr_ob;
                        movie.startAnimation("bbl_form_question_slow",question.q,"huge", question.t, question.a);
                    });
                }
            }
        });
        $(document).stopTime("show_question");
        }
    });
}
catch(err){}

$(".flash_pr_1").live('mouseover',function(){
	var movie = window.document.flash_pr_ob;
	var hover_anim=animation_played;
	$(document).everyTime(2, 'show_anim1', function() {
		if (hover_anim!=animation_played)
		{
			movie.startAnimation('bbl_form_active_4',"",'small', '', '');
			$(document).stopTime("show_anim1");
		}
	}); 
});

$(".flash_pr_1").live('mouseup',function(){
	var movie = window.document.flash_pr_ob;
	var hover_anim=animation_played;
	$(document).everyTime(2, 'show_anim2', function() {
		if (hover_anim!=animation_played)
		{
			movie.startAnimation('bbl_form_2',"",'small', '', '');
			$(document).stopTime("show_anim2");
		}
	});
});

});
