/* IconBerry engine - Jquery plugin 
* @author:a_sidorov@bk.ru 
* http://clever-web.net
*/
(function($) {
$.lang = {
chpass_passwords_saved:"New password had saved.",
chpass_above_5_chars:"Length of the password must be above 5 chars.",
chpass_non_equal:"The passwords are not equal.",
upload_require_filling:"You should enter right upload data. Year must contain 4 digits.",
register_require_filling:"You should enter right registration data.",
register_user_exists:"This login or email already exists.",
register_please_agree:"Please, agree that you read rules.",
email_doesnot_exists:"This email was not found.",
mail_was_sended:"Letter with new password was been sended to your email.",
insert_email_address:"Please, enter valid email."
}})(jQuery);
(function($) {
$.iberry = {
	exlinkcode: null,
	defaultmodalx: 150,
	defaultmodaly: 100,
	modalx: 0,
	modaly: 0,
	needaction:null,
	stack:null,
	pvlastid:0,
	pvdata:null,
	_registration_agree_rules:0,
	_registration_error:"",
	_upload_error:"",
	_chpass_error:"",
	_pause:function(opts){
		window.setTimeout(opts.action,opts.time);
	},
	open1modal : function()
	{
 		$("#overlay").show().fadeTo('fast',0.9,'',/*Overlay opacity*/
 		$("#modal1").css({opacity:0}).show().animate({opacity:1}));
	},
	openpreview : function()
	{
		$("#previewloader").show();
		$("#pv_img").unbind("load");
		$.getJSON("index.php/site/preview/"+$.iberry.pvlastid+"/",function(data){
		//alert(data.title);
			$("#down_butt_div").bind("click",function(){
				$(this).attr("href","index.php/ximages/icon_full/"+$.iberry.pvlastid+".png/down/");
			});
			$("#pvreleasedate").text(data.releasedate);
			$("#pvstudio").text(data.studio);
			$("#pvboxtype").text(data.box_title);
			$("#pvdownloaded").text(data.downloaded);
			$("#pvtitle").text(data.title);
			$("#pvgenre").text(data.genre_title);
			$("#pvusername").text(data.login);
			
			
			if (data.rawexlink)
			{
				$(".partner_link").text(data.rawexlink);
				//$.iberry.exlinkcode = data.rawexlink;
			}else
				$(".partner_link").hide();
			
			$("#pv_img").attr("src","index.php/ximages/icon_full/"+$.iberry.pvlastid+".png");
	 		$("#overlay").show().fadeTo('fast',0.9,'',/*Overlay opacity*/
	 		$("#modal2").css({opacity:0}).show().animate({opacity:1}));
	 		
	 		
	 		$("#close_button2").bind("click",function(){
	 			$("#pv_img").addClass("pv_img_bg").attr("src","css/ui/blank.gif");	 			
	 			$.iberry.closemodals();
	 		});
	 		
	 		$("#pv_img").bind("load",function(){
	 			if ($(this).attr("src")!='css/ui/blank.gif'){
	 				$("#previewloader").hide();
	 				$("#pv_img").removeClass("pv_img_bg");
	 			}
	 		});
	 		
 		});
	},
	closemodals:function()
	{
		$("#modal1").fadeTo('fast',0,
		function (){		
			$("#modal1").hide();
			$("#modal2").hide();
			
			$("#pv_img").addClass("pv_img_bg").attr("src","css/ui/blank.gif");
			
			$("#measure").html("");
			$("#modal1cont").html("");
			
			$("#overlay").fadeOut('slow');
			$.iberry._resetmodal1size();
			
			$.iberry.putloader("#modal1cont");
			$(".link_container").hide();
		});

	},
	loadsimplecontent:function(url)
	{
		$.get(url,function(data){

			$("#measure").html(data);
			$.iberry.modalx = $("#measure").width();
			$.iberry.modaly = $("#measure").height();
			$("#measure").html("");

			$.iberry.stack = data;
			$.iberry.resize();
		});
	},
	_resetmodal1size:function()
	{ 
		
		$.iberry.modaly = $.iberry.defaultmodaly;
		$.iberry.modalx = $.iberry.defaultmodalx;
		
		$.iberry.modalx +=+50;
		
		$("#modal1").css({
		height:$.iberry.modaly,
		width:$.iberry.modalx,
		'left':$("body").width()/2-($.iberry.modalx/2),
		'top':$("body").height()/2-$.iberry.modaly/2-60
		});
		
	},
	
	resize:function()
	{ 
		if ($.iberry.modaly<=100)
			$.iberry.modaly = 100;
	
		if ($.iberry.modalx<=100)
			$.iberry.modalx = 200;
		
		$.iberry.modalx +=+50;
		
		$("#modal1").animate({
		height:$.iberry.modaly,
		width:$.iberry.modalx,
		'left':$("body").width()/2-($.iberry.modalx/2),
		'top':$("body").height()/2-$.iberry.modaly/2-60
		},"cust","",$.iberry.needaction);
		$(".ctvldr").hide();	
	},
	putloader:function(location)
	{
		$.iberry._unsetajaxlinks();

		if (location!="#listing")
		$(location).html('<div class="fhw centerdiv ctvldr"><img src="css/ui/loader.gif"/></div>').show();
		else
			$(location).html('').addClass("listpreloader").show();
			//$(location).html('').show();
			
	},
	initcentering:function()
	{
		$("#modal1").css({
		height:$.iberry.modaly,
		width:$.iberry.modalx,
		'left':$("body").width()/2-($.iberry.modalx/2),
		'top':$("body").height()/2-$.iberry.modaly/2-60
		});
		
		$("#modal2").css({
		height:450,
		width:500,
		'left':$("body").width()/2-(500/2),
		'top':$("body").height()/2-550/2
		});		
	},
	makeExit:function(){
		$.get("index.php/login/eexit/",function(data){
			window.location.href = "/";
		});
	},
	_fillMain:function(data)
	{
		if(typeof(data.matchedrows) != "undefined")
			$("#chapter_title").text("IconBerry: Search: "+data.matchedrows);
		else
			$("#chapter_title").text("IconBerry: Last uploaded");
		
		if(data.rawlistinghtml)
		$("#listing").html(data.rawlistinghtml).removeClass("listpreloader");
		
		if(data.rawpaginator)
			$("#paginator").html(data.rawpaginator);
		else
			$("#paginator").html('&nbsp;');
		
		if(data.rawlogin)
		$("#logincontainer").html(data.rawlogin);
		
		$(".preview").bind("click",function(){
			$.iberry.pvlastid = $(this).attr("pid");
			$.iberry.openpreview();
		});	
		
		$.iberry._setpaginator();
	},
	goHomepage:function(){
		$.iberry.putloader("#listing");
		$.getJSON("index.php/site/home/0/"+$.iberry.unique_number(),function(data){

			$.iberry._fillMain(data);
			
		});	
	},
	makeEnter:function()
	{
		$.iberry.putloader("#listing");
		$.getJSON("index.php/site/environment/",function(data){

			$.iberry._fillMain(data);
			
			$("#logout").bind("click",function(){$.iberry.makeExit();});
			
			$("#mypics").bind("click",function(){
					$.iberry.putloader("#listing");
	
					$.getJSON("index.php/search/foruser/0/"+$.iberry.unique_number(),function(data){
						$.iberry._fillMain(data);
					});
			});
			
			$("#upload").bind("click",function(){
				$.iberry.open1modal();
				$.iberry.needaction = 	$.iberry._loadupload;		
				$.iberry._pause({action:'$.iberry.loadsimplecontent("index.php/upload/form/'+$.iberry.unique_number()+'")',
		 		time:jQuery.fx.speeds.cust});
			});
				

			
		});
	},
	doShortSearch:function()
	{
		$.iberry.putloader("#listing");
		
		$.post("index.php/search/short/0/"+$.iberry.unique_number(),{'tags':$("#search2").val()},function(data){
		//alert(data);
			$.iberry._fillMain(data);
		},"json");
	},
	_unsetajaxlinks:function()
	{
		$(".restore").unbind("click");
		$(".register").unbind("click");
		$(".aterms").unbind("click");
		$(".arules").unbind("click");
		$(".login").unbind("click");
	},
	_fast_load_login:function()
	{
		$("#modal1cont").fadeOut("slow",function(){
			$.iberry.putloader("#modal1cont");
			$.iberry.needaction = $.iberry._loadlogin;
			$.iberry.loadsimplecontent("index.php/login/form/"+$.iberry.unique_number());
		});
	},
	_setpaginator:function()
	{
		$(".pages a").bind("click",function(){
			$.iberry.putloader("#listing");			
			$.getJSON($(this).attr("href")+"/tm"+$.iberry.unique_number(),function(data){

				$.iberry._fillMain(data);
				$.iberry._setpaginator();
			});	
						
			return false;
		});		
	},
	_setajaxlinks:function()
	{
		$(".restore").bind("click",function(){
			$("#modal1cont").fadeOut("slow",function(){
				$.iberry.putloader("#modal1cont");
				$.iberry.needaction = $.iberry._loadrestore;
				$.iberry.loadsimplecontent("index.php/login/restoreform/"+$.iberry.unique_number());
			});
		});
		$(".register").bind("click",function(){
			$("#modal1cont").fadeOut("slow",function(){
				$.iberry.putloader("#modal1cont");
				$.iberry.needaction = $.iberry._loadregister;
				$.iberry.loadsimplecontent("index.php/register/form/"+$.iberry.unique_number());
			});
		});
		
		$(".login").bind("click",function(){
			$.iberry._fast_load_login();
		});
		
		$(".aterms").bind("click",function(){
			$("#modal1cont").fadeOut("slow",function(){
				$.iberry.putloader("#modal1cont");
				$.iberry.needaction = $.iberry._loadinfopage;
				$.iberry.loadsimplecontent("index.php/info/terms/"+$.iberry.unique_number());
			});
		});
		
		$(".arules").bind("click",function(){
			$("#modal1cont").fadeOut("slow",function(){
				$.iberry.putloader("#modal1cont");
				$.iberry.needaction = $.iberry._loadinfopage;
				$.iberry.loadsimplecontent("index.php/info/rules/"+$.iberry.unique_number());
			});
		});
		
		$("#terms_d").bind("click",function(){
			$.iberry.closemodals();
		});		
		
		$.iberry._setpaginator();
		
		$.iberry._set_focus();

	},
	_is_email:function(value){
		return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
	},
	_is_notnull:function(value){
		return $.trim(value).length > 0;
	},
	_is_equal:function(value1,value2){
		return value1==value2;
	},
	_is_fullyear:function(value){
		return (parseInt($.trim(value))>=1900 && parseInt($.trim(value))<=2030 );
	},
	_is_notzero:function(value){
		return parseInt($.trim(value))>0;
	},
	_register_precheck:function()
	{
		if ($.iberry._registration_agree_rules==0){
			$.iberry._registration_error = $.lang.register_please_agree;
			return 0;
		}
		
		if ($.iberry._is_email($("#email").val())==false 
		|| $.iberry._is_notnull($("#password3").val())==false
		|| $.iberry._is_notnull($("#username3").val())==false
		){
			$.iberry._registration_error = $.lang.register_require_filling;
			return 0;
		}else
			return 1;
	},
	_forget_precheck:function()
	{
		if ($.iberry._is_email($("#username5").val())==false 
		){
			$.iberry._registration_error = $.lang.register_require_filling;
			return 0;
		}else
			return 1;
	},
	_loadregister:function(){
		$("#modal1cont").hide().html($.iberry.stack).fadeIn("slow");
		$.iberry._setajaxlinks();
		
		$("#signup").bind("click",function(){
			if ($.iberry._register_precheck()!==0){
				$(".info_text2").addClass("info_text2_preload");
				$.post("index.php/register/send/",$('#reg_form').serialize(),function(data){
					$(".info_text2").removeClass("info_text2_preload");
						
						//alert(data);
						if(data==0)
							$("#login_info3").html($.lang.register_user_exists).hide().addClass("info_wrong").fadeIn("slow");
						else
							$.iberry._fast_load_login();
						
				});
			}else
			{
				$("#login_info3").html($.iberry._registration_error).hide().addClass("info_wrong").fadeIn("slow");
			}
		});
		$(".check_image").bind("click",function(){

			if ($(this).attr("src")=='css/ui/check_on.png')
			{
				$.iberry._registration_agree_rules = 1;
				$(this).attr("src",'css/ui/check_off.png');
			}
			else
			{
				$.iberry._registration_agree_rules = 0;
				$(this).attr("src",'css/ui/check_on.png');
			}
		
		});
		
	},
	_loadlogin:function(){
		$("#modal1cont").hide().html($.iberry.stack).fadeIn("slow");
		$.iberry._setajaxlinks();
		
		$(".check_image").bind("click",function(){
			if ($(this).attr("src")=='css/ui/check_on.png'){
					$("#rememberme").val(1);
					$(this).attr("src",'css/ui/check_off.png');
					}
			else{
					$("#rememberme").val(0);
					$(this).attr("src",'css/ui/check_on.png');
				}
		});
		
		
		
		$("#enter").bind("click",function(){
			$(".info_text2").addClass("info_text2_preload");
			$.post("index.php/login/enter/"+$.iberry.unique_number(),$('#reg_form').serialize(),function(data){
				$(".info_text2").removeClass("info_text2_preload");
				if(data!=0){
					//alert(data);
					$.iberry.closemodals();
					$.iberry.makeEnter();
				}	
				else
				{
					$("#info_welcome").fadeOut("fast",function(){
						$("#info_wrong").fadeIn("slow");
					});
				}	
			});
		});
		
	},
	_loadrestore:function(){
		$("#modal1cont").hide().html($.iberry.stack).fadeIn("slow");
		$.iberry._setajaxlinks();
		
		$("#sendrestore").bind("click",function(){
			
			if ($.iberry._forget_precheck()!==0){
				$(".loader_div").show();
				$(".info_text2").addClass("info_text2_preload");
				$.post("index.php/register/sendforget/"+$.iberry.unique_number(),$('#restore_form2').serialize(),function(data){
					$(".info_text2").removeClass("info_text2_preload");
						
						//alert(data);
						if(data==0)
							$("#login_info6").html($.lang.email_doesnot_exists).hide().removeClass("info_ok").addClass("info_wrong").fadeIn("slow");
						else
							$("#login_info6").html($.lang.mail_was_sended).hide().removeClass("info_wrong").addClass("info_ok").fadeIn("slow");
						
				});
			}else
			{
				$("#login_info6").html($.lang.insert_email_address).hide().removeClass("info_ok").addClass("info_wrong").fadeIn("slow");
			}
		});
	},
	_changepass_precheck:function()
	{
		
		
		
		if ($("#pass1").val().length>4){
			if ($.iberry._is_equal($("#pass1").val(),$("#pass2").val())==true)
				return 1;
			else
				$.iberry._chpass_error = $.lang.chpass_non_equal;
		}
		else
			$.iberry._chpass_error = $.lang.chpass_above_5_chars;
			
			return 0;
		
	
	},
	_loadchangepass:function(){
		$("#modal1cont").hide().html($.iberry.stack).fadeIn("slow");
		$.iberry._setajaxlinks();
		$("#passchbut").bind("click",function(){
			if ($.iberry._changepass_precheck()){
				$(".info_text2").addClass("info_text2_preload");
				$.post("index.php/changepass/doit/"+$.iberry.unique_number(),$("#changepass").serialize(),function(){
					$(".info_text2").removeClass("info_text2_preload");
					$(".chpass_info").html($.lang.chpass_passwords_saved).hide().removeClass("info_wrong").addClass("info_ok").fadeIn("slow");
					
				});
						
			}else
			{
				$(".chpass_info").html($.iberry._chpass_error).hide().removeClass("info_ok").addClass("info_wrong").fadeIn("slow");
				
			}
			
			
		});
		
	},
	_loadinfopage:function(){
		$("#modal1cont").hide().html($.iberry.stack).fadeIn("slow");
		$.iberry._setajaxlinks();
	},
	_upload_precheck:function()
	{
	
		if ($.iberry._is_notnull($("#title_dvd").val())==false 
		|| $.iberry._is_notzero($("#genre").val())==false
		|| $.iberry._is_notzero($("#boxtype").val())==false
		|| $.iberry._is_notnull($("#studio").val())==false
		|| $.iberry._is_fullyear($("#year").val())==false
		//|| $.iberry._is_notnull($("#userfile").val())==false
		){
			$.iberry._upload_error = $.lang.upload_require_filling;
			return 0;
		}else
			return 1;
	
	},
	_loadupload:function(){
		$("#modal1cont").hide().html($.iberry.stack).fadeIn("slow");
		$("#uplbut").bind("click",function(){
			
			$(".upload_info").html("");
			if ($.iberry._upload_precheck())
			{
			$(".info_text2").addClass("info_text2_preload");
			//trird party plugin
				$.ajaxFileUpload
				(
					{
						url:'index.php/upload/doit/',
						secureuri:false,
						//fileElementId:'fileToUpload',
						fileElementId:'upload_form',
						dataType: 'json',
						success: function (data, status)
						{
							$(".info_text2").removeClass("info_text2_preload");
							if(typeof(data.error) != 'undefined')
							{
								if(data.error != '')
								{
									//alert("error \n"+data.error);
									$(".upload_info").text(data.error).hide().removeClass("info_ok").addClass("info_wrong").fadeIn("slow");
									
									
								}else
								{
									//alert(data.msg);
									$("#uploaddata").hide();
									$(".upload_info").text(data.msg).hide().removeClass("info_wrong").addClass("info_ok").fadeIn("slow");
								}
							}
						},
						error: function (data, status, e)
						{
							//alert(data.toString()+"\n\n"+status);
							//alert(e);
						}
					}
				);
			}else
			{
				$(".upload_info").html($.iberry._upload_error).hide().removeClass("info_ok").addClass("info_wrong").fadeIn("slow");
			}
		});
		$.iberry._setajaxlinks();
	},
	_loadextsearch:function(){
		$("#modal1cont").hide().html($.iberry.stack).fadeIn("slow");
		$.iberry._setajaxlinks();
		
		$("#extsearch").bind("click",function(){
			$(".info_text2").addClass("info_text2_preload");
			$.post("index.php/search/extsearch/0/",$("#extsearchform").serialize(),function(data){
				$(".info_text2").removeClass("info_text2_preload");
				$.iberry.closemodals();
				$.iberry._fillMain(data);
			},"json");
		});
	},
	_set_focus:function()
	{
		$("#modal1").find("input:first").focus();
	},
	unique_number: function ()
	{
		return (new Date()).getTime();
	}
}})(jQuery);
/*
Maintain scripts
*/
$(document).ready(function(){
	
	jQuery.fx.speeds.fast = 300;
	jQuery.fx.speeds.slow = 700;
	jQuery.fx.speeds.cust = 300;
 
	$.iberry._setpaginator();
	$.iberry._resetmodal1size();
	
	$.iberry.initcentering();
	$.iberry.putloader("#modal1cont");
	$(window).resize(function(){ $.iberry.initcentering();});
	
	if(needchangepass==1)
	{
		$.iberry.open1modal();
		$.iberry.needaction = 	$.iberry._loadchangepass;		
		$.iberry._pause({action:'$.iberry.loadsimplecontent("index.php/changepass/form/'+$.iberry.unique_number()+'")',
 		time:jQuery.fx.speeds.cust});
	}
	
	if(needautologin==1){
		$.iberry.makeEnter();
	}else
	{
		$('#register').click(function(){
			$.iberry.open1modal();
			$.iberry.needaction = 	$.iberry._loadregister;		
			$.iberry._pause({action:'$.iberry.loadsimplecontent("index.php/register/form/'+$.iberry.unique_number()+'")',
	 		time:jQuery.fx.speeds.cust});
		});
		
		$('#login').click(function(){
			$.iberry.open1modal();
			$.iberry.needaction = 	$.iberry._loadlogin;		
			$.iberry._pause({action:'$.iberry.loadsimplecontent("index.php/login/form/'+$.iberry.unique_number()+'")',
	 		time:jQuery.fx.speeds.cust});
		});
		
		$('.nologin').click(function(){
			$.iberry.open1modal();
			$.iberry.needaction = 	$.iberry._loadinfopage;		
			$.iberry._pause({action:'$.iberry.loadsimplecontent("index.php/login/nologin/'+$.iberry.unique_number()+'")',
	 		time:jQuery.fx.speeds.cust});
		});
		
	}
	$("#exptended_search").bind("click",function(){
			$.iberry.open1modal();
			$.iberry.needaction = 	$.iberry._loadextsearch;		
			$.iberry._pause({action:'$.iberry.loadsimplecontent("index.php/search/extform/'+$.iberry.unique_number()+'")',
	 		time:jQuery.fx.speeds.cust});
	});
	
	$("#search2").click(function(){
		if ($('#search2').val()=="Search...")
			$('#search2').val("");
	});
			
	$("#shortsearch").click(function(){
		if ($('#search2').val()=="Search...")
			$('#search2').val("");
	
		if ($('#search2').val().length > 1)
			$.iberry.doShortSearch();
	});
	
	
	$('#search2').keypress(function(event) {
		if (event.keyCode == 13 && $('#search2').val().length > 1) 
			$.iberry.doShortSearch();
	});

		
	$("#active_logo").click(function(){
		$.iberry.goHomepage();	
	});
	
	$("#dlink_butt_div").click(function(){
		$(".link_container").text($.iberry.exlinkcode).show();
	});
	
/*	$("#overlay").click(function(){
		$.iberry.closemodals();
	});
*/	
	$("#close_button").click(function(){
		$.iberry.closemodals();
	});
	
});
