﻿var present_category_button=22;
function Present(){
	this.intervalRotation;
	this.intervalFrRotation;
	this.rotationCurrent=0;
	this.rotationFRCurrent=0;
	this.presentdropbox;
	this.statusSelected=0;
	this.fromId='';
	this.cntMyPresent;
	this.cntFrPresent;
	this.toptip=new Array();
	this.cat_ids;
	this.cat_names;
	this.currentCat;
	this.initEvents=function(){
		/*init event*/
		$('#present_btn_start').click(function(){present.showPresentsWindow('')});
		$('#present_btn_addmsg').click(function(){present.addmsg()});
		$('#present_btn_noaddmsg').click(function(){present.noaddmsg()});
		/*init status*/
		var arrstatus=new Array();
		for(var j=0;j<3;j++) arrstatus[j]=$('#present_status_msg'+j).attr('value');
		this.presentdropbox=new dropBox('present_select_box',arrstatus,'present.presentdropbox','present.select_status');
		/*init rotation*/
		this.cntMyPresent=$('#present_my_cnt').attr('value');
		if(this.cntMyPresent>1)this.rotationMyPresents();
		if ($('#present_fr_cnt').attr('value')){
			this.cntFrPresent=$('#present_fr_cnt').attr('value');
			if(this.cntFrPresent>1)this.rotationFrPresents();
		}
		/*init popup tip*/
	
		
	};
	
	this.rotationMyPresents=function(){
		this.intervalRotation=setInterval("present.rotationNext()",5000);
	};
	this.rotationFrPresents=function(){
		this.intervalFrRotation=setInterval("present.rotationFrNext()",5000);
	};
	this.rotationNext=function(){
		this.rotationCurrent++;
		if (this.rotationCurrent==this.cntMyPresent) this.rotationCurrent=0;
		for (var i=0;i<this.cntMyPresent;i++)
			if (i==this.rotationCurrent) {
				$('#present_my_'+i).css('display','block');
				$('#present_my_wall_'+i).css('display','block');
				
			}else {
				$('#present_my_'+i).css('display','none');
				$('#present_my_wall_'+i).css('display','none');
			}
	}
	this.rotationFrNext=function(){
		this.rotationFRCurrent++;
		if (this.rotationFRCurrent==this.cntFrPresent) this.rotationFRCurrent=0;
		for (var i=0;i<this.cntFrPresent;i++)
			if (i==this.rotationFRCurrent) $('#present_fr_'+i).css('display','block');
			else $('#present_fr_'+i).css('display','none');
	}
	this.select_status=function(i){
		this.statusSelected=i;
		for(var j=0;j<3;j++)
		if(j==i) 	$('#status_toptip'+j).css('display','block'); 
		else 		$('#status_toptip'+j).css('display','none');
		
	};
	
	this.showTab=function(n){
		
		for(var i=0;i<3;i++)
		if(i==n){
			$('#present_tab_'+i).css('display','block');
			$('#present_tab'+i).attr('class','active');
		}else {
			$('#present_tab_'+i).css('display','none');
			$('#present_tab'+i).attr('class','');
		}
	};
	this.addmsg=function(){
		$('#present_btn_noaddmsg').css('display','block');
		$('#present_btn_addmsg').css('display','none');
		$('#present_table_msg').css('display','block');
		$('#present_table_msg_').css('display','block');
		
	};
	this.noaddmsg=function(){
		$('#present_msg').attr('value','');
		$('#present_table_msg').css('display','none');
		$('#present_table_msg_').css('display','none');
		$('#present_btn_addmsg').css('display','block');
		$('#present_btn_noaddmsg').css('display','none');
	};
	this.showPresentsWindow=function(from_id){
		this.fromId=from_id;
		var browserWindow = $(window);
		var browserWidth = browserWindow.width();
		var browserHeight = browserWindow.height();
		$.ajax({
   			url: '/present/getWindowPresent',
		   	dataType : "text",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
				$("#present_all_window_content").html(data);
				$("#present_all_window").dialog({
		   			modal: true,
					height: browserHeight-50,
					resizable: false,
					title:$("#present_title").attr('value'),
		   			width:660,
		   			});
				this.cat_names=$('#presents_cat_names').attr('value');
				this.cat_ids=$('#presents_cat_ids').attr('value');
				this.addDropCategory();
				first=parseInt($('#present_div_cat_first').attr('value'));
				this.showCategoryDiv(first);
		   	}
		   	});	
	};
	this.showCategoryDivi=function(i){
		this.showCategoryDiv(this.cat_ids[i]);
		$('#present_cat_popup').attr('class','presents_category presents_category_selected');
	};
	this.showCategoryDiv=function(id){
		present_category_button.hide();
		$('#present_cat_popup').attr('class','presents_category');
		$('#present_div_cat_'+this.currentCat).hide();
		$('#present_menu_cat_'+this.currentCat).attr('class','presents_category');
		$('#present_div_cat_'+id).show();
		$('#present_menu_cat_'+id).attr('class','presents_category presents_category_selected');
		this.currentCat=id;
	};

	this.addDropCategory=function(){
		this.cat_ids=this.cat_ids.split(',');
		this.cat_names=this.cat_names.split(',');
		present_category_button=new dropBox('present_cat_popup',this.cat_names,'present_category_button','present.showCategoryDivi');
		$('#present_cat_popup_drop_el_0').hide();
	};
	this.deletePresent=function(id){
		$.ajax({
   			url: '/present/deletePresent',
		   	dataType : "text",
		   	data:'id='+id,
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
				$('#present_msg_id_'+data).css('display','none');
				$('#present_msg_id_deleted_'+data).css('display','block');
		   	}
		});	
	};
	this.undeletePresent=function(id){
		$.ajax({
   			url: '/present/undeletePresent',
		   	dataType : "text",
		   	data:'id='+id,
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
				$('#present_msg_id_'+data).css('display','block');
				$('#present_msg_id_deleted_'+data).css('display','none');
		   	}
		});	
	};
	this.showPresentsWindowOne=function(id,url,point){
		var browserWindow = $(window);
		var browserWidth = browserWindow.width();
		var browserHeight = browserWindow.height();
		$("#present_img_p").html(point);
		var name_btn=$("#present_btn_send").attr('value');
		$("#present_img_url").attr('src','/presents/'+url);
		$("#present_one_window").dialog({
		   	modal: true,
			resizable: false,
			height: 420,
			buttons: [{text: name_btn,click: function() { present.send(id);}}],
			title:$("#present_title").attr('value'),
		   	width:660,
		});
	};
	
	this.addslashes=function (str) {
		str=str.replace(/\\/g,'\\\\');
		str=str.replace(/\'/g,'\\\'');
		str=str.replace(/\"/g,'\\"');
		str=str.replace(/\0/g,'\\0');
		str=str.replace(/\n/g,'<br>');
		return str;
	};
	this.toPay=function(){
		 window.location = "/payment";
	}
	this.send=function(pid){
		var from='';
		if(this.fromId=='')from= $('#aOwnerUserId').attr('value');
		else from=this.fromId;
		$("#present_all_window").dialog( "close" );
		$("#present_one_window").dialog( "close" );
		var msg=this.addslashes($('#present_msg').attr('value'));
		$.ajax({
   			url: '/present/sendPresent',
		   	dataType : "text",
		   	data:'msg='+msg+'&pid='+pid+'&uid='+from+'&status='+this.statusSelected,
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
				if (data!='empty'){
					if(data=="errmoney"){
						$("#present_one_window_err").dialog({
							modal: true,
							resizable: false,
							height: 150,
							title:$("#present_title").attr('value'),
							buttons: [{text: 'Пополнить',click: function() { present.toPay();}}],
							width:400,
						});
					}else{
						$("#present_one_window_ok").dialog({
							modal: true,
							resizable: false,
							height: 150,
							title:$("#present_title").attr('value'),
							width:400,
						});
					}
				}
			}
		});
	};
	
	this.select_present=function(id){
		$('#present_a_'+id).css('background-color','#d0ebf7');
		$('#present_point_'+id).css('display','block');

	}
	this.unselect_present=function(id){
		$('#present_a_'+id).css('background','none');
		$('#present_point_'+id).css('display','none');
	
	}
	this.showPresentsWindowA=function(from_id){
		this.fromId=from_id;
		var browserWindow = $(window);
		var browserWidth = browserWindow.width();
		var browserHeight = browserWindow.height();
		$.ajax({
   			url: '/cms/present/getWindowPresentA',
		   	dataType : "text",
		   	data:'',
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
				$("#present_all_window_content").html(data);
				$("#present_all_window").dialog({
		   			modal: true,
					height: browserHeight-50,
					resizable: false,
					title:'Отправить подарок',
		   			width:660,
		   			});
					this.cat_names=$('#presents_cat_names').attr('value');
				this.cat_ids=$('#presents_cat_ids').attr('value');
				this.addDropCategory();
				first=parseInt($('#present_div_cat_first').attr('value'));
				this.showCategoryDiv(first);
		   	}
		   	});	
	};
	this.sendA=function(pid){
		var from=this.fromId;
		$("#present_all_window").dialog( "close" );
		$("#present_one_window").dialog( "close" );
		var msg=this.addslashes($('#present_msg').attr('value'));
		$.ajax({
   			url: '/cms/present/sendPresentA',
		   	dataType : "text",
		   	data:'msg='+msg+'&pid='+pid+'&uid='+from+'&status='+this.statusSelected,
		   	context: this,
		   	type:'POST',
		   	success: function(data) {
				if (data!='empty'){
					$("#present_one_window_ok").dialog({
						modal: true,
						resizable: false,
						height: 150,
						title:$("#present_title").attr('value'),
						width:400,
					});
				}
			}
		});
	};
	this.showPresentsWindowOneA=function(id,url,point){
		var browserWindow = $(window);
		var browserWidth = browserWindow.width();
		var browserHeight = browserWindow.height();
		$("#present_img_p").html(point);
		$("#present_img_url").attr('src','/presents/'+url);
		$("#present_one_window").dialog({
		   	modal: true,
			resizable: false,
			height: 420,
			buttons: [{text: 'Отправить',click: function() { present.sendA(id);}}],
			title:'Отправить подарок',
		   	width:660,
		});
	};
}

var present=new Present();
$(document).ready(function(){
	present.initEvents();
});
