/*-----------------------------------------------------------------ON DOCUMENT READY--------------------------------------------------*/


$j(document).ready(function()
{
	/* TABS INIT */
	tabs = new Tabs("tab1",0);
	tabs = new Tabs("tab2",0);


	$j(".ch_image").click(function(){
		window.location = "#";
	});


	$j("#ppic").dblclick(function(){
		$j("#image_picker").hide("slow");
	});


	$j("#getmenusettings").change(function (){

		var v =$j("#getmenusettings option:selected").val();

		switch (v)
		{
			case "1":
				$j("#vertical_menu_settings_id").show();
				$j("#horizont_menu_settings_id").hide();
			break;

			case "2":
				$j("#horizont_menu_settings_id").show();
				$j("#vertical_menu_settings_id").hide();

			break;
		}
	});


	$j('#image_picker').hide();


	$j('.dd').each(function (){

		if($j(this).siblings('input').val()!="")
			$j(this).siblings('input').css("background-color",$j(this).siblings('input').val());
	});

	$j('.dd').click(function (){

		$j('#image_picker').hide();
		$j('.colorpicker').show();
		$j('#edit_element_id').show();

		var el= $j(this).siblings('input');
		var color_picker_div=$j('.colorpicker');
		var new_color_picker=$j.farbtastic(color_picker_div);
		new_color_picker.linkTo(el);
	});


	$j('span.ch_image').click (function (e)
	{

		$j('.colorpicker').hide();

		if($j(this).siblings("input").eq(0).val() !="")
		{

			document.getElementById('picbox_img').src=$j('input[name=img_path]').val() +$j(this).siblings("input").eq(0).val();

			FitImage($j('input[name=img_path]').val() +$j(this).siblings("input").eq(0).val());

	    }

		if($j('#image_picker').is(':hidden')==false)
		{
				$j(e.target).text(tg('Choose'));
				$j('#picbox').hide();
				$j('#edit_element_id').hide();
		}


		$j('#image_picker').show();

		$j('#edit_element_id').show();
		$j('#ppic').data("source",e.target);

	});


	$j('#ppic').click(function ()
	{
		var dest_element = $j($j(this).data("source")).siblings("input").eq(0);
		var dest_element_label = $j($j(this).data("source"));


		$j('#ppic option:selected').each(function ()
		{

 				FitImage($j('input[name=img_path]').val() + $j(this).val());


			  	dest_element.val($j(this).val());
			  	dest_element_label.text('Set');

			  	$j('#picbox').show();

		})
	});

$j("#save_property, #save_property_cancel").click(function(){
	$j("#image_picker").hide();
	$j(".colorpicker").hide();
	$j("#edit_element_id").hide();
});


/*preview*/


/*DAVE SKIN PREVIEW*/

$j("#save_preview").click (function(){


			SetAction("act_filed_id","SaveSkinData");

			var data = jQuery("form.ajax").serialize();

			var url="/admin/skinmanager/skin_manager_functions.php?preview=true";
			$j.post(url, data, function(data)
						{
						}, 'html');



			var framesrc=$j(".navi2[targetlink]").attr('targetlink');

			//alert(framesrc);
			$j("#preview_frame_id").attr("src",framesrc);


      });




});



/*-----------------------------------------------------FUNCTIONS-----------------------------------------------*/



function set_param(value)
{
	document.location.href='db_skins_manager.php?id=' + value;
}



/*IMAGE PICKER*/

function switchpic() {

	var pc = document.getElementById('ppic');
	if (!pc) {
		return true;
	} else {
		if (pc.selectedIndex > -1) {
			var pic = pc.options[pc.selectedIndex].value;

			//alert($j('input[name=img_path]').val() + pic);
			document.getElementById('picbox_img').src = $j('input[name=img_path]').val() + pic;


			//FitImage($j('input[name=img_path]').val() + pic);

		}
	}
}

function custom_pic_func () {
	var ctb = document.getElementById('cpics_tbl');
	var cpc = document.getElementById('cpic_cb');
	var cpi = document.getElementById('custom_pic');
	if (cpc.checked == true) {
		cpi.disabled = false;
		ctb.style.display = 'block';
	} else {
		cpi.disabled = true;
		ctb.style.display = 'none';
	}
}

function load_custom_pics (gid) {
	var ctb = document.getElementById('cpics_tbl');
	var cpi = document.getElementById('custom_pic');
	ctb.style.display = 'block';
	xmlreq_put(get_variable('PHP_SELF')+'?vcp='+escape(cpi.value)+'&gid='+gid,'cpics_tbl');
}



function FitImage(img)
{
	var newImg = new Image();
	var _height = 0;
	var _width = 0;

	newImg.onload=function(_height, _width) {
		_height = newImg.height;
		_width = newImg.width;

		 var str =  "Width: " + _width + "px " + "Height: " + _height + "px";

		$j('#imgrealsize').attr("innerHTML",str);

		if(_height >= 137) _height=137;
		if(_width >= 137) _width=137;

		$j('#picbox_img').width(_width);
		$j('#picbox_img').height(_height);
	}
	newImg.src = img;
}


/*TABS*/

function Tabs(tabname,default_tab){
            var that = this;
            that.lastSelectedTab = null;
            that.partial_submit = true;
            that.active_tabs = new Array();
            var tabs = $j("td[name^='tab_"+tabname+"_']");
            // loop all tabs, set onclick function
            tabs.each(function(i, val){
                if($j(this).attr('show') == 'false')
                    that.hideTab($j(this));
                else{
                    $j(this).show();
                    that.active_tabs.push($j(this));
                }
                $j(this).css('cursor', 'pointer');
                $j(this).css('color', '#000');
                $j(this).click(function(){
                    that.hideTab($j(this));
                })
            })
            // click default_tab tab on page load
            if(!default_tab)
                    default_tab = 0;
            $j(tabs[default_tab]).trigger('click');

            if(that.active_tabs.length == 1)
                that.active_tabs[0].hide();
        }

        Tabs.prototype.hideTab = function(tab_obj){
            that = this;
            // get tab name without prefix
            name = tab_obj.attr('name').substr(4, tab_obj.attr('name').length);
            // get div of tab

            content_div = $j('#div_'+name);

			 if(tab_obj.attr('callback')){
				var function_name = tab_obj.attr('callback');
				var targetlink= tab_obj.attr('targetlink');

				eval(function_name + "('"+targetlink+"');");

				content_div.val ("asdasd");
			 }
	        else
            	content_div.val("additional");


            // hide last tab
            if(that.lastSelectedTab){
                $j('td[name="tab_'+that.lastSelectedTab+'"]').removeClass('navi2');
               // $j('td[name="tab_'+that.lastSelectedTab+'"]').addClass('navi2');
                $j('#div_'+that.lastSelectedTab).hide();
            }
            // show new tab
          //  tab_obj.removeClass('navi2');
            tab_obj.addClass('navi2');
            content_div.show();
            // set last selected tab
            that.lastSelectedTab = name;
        }


/*TABS*/

function SetAction(filed_id,act)
{
	$j("#"+filed_id+"").val(act);
//	jQuery('form .ajax').submit();
}

function change_iframe_src(targetlink)
{
	$j("#preview_frame_id").attr("src",targetlink);
}


function check_for_new_id() {
	setTimeout(function(){
		var zhp = d('new_skin_id');
		if (zhp.value != '' && zhp.value != undefined) {
			alert('New skin created! ID is '+zhp.value);
			location.href='skin_manager.php?id='+zhp.value;
		} else {
			check_for_new_id();
		}
	},1000);
}

function check_for_updated_id() {
	setTimeout(function(){
		var zhp = d('updated_skin_id');
		if (zhp.value != '' && zhp.value != undefined) {
			if (zhp.value == 'error') {
				alert('Error updating skin');
			} else {
				alert('Updated skin '+zhp.value);
			}
		} else {
			check_for_updated_id();
		}
	},1000);
}

function skin_change(el_id) {
	var e = d(el_id);

	//alert(e.options[e.selectedIndex].value);
	var changed_id = e.options[e.selectedIndex].value;
	if (confirm('This will change the currently active skin in the system!')) {
		location.href='/admin/skinmanager/skin_manager.php?act=change&id='+changed_id;
	}
}

function check_custom_input (element) {
	var e1 = d(element);

	e1.value = e1.value.toLowerCase();
}
