﻿// JScript File

jQuery.fn.createSelectElement = function() {
				
	//var cfe = "<ul>";
	
	var ul = document.createElement('ul');
	var parent = this;
	
	this.children().each(function(){
		
		var li = document.createElement('li');
		
		li.className = this.value;
		li.innerHTML = this.text;
		
		
		if(parent.attr("onchange") != null && parent.attr("onchange") != 'undefined')
			$(li).attr("onclick",parent.attr("onchange").replace("javascript:",""));
		
		$(li).click(function() {
			parent.val(this.className);
		});
		
		ul.appendChild(li); 
	});
	
	this.after(ul); //children()[0]);
	//var cfe = "</ul>";
};

//$('select').createElement();

jQuery.fn.initElement = function() {
				
	
	//var cfe = "</ul>";
};


jQuery.fn.createCustomCheckbox = function() {
				
	var ul = document.createElement('ul');
	var parent = this;
	
	this.children().each(function(){
		
		var li = document.createElement('li');
		
		li.className = this.value;
		li.innerHTML = this.text;
		
		
		if(parent.attr("onchange") != null && parent.attr("onchange") != 'undefined')
			$(li).attr("onclick",parent.attr("onchange").replace("javascript:",""));
		
		$(li).click(function() {
			parent.val(this.className);
		});
		
		ul.appendChild(li); 
	});
	
	this.after(ul); //children()[0]);
	//var cfe = "</ul>";
};

var arrSelect = new Array();
function CustomSelect()
{
	this.datasource			= null;
	this.customSelect		= null;
	this.currentitem		= null;
	this.list				= null;
	this.selector			= null;
	this.currentValue		= null;
	this.originalValue		= null;
	this.onClickEnable		= null;
	this.currentValueObj	= null;
	this.setValueTo	= null;
	
	this.init = function(selector) 
	{
		var parent				= this;
		this.selector			= selector;
		this.currentitem		= $(this.selector + ' a:first-child');
		this.currentValue		= $(this.selector + ' span').text();
		this.currentValueObj	= $(this.selector + ' span');
		this.originalValue		= this.currentValue;
		this.list				= $(this.selector + ' ul');
		this.customSelect		= $(this.selector);
		
		
		this.currentitem.click(function(){
			if(parent.customSelect.attr('class').indexOf("select-disabled") == -1)
			{
				parent.hideLists();
				parent.list = $(this.parentNode).find("ul");
				parent.list.toggle();
				// haka på alla sub events här!!!
				
				$(this.parentNode).find("li a").each(function(){
					this.onclick = function(){
						parent.onClickItem(this);
					}
				});
				
				
			}
		});
		
		arrSelect.push(selector);
				
	}
	
	this.onClickItem = function(link)
	{
		var v = $(link).text();
		v = jQuery.trim(v);
		if(v.length > 21)
		{
			v = v.substring(0,21);
			v = v + '...';
		}
		
		this.currentValueObj.text(v);
		this.currentValue = this.currentValueObj.text();
		this.list.hide();
		

		if(this.currentValue != this.originalValue)
			this.enableOthers();
		else
			this.disableOthers();
	} 

	
	this.hideLists = function() {
		
		for(i = 0;i < arrSelect.length;i++)
		{
			if(this.selector !=  arrSelect[i])  $(arrSelect[i] + ' ul').hide();
		}
	}
	
	this.disableOthers = function()
	{

		if(this.onClickEnable)
		{
			$(this.onClickEnable).addClass("select-disabled");
		}
	}
	
	this.enableOthers = function()
	{	
		if(this.onClickEnable)
		{
			this.onClickEnable.customSelect.removeClass("select-disabled");
			//this.onClickEnable.currentValueObj.text(this.onClickEnable.originalValue);
		}
	}
	
	this.setValue = function(v) {
		$(this.setValueTo).val(v);
	}

	this.create = function(selector)
	{
		$(selector).createElement();
	}
}


function CustomCheckbox()
{
	this.datasource = null;
	this.customElement = null;
	
	
	this.init = function()
	{
		if(this.datasource.attr("checked")) this.customElement.toggleClass("checked");
	
		var parent = this;
		this.customElement.click(function(){
			parent.tick();
		});
	}
	
	this.addTrigger = function(selector)
	{
		var _parent = this;
		$(selector).click(function(){
			_parent.tick();
		});
	}
	
	this.tick = function(){

		this.datasource.attr("checked") ? this.datasource.removeAttr("checked") : this.datasource.attr("checked","true");
		this.customElement.toggleClass("checked");
		
	}
	
	this.create = function()
	{

		this.customElement = document.createElement('div');
		this.customElement.className = "custom-checkbox";
		this.datasource.after(this.customElement);
		
		
		this.customElement = $(this.customElement);
		this.datasource.hide();
		this.init();
	}
}

