//Execute functions at startup
$(document).ready(function () {
    checkboxesReplace();
    replaceBlockQuotes();
	helperInitialize();
	validationChecks();
});

//Replace blockquotes with images
function replaceBlockQuotes() {
	$('.quote').each(function () { 
		$(this).html('<img src="'+baseurl+'images/blockquote-open.gif" alt="blockquote">' + $(this).html() + '<img src="'+baseurl+'images/blockquote-close.gif" alt="blockquote">');
	});
}

function checkboxesReplace() {
	$('input:checkbox').each(function () {
		var checkbox = $(this);
		checkbox.hide();

		if (checkbox.val() == "1")
			checkbox.attr('checked', true);
		
		
		var className = checkbox.attr('class').split(" ");
		checkbox.wrap('<span class="' + (className.length == 2 ? checkbox.attr('class') : "checkbox plain") + (checkbox.is(':checked') ? "-checked" : "") + '"></span>');
		checkbox.parent().click(function () {
			checkboxOnChange(checkbox);
			return false;
		});

		checkbox.unbind('click');
		/*
		checkbox.click(function(event){
			//checkboxOnChange(checkbox, null);
			event.stopPropagation();
		});
		*/
	});
}

function checkboxOnChange(checkbox, check) {
	var setChecked = !checkbox.is(':checked');
	
	if (check != null) {
		setChecked = check;
	}

	var className = checkbox.attr('class').split(" ");
	checkbox.attr('checked', setChecked);
	checkbox.parent().attr('class', (className.length == 2 ? checkbox.attr('class') : "checkbox plain") + (setChecked ? "-checked" : ""));
	checkbox.change();
	
}

//Setup helper text balloons for form
function helperInitialize() {

    $('body').append('<div id="helper"><div class="helper-header"><div class="helper-content"><p>Help</p></div><div class="helper-corner-r-t">&nbsp;</div></div><div class="helper-corner-l-b"></div><div class="helper-corner-r-b"></div></div>');

    $('input:visible').each(function () {
        if ($(this).attr('title') || $(this).attr('alt')) {
            $(this).focus(helperShow);
            $(this).blur(helperHide);
        }
    });

    $('.helpertext').each(function () {
        if ($(this).attr('title') || $(this).attr('alt')) {
            $(this).mousemove(helperShow);
            $(this).mouseleave(helperHide);
        }
    });
}

//Postition the helper text balloon on the right position
function helperShow(event) {
    var elementPos = $(this).offset();
    var text = ($(this).attr('alt') ? $(this).attr('alt') : $(this).attr('title'));
    if ($(this).attr('title'))
        $(this).attr('title', '');

    if (text != null && text.length > 0) {
        $('#helper').attr('alt', text);

        //$('#console').html($('#helper .helper-content').height());

        $('#helper .helper-content').html("<p>" + text.replace(/'/, "\'") + "</p>");
    }

    //$('#helper .helper-header').css('height', ($('#helper').height() - 25) + 'px');
    //$('#helper .helper-content').css('width', ($('#helper').width() - 15) + 'px');
    //$('#helper .helper-corner-r-b').css('width', ($('#helper').width() - 15) + 'px');

    elementPos.top -= $('#helper').height();

    $('#helper .helper-corner-r-t').css('height', $('#helper .helper-content').height());

    if (!$(this).is('input'))
        elementPos.left = event.pageX - $('#helper').width() + 30;
       
    $('#helper').css('top', elementPos.top);
    $('#helper').css('left', elementPos.left);
    $('#helper').stop(true, true).fadeIn(0);
}

//Hide helper
function helperHide() {
    $('#helper').fadeOut(0);
    $(this).attr('title', $('#helper').attr('alt'));
}

function showDefaultOverlay(message) {
    $.blockUI({
        message: message
    });
    $('.blockOverlay').attr('title','Click to close').click($.unblockUI);
}

//Show overlay during payment
function showOverlay(text) {
    $.blockUI({
        message: '<br /><h1>' + text + '</h1>'
    });
}

function validateURL(url) {
	if (url.length == 0) {
		return true;
	} else if (url.substring(0, 7) == "http://") {
		var parts = url.split(".");
		if (parts.length >= 2 && parts[parts.length - 1].length >= 2) {
			return true;
		}
	}
	return false;
	/*
	var RegexUrl = new RegExp();
	RegexUrl.compile("^(http|https)://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
	return RegexUrl.test(url);
	*/
}


//Change backgroundcolor of element using color array
var iBlink = 0;
var blinkColorArray = ['#F2FAD2', '#EFF8CA', '#ECF6C2','#E6F3B1','#E0F0A0','#DAED8F','#D4EA7E','#CFE76D','#C9E45C','#C3E14B','#BDDE3A','#B7DA2A'];

//Start timer to change colors of an element
function blink(elementid){
    setInterval ("changeBackgroundColor('"+elementid+"')",100);
}

function changeBackgroundColor(id) {
	$('#' + id).css('backgroundColor', blinkColorArray[iBlink]);
	if (iBlink == 10){
		iBlink = 0;
		colorArray = blinkColorArray.reverse();
	} else {
		iBlink++;
	}
}

function validationChecks() {
	$('.validate-empty').each(function(){
		$(this).keyup(function(){
			if ($(this).val().length < 2)
				$(this).prev().css('color', 'red');
			else
				$(this).prev().css('color', '');
		});
	});

	$('.validate-password').each(function(){
		$(this).keyup(function(){
			if ($(this).val().length < 4)
				$(this).prev().css('color', 'red');
			else
				$(this).prev().css('color', '');
		});
	});

	$('.validate-email').each(function(){
		$(this).keyup(function(){
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if (!reg.test($(this).val()))
				$(this).prev().css('color', 'red');
			else
				$(this).prev().css('color', '');
		});
	});
}

/**
 * DEPRECATED JAVASCRIPT
 */

//Submit form without confirmation
function submitForm(option){
	$('#option').val(option);
	$('#form').submit();
}

//Submit form with confirmation
function submitConfirmForm(option, caption){
	caption = caption.replace("[return]", "\n");
	var agree=confirm(caption);
	if (agree){
		$('#option').val(option);
		$('#form').submit();
	}
}
