Kphp = {data: {link: '', output: '', cahce: {}, error: null, flashcopier: null}, settings: {url: ''}};

Kphp.extend = function(obj)
{
	for (var i in obj)
	{
		if (this[i])
		{
			Kphp.extend.apply(this[i], [obj[i]]);
		}
		else
		{
			this[i] = obj[i];
		}
	}
};

jQuery.fn.extend({
	kphpCopy : function ()
	{
		if (this.kphp().createTextRange)
		{
			var range = this.kphp().createTextRange();
			if (range)
			{
				range.execCommand('Copy');
			}
		}
		else
		{
			if (Kphp.data.flashcopier == null)
			{
				flashcopier = document.createElement('div');
				flashcopier.id = 'flashcopier';
				document.body.appendChild(flashcopier);

				Kphp.data.flashcopier = 'flashcopier';
			}

			$('div#flashcopier').html('<embed src="' + Kphp.settings.base_path + 'clipboard.swf" FlashVars="clipboard='+escape(this.kphp().value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>');
		}
	},

	kphp : function ()
	{
		return this[0];
	}
});

Kphp.init = function()
{
	$('img.smilie:not(.processed)').each(function()
	{
		$(this).addClass('processed');
		$(this).click(function ()
		{
			$('input.code').val(Kphp.getCode(this.src));
			$('input.code').select();
		});
	});
};

Kphp.getCode = function (link)
{
	var output = '';

	var selection = Kphp.getSelection();

	if (selection == 0)
	{
		output = '[url=' + Kphp.settings.url + '][img]' + link + '[\/img][\/url]';
	}
	else if (selection == 1)
	{
		output = link;
	}

	Kphp.data.output = output;
	Kphp.data.link = link;

	return output;
};

Kphp.changeOutput = function()
{
	if (Kphp.data.output != "")
	{
		$('input.code').val(Kphp.getCode(Kphp.data.link));
		$('input.code').select();
	}
};

Kphp.getSelection = function()
{
	return $('select.output')[0].selectedIndex;
};

Kphp.Copy = function ()
{
	var value = Kphp.data.output;

	if (value.length < 1 || value == '')
	{
       alert('يرجى أختيار صورة ثم اضغط على زر النسخ');
       return false;
	}
	else
	{
		$('input.code').kphpCopy();
		alert('تم نسخ الرابط بنجاح');
	}
};

$(document).ready(function()
{
	Kphp.init();

	$('select.output').change(function() {
		Kphp.changeOutput();
	});

	if (jQuery.browser.mozilla)
	{
		$('button.copy').css('display', 'none');
	}
	else
	{
		$('button.copy').click(function (){
			Kphp.Copy();
			return false;
		});
	}
	$('form table:has(th.select-all):not(.tableSelect-processed)').each(Kphp.tableSelect);
});

Kphp.selectAll = function (obj)
{
	Kphp.toggleAll(obj, "select-one", obj.selectAll.selectedIndex);
};

Kphp.toggleAll = function(obj, formtype, setto)
{
	for (var i =0; i < obj.elements.length; i++)
	{
		var elm = obj.elements[i];
		if (elm.type == formtype && elm.name != 'selectAll')
		{
			switch (formtype)
			{
				case "select-one":
					elm.selectedIndex = setto;
				break;
			}
		}
	}
}