;
var candidateActionChanging = false;
var colortheme = "colortheme";
var path = "path"; 

function createDelegate(instance, method) {
	return function () { return method.apply(instance, arguments); };
}

//May be move to the Array prototype?
function contains(array, element) {
	var length = array.length;
	for (var i = 0; i < length; i++) {
		if (array[i] === element) {
			return true;
		}
	}
	return false;
}

(function($){
    $(document).ready(function(){

		 $('#fontsizer').jfontsizer({
			applyTo: 'body',
			changesmall: '2',
			changelarge: '2',
			changeSmallTooltip: 'Small',
			changeMediumTooltip: 'Medium',
			changeLargeTooltip: 'Large'
		});
        
        $(this).initButtons();
		
		$(".es-localization")
		    .languages();

		$(".site-menu").siteMenu();
		
		$(".ui-button").button();

		$(".asp-grid-view").grid();

		$(".dev-grid-view").devgrid();

		$(".actionsDropDown").actionsDropDown();

		$(".tabs-container .tab").hover(
			function() {
				$(this).addClass("tab-hover");
			},
			function() {
				$(this).removeClass("tab-hover");
			}
		);

		$("a:not([href]).ui-button").enableButton();
    });

	$.fn.siteMenu = function(){
		return $(this).each(function(){

			$(this).find(".menu-item-free").buttonBehaviour();

			$(this).find(".menu-item-reports").hover(function(){
			
				if($(".sub-menu-reports").is(":not(:visible)")){
					$(".sub-menu-reports").slideDown("fast").show();

					$(this).hover(
						function() {}, 
						function() {
							$(".sub-menu-reports").slideUp("slow"); 
						}
					);
				}
			});

		});
	};

	$.fn.moveEvent = function(eventType, targetEventType) {
		return $(this).each(function() {
			var events = $(this).data("events");

			if (events) {
				if (events[eventType]) {
					$(this).unbind(targetEventType + ".specialEvent");

					for (var handler in events[eventType]) {
						$.event.add(this, targetEventType, events[eventType][handler], events[eventType][handler].data);
					};

					$(this).unbind(eventType);

					$(this).bind(eventType + ".specialEvent", function () {
						return false;
					});
				}
			}
		})
	};

	$.fn.enableButton = function() {
		return $(this).each(function() {
			$(this)
				.button("enable")
				.moveEvent("disabledclick", "click")
				.unbind("disabledclick");
		});
	};

	$.fn.disableButton = function() {
		return $(this).each(function() {
			if ($(this).button("option", "disabled") === false) {
				$(this)
					.button("disable")
					.moveEvent("click", "disabledclick");
			}
		});
	};

    
    $.fn.languages = function() {
		$(this).each(function() {
			$(this).buttonBehaviour();

			$(this).click(function() {
				$(this).next(".es-locales").fadeIn("medium");
				$(this).addClass("es-active");
				$(this).closeLanguages();
			});
			
			
			$(this).next(".es-locales").find(".es-locale").click(function(){
			    eval($(this).find("a").attr("href"));
			});
		});
	};

	$.fn.closeLanguages = function() {
		return $(this).each(function() {
			var container = $(this);

			setTimeout(function() {
				if (container.hasClass("ui-state-hover")) {
					container.closeLanguages();
				}
				else if (container.next(".es-locales").find(".ui-state-hover").size() > 0) {
					container.closeLanguages();
				}
				else {
					container.next(".es-locales").fadeOut("medium");
					container.removeClass("es-active");
				}
			}, 1000);
		});
	};
	
	$.fn.buttonBehaviour = function() {
		return $(this).each(function() {
			$(this)
	            .addClass("ui-state-default")
	            .hover(
					function() {
						$(this).addClass("ui-state-hover");
					},
					function() {
						$(this).removeClass("ui-state-hover");
					}
				)
				.mousedown(
					function() {
						$(this).addClass("ui-state-active");
					}
				)
				.mouseup(
					function() {
						$(this).removeClass("ui-state-active");
					}
				);
		});
	};
	
	$.fn.initButtons = function() {
		return $(this).each(function() {
			$(this).find(".es-locale")
				.button();
		});
	};

	$.fn.grid = function() {
		return $(this).each(function(){

			$(this).find(".asp-grid-view-normal, .asp-grid-view-alt").find("td:first").find(":checkbox").click(function(){
				
				$(this).parents("tr:first").toggleClass("asp-grid-view-selected");
			});

			$(this).find(".asp-grid-view-head").find("td:first, th:first").find(":checkbox").bind("click", function(e){
				
				var selectAllCheckbox = e.target;
				
				var rows = $(this).parents("table:first").find(".asp-grid-view-normal, .asp-grid-view-alt, .asp-grid-view-selected");

				if($(selectAllCheckbox).is(":checked")){
					
					$(rows).find("td:first").find(":checkbox:not(:checked)").each(function(){
						$(this)[0].click();
					}); 
				}
				else {
					
					$(rows).find("td:first").find(":checkbox:checked").each(function(){
						$(this)[0].click();
					});
				}
			});

			$(this).find(".asp-grid-view-normal, .asp-grid-view-alt, .asp-grid-view-selected")
				.each(
					function(){
						var checkbox = $(this).find("td:first").find(":checkbox");

						if(checkbox.length == 1){

							if($(checkbox).is(":checked")){
								$(this).addClass("asp-grid-view-selected");
							}
						}
					}
				)
				.click(
					function(e){
						
						if ($(e.target).is(":not(:checkbox, .unclickable)")) {

							var checkbox = $(this).find("td:first").find(":checkbox");

							if (checkbox.length == 1) {
												
								checkbox[0].click();
							}
							else if($(e.target).is("tr, td")){
								if($(this).find(".select-command-link").length == 1){
									eval($(this).find(".select-command-link").attr("href"));									
								}
							}
						}
					}
				)
				.mousedown(
					function(e){
			
						var checkbox = $(this).find("td:first").find(":checkbox");

						if (checkbox.length == 1 || $(this).find(".select-command-link").length == 1) {
							$(this).addClass("asp-grid-view-pressed");
						}
					}
				)
				.mouseup(
					function(e){
			
						var checkbox = $(this).find("td:first").find(":checkbox");

						if (checkbox.length == 1 || $(this).find(".select-command-link").length == 1) {
							$(this).removeClass("asp-grid-view-pressed");
						}
					}
				);
		});
	};

	$.fn.devgrid = function() {	
		return $(this).each(function(){
			
			$(this).find(".asp-grid-view-normal, .asp-grid-view-alt")
				.click(
					function(e){
						
						if ($(e.target).is(":checkbox")) {
							return;
						}

						var checkbox = $(this).find(":checkbox");

						if (checkbox.length == 1) {
							checkbox[0].click();
						}
					}
				)
				.mousedown(
					function(e){
			
						var checkbox = $(this).find(":checkbox");

						if (checkbox.length == 1) {
							$(this).addClass("asp-grid-view-pressed");
						}
					}
				)
				.mouseup(
					function(e){
			
						var checkbox = $(this).find(":checkbox");

						if (checkbox.length == 1) {
							$(this).removeClass("asp-grid-view-pressed");
						}
					}
				);
		});
	};

	setCookie = function (c_name,value,expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ";path=/";
	};
		
	getCookie = function (c_name) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");

			if (c_start!=-1) {
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);

				if (c_end==-1) c_end=document.cookie.length;

				return unescape(document.cookie.substring(c_start,c_end));
			}
		}

		return "";
	};

	$.fn.actionsDropDown = function(){
		return $(this).each(function(){
			$(this).change(function(){
				
				if(candidateActionChanging){
					return;
				}
				else {
					candidateActionChanging = true;	

					var selectedItem = $(this).find(":selected").val();

					$(".actionsDropDown").each(function(){
						$(this).find("option[value='" + selectedItem + "']").attr("selected", "selected");
					});

					if(updateActionButton){
							updateActionButton();
						}

					candidateActionChanging = false;	
				}

			});
		});
	};

	$.messageBox = function(data, options) {
		var buttons = [];

		if (options.okCall !== null) {
			var okText = resources.buttons.ok;

			if (options.okText != null) {
				okText = options.okText;
			}

			buttons[okText] = function() {
				options.okCall();
			};
		}

		if (options.cancelCall !== null) {
			var cancelText = resources.buttons.cancel;

			if (options.cancelText != null) {
				cancelText = options.cancelText;
			}

			buttons[cancelText] = function() {
				$(this).dialog("close");
				$(this).dialog("destroy");

				options.cancelCall();
			};
		}

		var title = '';

		if (options.title != null) {
			title = options.title;
		}

		var defaultOptions = {
			title: title,
			buttons: buttons,
			width: 405,
			position: 'center',
			closeOnEscape: false,
			modal: true,
			resizable: false,
			open: function() {
				$(this).parent().find('.ui-dialog-buttonpane button:eq(0)').focus();

				$(this).parent().find("input").keypress(function(e) {
					if ((!!e.which && e.which === 13) || (!!e.keyCode && e.keyCode === 13)) {
						options.okCall();
					}
				});

				if (options.openCall != null) {
					options.openCall();
				}
			}
		};

		$.extend(defaultOptions, options);

		$(data).dialog(defaultOptions);
	};

	$.applyColorTheme = function(cssFile)
	{
		$("link.color-theme").attr("href", cssFile);
	    document.cookie = colortheme + "=" + escape(cssFile);
	    document.cookie = path + "=/" +  document.location.hostname + "/";
	};

	$.fn.setSpinEditState = function(enabled) {
			return $(this).each(function() {
                for (var spinEdit in __aspxSpinEditCollection.elements)
                {
                    if ($(this).find("#" + spinEdit).length > 0) {
                        __aspxSpinEditCollection.elements[spinEdit].SetEnabled(enabled);
                    }
                }
			});
		};    
})(jQuery);


