// anti frames protection
if (top.frames.length > 0) {
	try {
		var parentLocation = parent.location.href;
	} catch(err) {}
	if (typeof parentLocation == "undefined") {
		top.location = document.location;
	} else {
		var parentLocationSplitTab = parentLocation.split('/');
		var selfLocationSplitTab = document.location.href.split('/');
		parentLocationSplitTab.length=3;
		selfLocationSplitTab.length=3;
		var parentLocationDomain = parentLocationSplitTab.join('/');
		var selfLocationDomain = selfLocationSplitTab.join('/');
		if (parentLocationDomain != selfLocationDomain) {
			top.location = document.location;
		}
	}
}

// spanButton feature
function spanButton(color, size, text) {
	return "<span class=\""+color+"\"><span><span><span class=\""+size+"\">"+text+"</span></span></span></span>";
}

//textarea remainings chars
var maxChars = Array();
$(document).ready(function() {
	$("textarea").each( function() { 
		if ($(this).attr('id') && $(this).attr('id').length>0 && $("#"+$(this).attr('id')+"RemainingChars").length > 0) {
			maxChars[$(this).attr('id')]=$("#"+$(this).attr('id')+"RemainingChars").html();
			remainingChars($(this));
			$(this).keyup( function() { 
				remainingChars($(this)); 
			});
		}
	});
});

// remainingChars feature
function remainingChars(textarea) {
	var max = 0 + maxChars[textarea.attr('id')];
	var remaining = 0 + maxChars[textarea.attr('id')] - textarea.val().length;
	if(remaining <= 0) {
		textarea.val(textarea.val().substring(0, max));
		remaining = 0;
	}
	$("#"+textarea.attr('id')+"RemainingChars").html(""+remaining);
}

// a.submit feature duplicated in pagination.js
$(document).ready(function() {
	$("a.submit").attr("href","#");
	$("a.submit").click(function() {
		$(this).parents().filter('form').submit();
	});
});

// customDropDown
$(document).ready(function() {
	$(".showCustomDropDown").click(function() {
		var prevCustomDropDown = $(this).prev(".customDropDown");
		$(".customDropDown").not(prevCustomDropDown).hide();
		prevCustomDropDown.toggle();
		var maxLiWidth=0;
		prevCustomDropDown.children("ol").children("li").each(function() {
			if (maxLiWidth<$(this).parent().width()) {
				maxLiWidth=$(this).parent().width();
			}
		});
		if (!$(this).hasClass("resized") && $(this).width()>maxLiWidth && prevCustomDropDown.is(":visible")) {
			prevCustomDropDown.children("ol").width($(this).width());
			$(this).addClass("resized");
		}
		return false;
	});
});

// language selector
$(document).ready(function() {
	$(".languageSelector a").click(function() {
		var currentLocation = document.location.href;
		var anchor = "";
		if (currentLocation.indexOf("#") != -1) {
			anchor = currentLocation.substring(currentLocation.indexOf("#"), currentLocation.length);
			currentLocation = currentLocation.substring(0, currentLocation.indexOf("#"));
		}
		if (currentLocation.indexOf("?")==-1) {
			document.location.href = currentLocation+"?lang="+$(this).attr("href").substring(1)+anchor;
		} else {
			document.location.href = currentLocation+"&lang="+$(this).attr("href").substring(1)+anchor;
		}
		return false;
	});
});



