var select_level = false;
var elem_changed = false;
$(document).ready(function(){

	$(function() {
        $( "#tabs" ).tabs();

		//true games from app/views/content/admin/edit_advanced.php
		if(select_level)
			$("#tabs").tabs("select", select_level);
		select_level = false;
    });

	$('form').change(function(){

		elem_changed = true;
	});
	if(window.CKEDITOR){

		if($('[name=content]').length > 0){

			CKEDITOR.instances['content'].on('blur', function(e){

				elem_changed = true;
			});
		}
	}
	$('a').click(function(){

		if($(this).attr('href').indexOf('tabs') == -1 && elem_changed == true){

			if($(this).attr('class') == undefined || $(this).attr('class').indexOf('view_positions') == -1){

				if( $.confirm( $.language('Data has changed continue without save?') ) == true){

					elem_changed = false;
					return true;
				}
				return false;
			}
		}
		if($(this).attr('href').indexOf('#tabs-3') > -1){

			$('[name=file_type]').val('IMAGE');
		}
		if($(this).attr('href').indexOf('#tabs-4') > -1){

			$('[name=file_type]').val('FILE');
		}
	});

	$('[name=insert_image]').click(function(){

		$('[name=image]').css({'background' : 'white'});

		if( $('[name=image]').val().length == 0 && $('[name=image_url]').val().length == 0 ){

			$('[name=image]').css({'background' : 'red'});
			return false;
		}
	});

	$('.show_sub_menu').click(function(){

		var parent_id = $(this).attr('href').replace('#', '');

		if( $(this).html() == '+' ){

			var elemClass = $('.submenu_'+parent_id).attr('class');

			$('.submenu_'+parent_id).show();
			$('.submenu_'+parent_id).attr({'class' : $.trim(elemClass.replace('hidden', ''))});
			$(this).html('-');
			$.get('/ajax.php?model=content&page=content_list&action=register_parent_id&parent_id='+parent_id);
		}
		else{

			$.get('/ajax.php?model=content&page=content_list&action=unregister_parent_id&parent_id='+parent_id, function(data){

				if($.canJSON(data)){

					$.each($.parseJSON(data), function(k, v){

						$('.submenu_'+v).hide();
						var elemClass = $('.submenu_'+v).attr('class');
						$('.submenu_'+v).attr({'class' : elemClass+' hidden'});
						$('.current_sub_menu_link_'+v).html('+');
					});
				}
			});
		}
		return false;
	});

	$('[name=plugin_id]').change(function(){

		$.get('/plugin.php?plugin='+this.value+'&page=showDialog&to_value=plugin_code', function(data){

			$('[name=plugin_code]').after( $().ajaxDiv(data) );
		});
		$('[name=plugin_code]').val('');
		if(this.value.length > 0)
			$('[name=plugin_code]').val('[PLUGIN='+this.value+']');
	});

	$('[name=plugin_id]').click(function(){

		$.get('/plugin.php?plugin='+this.value+'&page=showDialog&to_value=plugin_code', function(data){

			$('[name=plugin_code]').after( $().ajaxDiv(data) );
		});
		$('[name=plugin_code]').val('');
		if(this.value.length > 0)
			$('[name=plugin_code]').val('[PLUGIN='+this.value+']');
	});
	$('[name=plugin_code]').click(function(){

		$(this).select();
	});
	$('#content_source_link').click(function(){

		if( $('.content_source').css('display') == 'none' ){

			$('.content_source').show();
		}
		else{

			$('.content_source').hide();
		}

		return false;
	});

	/*$('#content_list').tableDnD({
		onDragClass: 'contentDrag',
		onDragStart: function(){

			$('#content_list tr').css({'background' : 'white'});
		},
		onDrop: function(table, row) {

			var prevMenu = $(row).prevAll('tr:not(.hidden)').attr('id');
			if(prevMenu == undefined) prevMenu = 'id_0';
			var currentMenu = $(row).attr('id');
			var nextMenu = $(row).nextAll('tr:not(.hidden)').attr('id');
			if(nextMenu == undefined) nextMenu = 'id_0';
			$().fadeOutDiv({'content' : 'loading', 'bgDiv' : true});

			$.get('/ajax.php?model=content&page=content_list&action=move_menu&prevMenu='+prevMenu.replace('id_', '')+'&currentMenu='+currentMenu.replace('id_', '')+'&nextMenu='+nextMenu.replace('id_', ''), function(data){

				//alert(data);
				window.location.href = window.location.href;
			});
		}
	});*/
});