$(document).ready(function () {
	if (!contentid) {
		$('.fademap').fademap();
	}
});

(function (){
	$.fn.loCheckbox = function () {
		var num = 0;
		var inst = this;
		this.click(updateAll);
		
		
		function updateAll() {
			inst.each(function (index) {
				inst[index].update();
			})
		}
		
		return this.each(function(index){
		  	num++;
			var checkedImg = '<img src="i/checkbox/chk1.gif" id="loCheckbox'+num+'" style="vertical-align:middle" />';
			var uncheckedImg = '<img src="i/checkbox/chk0.gif" id="loCheckbox'+num+'" style="vertical-align:middle" />';
			var img;
			
			var checkbox = $(inst[index]);
			
			
			checkbox.css('display', 'none')
			
			if (!checkbox.attr('checked'))
				checkbox.before(uncheckedImg)
			else 
				checkbox.before(checkedImg)
			
			var img = $('#loCheckbox' + num);
			img.click(clickfunc)
				
			function clickfunc(e) {
				if (!checkbox.attr('checked')) {
					checkbox.attr('checked', 'checked')
				} else {
					checkbox.removeAttr('checked');
				}
				updateAll();
				if (checkbox[0].onchange) checkbox[0].onchange();
				if (checkbox[0].onclick) checkbox[0].onclick();
			}
			
			this.update = function () {
				if (checkbox.attr('checked')) {
					img.attr('src', 'i/checkbox/chk1.gif');
				} else {
					img.attr('src', 'i/checkbox/chk0.gif');
				}
				if (checkbox[0].onchange) checkbox[0].onchange();
			}
			
			
		});
	}
	
	$.fn.loInput = function () {
		this
			.wrap('<table border="0" cellspacing="0" cellpadding="0" class="ib"><tbody><tr><td></td></tr></tbody></table>')
			.css('background', 'url(i/input/center.gif) repeat-x top')
			.css('height', '22px')
			.css('line-height', '22px')
			.css('border','none')
			.css('font-size','11px')
			.parent()
			.before('<td style="width:6px;background:url(i/input/left.gif) top">&nbsp;</td>')
			.after('<td style="width:6px;background:url(i/input/right.gif) top">&nbsp;</td>')
	}
	
	$.fn.loButton1 = function () {
		this.css('width', this.width() - 2* 17)
		this.wrap('<table border="0" cellspacing="0" cellpadding="0" class="ib"><tbody><tr><td></td></tr></tbody></table>')
			.css('background', 'url(i/input/btn_center.gif) repeat-x top')
			.css('height', '22px')
			.css('padding', '0')
			.css('margin', '0')
			.css('line-height', '20px')
			.css('border','none')
			.css('color', '#040404')
			.css('font-size','11px')
			
				.parent()
				.before('<td style="width:17px;background:url(i/input/btn_left.gif) top">&nbsp;</td>')
				.after('<td style="width:17px;background:url(i/input/btn_right.gif) top">&nbsp;</td>')
	}
	
	$.fn.loButton2 = function () {
		this.css('width', this.width() - 2* 17)
		this.wrap('<table border="0" cellspacing="0" cellpadding="0" class="ib"><tbody><tr><td></td></tr></tbody></table>')
			.css('background', 'url(i/input/btn2_center.gif) repeat-x top')
			.css('height', '22px')
			.css('padding', '0')
			.css('margin', '0')
			.css('line-height', '20px')
			.css('border','none')
			.css('color', '#fefefe')
			.css('font-size','11px')
			
				.parent()
				.before('<td style="width:17px;background:url(i/input/btn2_left.gif) top">&nbsp;</td>')
				.after('<td style="width:17px;background:url(i/input/btn2_right.gif) top">&nbsp;</td>')
	}
	
	$.fn.loButton3 = function () {
		this.css('width', this.width() - 2* 17)
		this.wrap('<table border="0" cellspacing="0" cellpadding="0" class="ib"><tbody><tr><td></td></tr></tbody></table>')
			.css('background', 'url(i/input/btn3_center.gif) repeat-x top')
			.css('height', '22px')
			.css('padding', '0')
			.css('margin', '0')
			.css('line-height', '20px')
			.css('border','none')
			.css('color', '#4c4c4e')
			.css('font-size','11px')
			
				.parent()
				.before('<td style="width:17px;background:url(i/input/btn3_left.gif) top">&nbsp;</td>')
				.after('<td style="width:17px;background:url(i/input/btn3_right.gif) top">&nbsp;</td>')
	}
	
	$.fn.loButton4 = function () {
		this.css('width', this.width() - 2* 17)
		this.wrap('<table border="0" cellspacing="0" cellpadding="0" class="ib"><tbody><tr><td></td></tr></tbody></table>')
			.css('background', 'url(i/input/btn4_center.gif) repeat-x top')
			.css('height', '22px')
			.css('padding', '0')
			.css('margin', '0')
			.css('line-height', '20px')
			.css('border','none')
			.css('color', '#040404')
			.css('font-size','11px')
			
				.parent()
				.before('<td style="width:17px;background:url(i/input/btn4_left.gif) top">&nbsp;</td>')
				.after('<td style="width:17px;background:url(i/input/btn4_right.gif) top">&nbsp;</td>')
	}
	
	$.fn.loButton5 = function () {
		this.css('width', this.width() - 2* 11)
		this.wrap('<table border="0" cellspacing="0" cellpadding="0" class="ib"><tbody><tr><td></td></tr></tbody></table>')
			.css('background', 'url(i/input/btn5_center.gif) repeat-x top')
			.css('height', '22px')
			.css('padding', '0')
			.css('margin', '0')
			.css('line-height', '20px')
			.css('border','none')
			.css('color', '#040404')
			.css('font-size','11px')
			
				.parent()
				.before('<td style="width:11px;background:url(i/input/btn5_left.gif) top">&nbsp;</td>')
				.after('<td style="width:11px;background:url(i/input/btn5_right.gif) top">&nbsp;</td>')
	}
	
	$.fn.styleSelect = function(options){
		
		var tabindex = 1;
		
		var opts = $.extend({}, $.fn.styleSelect.defaults , options);
		
		//set tabindex		
		$('input,select,textarea,button').each(function() {
			
			var input = $(this);
				
			if (!input.attr('tabindex')){
				
				input.attr('tabindex', tabindex);
				input.css('z-index', tabindex)
				tabindex++;
				
			} 
		});
		
		var selinst = this;
	
		return this.each(function(ind){
	
			mainSelect = $(selinst[ind]);
			var mainId = mainSelect.attr('name');
			
			var styledTabIndex = mainSelect.attr('tabindex');
			
			var date = new Date;
			var selectId = 'selectbox_'+mainId+date.getTime();
			
			//Hidde select box
			mainSelect.hide();
	
			//Main container 
			var mainContainer = $('<div tabindex="'+styledTabIndex+'"></div>').css({position : 'relative', 'z-index' : 100-styledTabIndex})
					.addClass(opts.styleClass)
					.attr('id', selectId)
					.insertBefore(mainSelect);
			
			//Options container
			var subContainer = $('<ul></ul>').css({'position' : 'absolute', 'z-index' : 100+styledTabIndex, 'top' : opts.optionsTop, 'left' : opts.optionsLeft})
					.appendTo($(mainContainer))
					.hide();
				
			//Generate options list
			var optionsList = "";
			
			mainSelect.find('option').each(function(){
			
				optionsList += '<li id="'+$(this).val()+'"';
				if($(this).attr('class')) optionsList += ' class="'+$(this).attr('class')+'" ';
				optionsList += '>';
				optionsList += '<span style="display: block;"';
				if ($(this).attr('selected')) optionsList += ' class="selected" ';
				optionsList += '>';
				optionsList += $(this).text();
				optionsList += '</span>';
				optionsList += '</li>';
				
			});

				subContainer.append(optionsList);
				
				checkSelected(opts.styleClass,opts.optionsWidth);
				
			//Show otions
			$('#'+selectId).click(function(){
				$(this).find('ul').slideToggle(opts.speed);
			});
			
			//On click
			$('#'+selectId+' li').click(function(){
				
				doSelection($(this));

			});
			
			
			//Keyboard support
			$('#'+selectId).keydown(function(event){
				
				var active = $(this).find('.selected').parent();
				
				if (event.keyCode == 40 || event.keyCode == 39 ){ doSelection(active.next()); }
				if (event.keyCode == 37 || event.keyCode == 38 ){ doSelection(active.prev()); }
				
				if (event.keyCode == 13 || event.keyCode == 0){ $(this).find('ul').slideToggle(opts.speed); }
				if (event.keyCode == 9){ $(this).find('ul').hide(opts.speed); }
				
			});
			
			selinst.update = function () {
				var item = subContainer.find('li#' + mainSelect[0].value);
				doSelection(item);
			}
			
			//Do selection
			var doSelection = function(item){
				
				item.siblings().find("span").removeClass('selected');
				item.find("span").addClass('selected');
		
				var selectedItem = item.attr('id');

				var realSelector = $('select[name="'+mainId+'"]');
				realSelector.siblings().selected = false;
				realSelector.find('option[value="'+selectedItem+'"]').attr('selected','selected');
				realSelector.trigger(opts.selectTrigger);
		
				checkSelected(opts.styleClass,opts.optionsWidth);
				
				var selobj = item.parent().parent().parent().find('select')[0];
				selobj.value = selectedItem;
				if (selobj.onchange) selobj.onchange();
				
			}
			
			$('#'+selectId).click(function(e) {
				e.stopPropagation();
			});
			
			$(document).click(function() {
				$('#'+selectId+' ul').hide();
			});
			
			});	
		}
		
		//Selected items check
		function checkSelected(mainClass,mainWidth){
				
				$('.'+mainClass).each(function(){
				
					var elementList = $(this);
					
					$(this).find('span').each(function(){
					
						var spanClass = $(this).attr("class");
						if (spanClass == "passiveSelect" || spanClass == "activeSelect") $(this).remove();
					
					});
					
					var selectedName = $(this).find('.selected');
					
					$('<span></span>').text(selectedName.text())
							.attr('id', selectedName.parent().attr('id'))
							.addClass('passiveSelect')
							.appendTo($(this));
					
					if (mainWidth === 0){
						if (elementList.width()) {
						$(this).css({'width' :  elementList.width()});
							$(this).find('ul').css('width', elementList.width())
					}
					} else {
						$(this).css({'width' :  mainWidth});
						$(this).find('ul').css('width', mainWidth)
					}
					
					
				});
				
				$('.'+mainClass+' span').each(function(){
					if ($(this).attr('id')){
						$(this).removeClass();
						$(this).addClass('activeSelect');
					}
				});
		}	
	
		$.fn.styleSelect.defaults = {
		
			optionsTop: '26px',
			optionsLeft: '0px',
			optionsWidth: 0,
			styleClass: 'selectMenu',
			speed: 0,
			selectTrigger: 'change'
			
		};
	
	$.fn.fademap = function () {
		var imgs;
		var sel = this.attr('img');
		if (sel) {
			imgs = $(sel);
			var inst = this;
		}
		if (imgs)
		imgs.each(function (index) {
			var img = $(imgs[index]);
			img.before('<div id="fadecont' + index + '" style="position:absolute;"></div>');
			var cont = $('#fadecont' + index);
			
			var areas = inst.find('area');
			var rects = new Array();
			
			/*img.mouseout(function (e) {
				for (j=0;j<rects.length;j++) {
					$(rects[j]).stop(true).animate({'opacity':'0'});
				}
			})*/
			
			for (i=0; i<areas.length;i++) {
	
				if ($(areas[i]).attr('shape').toLowerCase() == 'rect')	{
					
					var coords = explode(',', $(areas[i]).attr('coords'));
					var href = $(areas[i]).attr('href');
					if (!href) href = '';
	
					if (href) {	
						cont.append('<a href="' + href + '" id="fademapitem' + index + '-'+ i +'" style="display:block;position:absolute;background:#000000;text-decoration:none;left:' + coords[0] + 'px;top:' + coords[1] + 'px;width:' + (coords[2]-coords[0]) + 'px;height:' + (coords[3] - coords[1]) + 'px">&nbsp;</a>');
					} else {
						cont.append('<div style="display:block;position:absolute;overflow:hidden;text-decoration:none;left:' + coords[0] + 'px;top:' + coords[1] + 'px;width:' + (coords[2]-coords[0]) + 'px;height:' + (coords[3] - coords[1]) + 'px"><img id="fademapitem' + index + '-'+ i +'" usemap="'+img.attr('usemap')+'" src="img/spacer.gif" style="background:#000;margin-left:-' + coords[0] + 'px;margin-top:-' + coords[1] + 'px;width:'+img.width()+'px;height:'+img.height()+'px;" ></div>');
					}
					
					var a = $('#fademapitem' + index + '-'+ i);
					rects.push(a[0]);
					a.css('opacity', 0)
					.mouseover(function (e) {
						for (j=0;j<rects.length;j++) {
							if (rects[j] == e.currentTarget) $(e.currentTarget).stop(true).animate({'opacity':'0'});
							else $(rects[j]).stop(true).animate({'opacity':'0.5'});
						}
					})
					.mouseout(function (e) {
						var offset = img.offset();
						if (e.pageX < offset.left || e.pageY < offset.top || e.pageX >= (img.width() + offset.left) || e.pageY >= (img.height() + offset.top)) {
							for (j=0;j<rects.length;j++) {
								$(rects[j]).stop(true).animate({'opacity':'0'});
							}
						}
					})
				}
			}
			
			
			
			
		});
		
		function explode( delimiter, string ) { 
			var emptyArray = { 0: '' };
	 
			if ( arguments.length != 2
				|| typeof arguments[0] == 'undefined'
				|| typeof arguments[1] == 'undefined' )
			{
				return null;
			}
		 
			if ( delimiter === ''
				|| delimiter === false
				|| delimiter === null )
			{
				return false;
			}
		 
			if ( typeof delimiter == 'function'
				|| typeof delimiter == 'object'
				|| typeof string == 'function'
				|| typeof string == 'object' )
			{
				return emptyArray;
			}
		 
			if ( delimiter === true ) {
				delimiter = '1';
			}
		 
			return string.toString().split ( delimiter.toString() );
		}
		
	}

})(jQuery)
