/* media */
function MediaModel() {
	
	var handle				=	this;
	
	this.admin				=	0;
	this.form_inited		=	false;
	this.file_max			=	5;
	this.page				=	0;
	this.content_id			=	0;
	this.hide_add_to_item	=	0;
	this.hide_actions		=	0;
	this.controller			=	'multimedia';
	this.list_hold			=	$('#media_list_hold');
	this.detail_list_hold	=	$('#media_list_hold');
	this.info_hold			=	$('#media_info');
	
	this.lang				=	conf.lang;
	this.item_id			=	0;
	this.media_id			=	false;
	this.image_id			=	0;
	this.reorder			=	0;
	this.speed				=	500;
	this.all				=	0;
	this.convert_mode		=	0;// 0,1,2
	this.convert_modes		=	[0,1,2];
	this.list_view			=	'thumb';
	this.category_id		=	0;
	this.approve_hide		=	0;
	this.user_id			=	0;
	
	this.gate				=	conf.full_url+'admin/'+this.controller+'/';

	this.msg				=	language.msg || {};

	// public state handler
	this.state_handler		=	state_obj || new StateModel;
	this.log = function(msg, type) {
		this.state_handler.log(this.controller + "_out", msg, type);
	};

	this.set_upload_item_id = function (id) {
		$("#media_upload_swf").css("display", "block");
		this.item_id = id;
	}

	this.media_get_item_id = function() {
		return this.item_id;
	}

	this.render = function (hold, content) {
		hold.html(content);
	};

	this.tooltip = function (state) {
		if (state)	$("#category_tooltip").show();
		else		$("#category_tooltip").hide();
	};
	this.tooltip_category = function (state) {
		if (state)	$("#category_tooltip").show();
		else		$("#category_tooltip").hide();
	};

	this.close_info = function(id) {
		if (id == undefined) return;
		$("#media_detail_hold_"+id).empty();
		$("#media_detail_hold_"+id).hide();
		$("#media_edit_title_hold_"+id).hide();
		$("#item_"+id).css("position", "static");
		this.media_id = 0;
	};
	
	this.getInf = function(id) {
		if (id == undefined || id < 0) return;
		if (this.info_open) {
			this.close_info(this.image_id);
			this.info_open = false;
			return;
		}
		if (this.image_id != 0) {
			this.close_info(this.image_id);
			this.info_open = true;
		}
		$("#item_"+id).css("position", "relative");

		this.image_id	=	id;
		$("#media_detail_hold_"+id).show();
		$.post(
			this.gate + 'get_info',
			{
				type			:	'media',
				id				:	id,
				hide_add		:	media.hide_add_to_item,
				hide_actions	:	media.hide_actions
			},
			function (data) {
				handle.render( $('#media_detail_hold_'+id), data);
				$("a#media_info_"+id)
					.css("cursor", "pointer")
					.click(function (e) {
						e.preventDefault();
						handle.close_info(id);
					});
			}
		);
		//this.render( $('#media_detail_hold_'+id) , this.msg.loading );
	};
	
	this.getRand = function(){
		return Math.random( 99999999 );
	};
	this.show_all = function( type ){
		this.type = type;
		this.all = 1;
		this.getMediaDetailList();
	};
	this.save_cancel = function( type ){
		$('#edit_title_hold').hide(this.speed);
	};

	this.admin_view = function (cat_id) {
		if (cat_id == undefined || cat_id < 0) return;
		this.init(0, cat_id);
	};
	
	this.get_page = function (page) {
		if (page < 1) return;
		this.page = page - 1;
		this.getMediaDetailList();
	};

	this.init = function (page, type) {
		$("#edit_title_hold").hide();
		if (page != undefined) {
			this.page = page;
		}
		if (type != undefined) {
			this.type = type;
		}
		this.set_active_class(this.list_view);
		this.getMediaDetailList();
	};

	this.getMediaDetailList = function() {
		var obj = {
			all				:	this.all,
			page			:	this.page,
			type			:	this.type,
			reorder			:	this.reorder,
			admin			:	this.admin,
			item_id			:	this.item_id,
			list_view		:	this.list_view,
			approve_hide	:	this.approve_hide,
			user_id			:	this.user_id
		};
		$.post(
			this.gate + 'getMediaDetailList',
			obj,
			function(data) {
				$('#media_list_hold').html(data.content);
				$('.media_item_details').click(function(){
					handle.getInf($(this).attr('id').split('_')[2]);
				});
				$('.media_item_edit').click(function(){
					handle.edit_title($(this).attr('id').split('_')[2]);
				});
				$('.media_item_delete').click(function(){
					handle.remove($(this).attr('id').split('_')[2]);
				});
				$('.media_pagination').html(data.pagination);
				handle.attach_detail_list_action();
			},
			"json"
		);
		//remove all link events
		//$.each($("div.s_pager a"), function() {
			//$(this).unbind("click");
		//});
		$('#media_list_hold').html(this.msg.loading);
	};
	this.attach_detail_list_action = function(){
		if ( $('.thumb_render').length > 0 ) {
			$.each( $('.thumb_render'), function()
				{
					var id = $(this).attr("id").split('_')[2];
					$('img', this).unbind("click").bind("click", function(){media_getInf(id);});
					$('ul > li.img_shown', this).unbind("click").bind("click", function(){media_make_hidden(id);});
					$('ul > li.img_hidden', this).unbind("click").bind("click", function(){media_make_visible(id);});
					$('ul > li.img_denied', this).unbind("click").bind("click", function(){media_refuse(id);});
					$('ul > li.img_approved', this).unbind("click").bind("click", function(){media_approve(id);});
					$('ul > li.img_remove', this).unbind("click").bind("click", function(){media_remove(id);});
					$('ul > li.img_edit', this).unbind("click").bind("click", function(){media_edit_title(id);});
				} 
			);
		}
	};
	/**
	 * delete an item
	 * @todo make script check for clones and existense in documents
	 * @param id
	 * @return void
	 */
	this.remove = function(id)
	{
		if (id < 0) return;
		if (confirm(this.msg.confirm)) {
			$.post(
				conf.full_url+'admin/'+media.controller+'/remove',
				{id:id},
				function(reply) {
					if (reply.status) {
						$('#single_' + handle.controller + '_hold').empty();
					}
					handle.getMediaDetailList();
				},
				"json"
			);
		}
	};
	//sort update
	this.sortUpdate = function()
	{
		if (this.item_id == 0) return;
		var dragEls = $('#media_list_hold > ul.sortable_list li');
		var items =[];
		var unique = {};
		jQuery.each(dragEls, function (){ 
			var id = $(this).attr('id').split('_')[1]; 
			if( unique[ id ] == undefined ) {
				unique[ id ] = 1;
				items.push( id );
			}
		});
		items = items.join("_");
		$.post(
			conf.base_url + conf.lang + '/admin/' + this.controller + '/update_media_order',
			{ item_id: this.item_id, items: items }
		);
	};
	//edit title
	this.edit_title = function(id)
	{
		if(id == undefined) return;
		if (this.image_id != 0) {
			this.close_info(this.image_id);
		}
		this.image_id = id;

		$.post(
			this.gate + 'json_get_info',
			{id:id},
			function(reply) {
				$("#item_"+id).css("position", "relative");
				for(var i in o = conf.lang_list.split(",")){
					$('#edit_title_hold_'+id+'_title_' + o[i]).val(reply.info.title[o[i]]);
				}
				$("#media_edit_title_hold_"+id).show();

				//submit
				$("form#edit_title_"+id+"_form").submit(function(e) {
						e.preventDefault();
						handle.save_title();
					}
				);
				//cancel
				$("#edit_title_hold_cancel_"+id).click(function(){
						$("#media_edit_title_hold_"+id).hide();
					}
				);
				$("a#close_title_button_"+id)
				.css("cursor","pointer")
				.click(function(e){
					e.preventDefault();
					$("#media_edit_title_hold_"+id).hide();
				});
				//tabs
				Tabs.init('normal');
			},
			"json"
		);
	};
	//edit title
	this.save_title = function() {
		var obj = {
			id: this.image_id
		};
		for( var i in o = conf.lang_list.split(",") ){
			obj['title_' + o[i] ] = $("#edit_title_hold_"+this.image_id+"_title_"+o[i]).val();
		}
		$.post(
			this.gate+'edit_title',
			obj,
			function(data) {
				if (data.status) {
					handle.reset_edit_title_form();
					handle.getMediaDetailList();
				}
				handle.reset_state();
			},
			"json"
		);
	};
	this.reset_state = function() {
		$('#' + this.controller + '_out').text('');
	};
	
	this.show_convert = function() {
		$('#convert_media').show(this.speed);
	};

	this.hide_convert = function() {
		$('#convert_media').hide(this.speed);
	};

	this.convert_media = function()
	{
		var obj = {
			mode	:	this.convert_mode,
			item_id	:	this.media_id
		};
		switch (this.convert_mode) {
		case 1:
			obj.width	= 	$('input#video_width').val();
			obj.height	= 	$('input#video_height').val();
			obj.fps		=	$('input#video_fps').val();	
		break;
		case 2:
			$.each(
					$('input[name=custom_dim]'),
					function()
					{
						if ($(this).attr("checked")) {
							obj.size = $(this).val();
						}
					}
			);
			obj.fps		=	$('input#video_fps_1').val();
		break;
		default:
		break;
		}
		$.post(
			conf.base_url + conf.lang + '/ajax/admin/' + this.controller + '/convert',
				obj,
				function (data) {
					if (data.state) {
						handle.init();
					}
				},
				"json"
		);
	};
	/**
	 * set the convert mode
	 * @param mode string
	 * @return
	 */
	this.set_convert_mode = function(mode){
		this.convert_mode = mode;
	};
	this.approve_current = function(){
		this.approve(this.image_id);
	}
	this.approve = function(id)
	{
		$.post(
				conf.base_url + conf.lang + '/ajax/admin/' + this.controller + '/approve',
				{id:id},
				function(reply)
				{
					if(Boolean(reply)) {
						$('#media_out')			.show(handle.speed)				.text(handle.msg.approved);
						$('#single_media_hold')	.removeClass("media_refused")	.addClass("media_approved");
						handle.getMediaDetailList();
					}
				}
		);
	};
	this.refuse_current = function(){
		this.refuse(this.image_id);
	}
	this.refuse = function(id)
	{
		$.post( 
			conf.base_url + conf.lang + '/ajax/admin/' + this.controller + '/refuse',
			{id:id},
			function (reply)
			{
				if(Boolean(reply)) {
					$('#media_out')			.show(handle.speed)				.text(handle.msg.refused);
					$('#single_media_hold')	.removeClass("media_approved")	.addClass("media_refused");
					handle.getMediaDetailList();
				}
			}
		);
	};
	this.init_sortable = function()
	{
		/*
		 * 
		 var sortable_conf = {
			update:function(ui, event)
			{
				handle.sortUpdate();
			},
			revert: true
		};
		if ($('.sortable_list').children().length() > 0 ) {
			$('.sortable_list').sortable(sortable_conf);
		}
		console.log( $('.sortable_list').children().length() );
		*/
	};
	this.add_to_item = function (id)
	{
		if (this.item_id != 0 ) {
			$.post( 
				conf.base_url + conf.lang + '/admin/' + this.controller + '/add_to_item',
				{id:id,item_id:this.item_id},
				function (reply)
				{
					if (Boolean(reply)) {
						$('#media_out').text( '' );
						$('#media_out').show(handle.speed).text(handle.msg.attached);
						//$('#single_media_hold').empty();
						//media.getPagination();
						//media.getList();
					}
				}
			);
		}
	};
	this.reset_view_panel  = function(){
		$.each( $('.list_view > div > a'), function(){
				$(this).removeClass("active");
			}
		);
	};
	
	this.set_active_class = function(view)
	{
		this.reset_view_panel();
		$('div.list_view > div.'+view+' > a').addClass("active");
	};
	this.set_view = function(view)
	{
		this.set_active_class(view);
		this.list_view = view;
		this.getMediaDetailList();
	};
	this.set_visibility = function(id,visibility)
	{
		$.post( 
			conf.full_url+'admin/'+this.controller+'/set_visibility',
			{id:id,visibility:visibility},
			function ()
			{
				handle.getMediaDetailList();
			}
		);
	};
	this.attach_add = function(id){
	};
	this.attach_remove = function(id){
	};
	this.close_info_box = function()
	{
		$('#media_info').hide();
	};

	this.getCategoryList = function() {
		$.post(
			conf.full_url+'admin/multimedia/category/get_category_list',
			null,
			function (data) {
				$('#category_list_hold').html(data.content);
				Tabs.selectOnly(0);
			},
			"json"
		);
	};

	this.addCategory = function() {
		this.tooltip(0);
		Tabs.selectOnly(2);
		$.post(
			conf.full_url+'admin/multimedia/category/get_category_add_form',
			null,
			function (data) {
				$("#category_new_hold").html(data.content);
				SubTabs.init();
				$("form#category_add_form").submit(function(e) {
					e.preventDefault();
					handle.saveCategoryAdd();
				});
				$("button#category_add_cancel")
					.click(
						function ()
						{
							$("#category_new_hold").empty();
							Tabs.selectOnly(0);
							handle.tooltip(1);
						}
					);
			},
			"json"
		);
	}
	this.editCategory	=	function (id) {
		if (id == undefined || id < 0) return;
		this.tooltip(0);
		this.category_id = id;
		Tabs.selectOnly(1);
		$.post(
			conf.full_url+'admin/multimedia/category/get',
			{id:id},
			function (data) {
				$('#category_edit_hold').html(data.content);
				SubTabs.init();
				handle.attachCategoryEditAction();
			},
			"json"
		);
	};

	this.removeCategory = function(id)
	{
		if (id == undefined || id < 0) return;
		if (confirm(this.msg.confirm)) {
			$.post(
				conf.full_url+'admin/multimedia/category/remove',
				{id:id},
				function (o) {
					if (o.state) {
						handle.log(handle.msg.category.remove_success, 2);
						handle.getCategoryList();
					} else {
						handle.log(o.err.join("<br />"), 1);
					}
				},
				"json"
			);
		}
	}
	this.reset_category_add_form = function(){
		for (var i=0;i<conf.lang_list.split(',').length;i++) {
			$('input[name='+conf.lang_list.split(',')[i]+'_title]').val('');
		}
		$('input[name=directory]').val('');
	};

	this.saveCategoryEdit = function() {
		var obj = {
			id			:	this.category_id,
			directory	:	$("input[name=directory]").val()
		};
		for (var i=0;i<conf.lang_list.split(',').length;i++) {
			var code = conf.lang_list.split(',')[i];
			obj['title_'+code] = $('input[name=title_'+code+']').val();
		}
		
		$.post(
			conf.full_url+'admin/multimedia/category/edit',
			obj,
			function (data) {
				if (data.state) {
					handle.log(handle.msg.category.edit_success, 2);
				} else {

				}
			},
			"json"
		);
	}
	this.saveCategoryAdd = function() {
		var obj = {
			id			:	this.category_id,
			directory	:	$("input[name=directory]").val()
		};
		for (var i=0;i<conf.lang_list.split(',').length;i++) {
			var code = conf.lang_list.split(',')[i];
			obj['title_'+code] = $('input[name=title_'+code+']').val();
		}

		$.post(
			conf.full_url+'admin/multimedia/category/add',
			obj,
			function (data) {
				if (data.state) {
					handle.log(handle.msg.category.add_success, 2);
					for (var i in data.err) $("p#"+i+"_err").text('');
				} else {
					for (var j in data.err) $("p#"+j+"_err").text(data.err[j]);
				}
			},
			"json"
		);
	}
	this.attachCategoryEditAction = function() {
		$("form#category_edit_form").submit(function(e) {
				e.preventDefault();
				handle.saveCategoryEdit();
			}
		);
		$("button#category_edit_cancel")
			.unbind("click")
			.bind("click", function() {
					handle.tooltip(1);
					$("div#category_edit_hold").empty();
					handle.category_id = 0;
					Tabs.selectOnly(0);
				}
			);
	};
	
	this.attachActions = function(){
		$("a#category_reload, a#category_new_tab").css("cursor", "pointer");
		$("a#category_reload").click(function(){
				handle.getCategoryList();
			});

		$("a#category_new_tab").click(function(){
				handle.addCategory();
			}
		);
		/*
		$('button#media_category_edit_cancel')
			.unbind("click")
			.bind("click", function(){
				}
			);
		$('button#media_category_add_cancel')
			.unbind("click")
			.bind("click", function(){
					handle.reset_category_add_form();
					Tabs.select(0);
				}
			);
			*/
	};
	//reset edit title form
	this.reset_edit_title_form = function()
	{
		$("form#edit_title_form").unbind("submit");
		$("button#media_edit_title_cancel").unbind("click");
		//new
		$('#edit_title_hold').hide(handle.speed);
		for(var i in o = conf.lang_list.split(",")){
			$('#edit_title_hold_title_' + o[i]).val("");
		}
		$('#show_state').attr("checked", "");
		
	};
	this.category_init = function(){
		this.attachActions();
		this.getCategoryList();
		/*shibana rabota*/
		Tabs = new TabModel('.tabulation');
		Tabs.init('normal');
		Tabs.selectOnly(0);
	};
}

var media = new MediaModel();
function media_getInf(id){media.getInf(id);}
function media_remove(id){media.remove(id);}
function media_edit_title(id){media.edit_title(id);}
function media_save_title(){media.save_title();}
function media_show_all(p){media.show_all( p );}
function media_save_cancel(){media.save_cancel();}
function media_init() {
	if (arguments.length == 2) {
		media.init(arguments[0], arguments[1]);
	} else if (arguments.lenght == 1) {
		media.init(arguments[0]);
	} else {
		media.init();
	}
}
function media_refuse(id){media.refuse(id);}
function media_approve(id){media.approve(id);}
function media_show_convert(){media.show_convert();}
function media_hide_convert(){media.hide_convert();}
function media_convert_media(){media.convert_media();}
function media_set_convert_mode(mode){media.set_convert_mode(mode);}
function media_init_sortable(){media.init_sortable();}
function media_init_draggable(){media.init_draggable();}
function media_add_to_item(id){media.add_to_item(id);}
function media_set_view(view){media.set_view(view);}
function media_approve_current(){media.approve_current();}
function media_make_visible(id){media.set_visibility(id,0);}
function media_close_info(){media.close_info_box();}
function media_category_remove(id){media.category_delete(id);}
function media_category_add(){media.category_add();}
function media_category_save(){media.category_save();}
function media_category_edit(id){media.category_edit(id);}
/* MVC */

function media_make_hidden(id){media.set_visibility(id,1);}
function media_attach_add(id){media.attach_add(id);}
function media_attach_remove(id){media.attach_remove(id);}
function media_get_item_id() { return media.media_get_item_id();}

