/*jsl:ignoreall*/
/*jsl:option explicit*/
/*jsl:declare jQuery*/
/*jsl:declare $*/
/*jsl:declare document*/
/*jsl:declare window*/
String.prototype.startWith = function(t) {
	if (this == "undefined") return false;

	return this.match("^" + t) == t;
};

var outlineSelectedFields = function($) {
	$("#register fieldset ul li div input").focus(function(event) {
		$(event.target.parentNode).addClass("selected");
	});

	$("#register fieldset ul li div input").blur(function(event) {
		$(event.target.parentNode).removeClass("selected");
	});
};

// This function activates any input or textarea field, so when user focuses on some field
// then default value is cleared and when field looses focus, then if field is empty default
// value is restored.

// Function is supposed to be invoked without any parameters on startup - then it will
// activate some set of default fields (you can edit it below).
// When this function is invoked later, with parameters, then the first one is required
// and it should be any value of an array of values that will be then parsed with jQuery()
// method (for example it could be DOM element reference or CSS selector string).

// defaultValue when not set as parameter when invoking function will be automatically
// set to value that field currently have.

var clearInitialValueOnFocus = function(fields, defaultValue) {
	var selector, i;
	fields = fields ? [].concat(fields) : [
	"#login li.email input, #login li.password input", // login form selector
	"#register input.text" // registration form like the one on registration.html
	];
	
	for (i = 0; ((selector = fields[i])); i++) {
		$(selector).each(function(index, element) {
			element = $(element);
			var originalValue = defaultValue || element.val();

			if (!element.val().length) {
				element.val(originalValue);
			}
			if(element.get(0).tagName.toLowerCase() == 'textarea') {
				element.get(0).defaultValue = element.val();
			}
				
			element.focus(function() {
				if (!element.attr("readonly") && (element.val() === originalValue)) {
					element.val("");
				}
				if(element.get(0).tagName.toLowerCase() == 'textarea') {
					element.get(0).defaultValue = element.val();
				}
			});
			element.blur(function() {
				if (!element.val().length) {
					element.val(originalValue);
				}
				if(element.get(0).tagName.toLowerCase() == 'textarea') {
					element.get(0).defaultValue = element.val();
				}
			});
		});
	}

};

function globalOnReady() {
	create_tooltip();

	$("input[type=checkbox]:not(.not_skinned)").checkize();
	/* Inline little accordion, Paul update it if necessary to a function */
	$('dl.faq dd').hide();
	$('dl.faq dd.active').show();
	$('dl.faq dt').click(function() {
		$(this).toggleClass('active');
		$(this).next().toggleClass('active').slideToggle('fast');
	})

	/* Classes added to the tables */
	$('table tr td:last-child').addClass('last');
	$('table tr th:last-child').addClass('last');
	$('table tr td div.wrap a.button').hover(
		function() {
			$(this).parent().parent().addClass('hover');
		},
		function() {
			$(this).parent().parent().removeClass('hover');
		}
		);

	/* Sidebar of Statistics focus */
	$('.title .inner .inner2').hover(
		function() {
			$(this).parent().parent().addClass('titlehover');
		},
		function() {
			$(this).parent().parent().removeClass('titlehover');
		}
		);

	/* Safari detection */
	if ($.browser.safari) $('body').addClass('safari');
}

jQuery(document).ready(outlineSelectedFields);
jQuery(document).ready(function() {
	clearInitialValueOnFocus();
	globalOnReady();
});


/*
#TODO:
Need to pass the selector dynamically
Redirect to a url after 8 seconds 
*/
function redirect_to_targeturl(currentsecond, targetURL) {
	if (currentsecond != 1) {
		currentsecond -= 1
		$("#redirect_timer").html(currentsecond);
	}
	else {
		window.location = targetURL
		return
	}
	setTimeout("redirect_to_targeturl(" + currentsecond + ", '" + targetURL + "')", 1000)
}

/* function to create tooltip on click */

function create_tooltip() {
	$(".tooltip_link").each(function(i) {
		$("<div id='tooltip" + i + "' class='tooltip " +
			($(this).is('.tooltip_left') ? 'tooltip-left' : 'tooltip-right') +
			($(this).is('.big') ? ' big' : '') + ($(this).is('.small') ? ' small' : '') +
			($(this).attr('id').length ? ' ' + $(this).attr('id') : '') + "'>" +
			"<div class='tooltip_top'>" +
			"<div class='tooltip_content'>" +
			"<a href='#' class='tooltip_close_button'></a>" +
			$(this).attr('val') +
			"</div>" +
			"</div>" +
			"<div class='tooltip_bottom'></div>" +
			"</div>").appendTo($('body'));

		var tooltip_div = $("#tooltip" + i);

		$(this).click(function(e) {
			var iconOffset = $(this).offset();
			var iconWidth = $(this).width();
			var iconHeight = $(this).height();
			if ($(this).is('.tooltip_left'))
				tooltip_div.css({
					left: (iconOffset.left - tooltip_div.width()) + "px",
					top: (iconOffset.top - 76 + (iconHeight / 2)) + "px"
				});
			else tooltip_div.css({
				left: (iconOffset.left + iconWidth) + "px",
				top: (iconOffset.top - 76 + (iconHeight / 2)) + "px"
			});
			
			if (tooltip_div.is(':visible')) tooltip_div.hide();
			else {
				$('.tooltip:visible').hide();
				tooltip_div.show();
				$(document).one('click', clickFunc);
			}
			return false;
		});

		function clickFunc(e) {
			if ($(e.target).parents('.tooltip').length == 0 && !$(e.target).is('.tooltip_link'))
				tooltip_div.hide();
			else
				$(document).one('click', clickFunc);
		}

		tooltip_div.find('a.tooltip_close_button').click(function() {
			tooltip_div.hide();
			return false;
		});
	});
}

function reset_tooltips() {
	$('.tooltip').hide();
}

$(window).bind('resize', function() {
	$(".tooltip_link").each(function(i) {
		var tooltip_div = $("#tooltip" + i);
		if (tooltip_div.is(':visible')) {
			var iconOffset = $(this).offset();
			var iconWidth = $(this).width();
			var iconHeight = $(this).height();
			if ($(this).is('.tooltip_left'))
				tooltip_div.css({
					left: (iconOffset.left - tooltip_div.width()) + "px",
					top: (iconOffset.top - 76 + (iconHeight / 2)) + "px"
				});
			else tooltip_div.css({
				left: (iconOffset.left + iconWidth) + "px",
				top: (iconOffset.top - 76 + (iconHeight / 2)) + "px"
			});
		}
	});
});

function purge(d) {
	if(typeof d == 'undefined' || null === d) return;
	
	var a = d.attributes, i, l, n;
	if (a) {
		l = a.length;
		for (i = l - 1; i >= 0; i -= 1) {
			n = a[i].name;
			if (n == 'coords') {
				d[n] = '0,0,0,0';
			}
			else if (typeof d[n] == 'function') {
				d[n] = null;
			}
		}
	}
	a = d.childNodes;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i += 1) {
			purge(d.childNodes[i]);
		}
	}
}

function cloneObject(obj) {
	var clone = {};
	for(var i in obj) {
		if(typeof(obj[i])=="object")
			clone[i] = cloneObject(obj[i]);
		else
			clone[i] = obj[i];
	}
	return clone;
}

function arrayClean(arrayObj) {
	for (var i = 0; i < arrayObj.length; i++) {
		arrayObj.splice(i, 1);
		i--;
	}
}

function objectClean(objectObj) {
	for (var key in objectObj) {
		delete objectObj[key];
	}
}

function addCharCounter(elem, max, defaultText) {
	var count_settings = {
		format: "%1",
		delay: 100
	};

	$this = $(elem);
	if ($this.find('.characters').length &&
		$this.find('textarea').length &&
		typeof $this.charCounter != "undefined") {
		count_settings.container = $this.find('.characters')[0];
		$($this.find('textarea').get(0)).charCounter(max, count_settings, defaultText);
	}
}

new function($) {
	$.fn.setCursorPosition = function(pos) {
		if ($(this).get(0).setSelectionRange) {
			$(this).get(0).setSelectionRange(pos, pos);
		} else if ($(this).get(0).createTextRange) {
			var range = $(this).get(0).createTextRange();
			range.collapse(true);
			range.moveEnd('character', pos);
			range.moveStart('character', pos);
			range.select();
		}
	}
}(jQuery);
