/*!
 * amongtheants.com
 * Version: 1.0
 * Updated: 07-02-2010
 * http://www.amongtheants.com/
 *
 * Copyright (c) 2010 paramana.com
 *
 */

var webStart = {
    externalLinks: function(){
        $("a.externalLink").livequery(function(){
            $(this).attr("target","_blank");
        });
    },
	elementsProps: function(){
		$('.cover').livequery(function(){
			$(this).css('opacity', '0.5');
		});
	},
    contactForm: function(){
        var _self = this;
        $('.error').livequery(function(){
            $(this).hide();
        });
        $("#sendContact").livequery(function(){
            var $self = this;
            $(this).click(function() {
                if(!_self.contactStart) {
                    //$(this).css('')
                    // validate and process form
                    // first hide any error messages
                    $('.error').hide();
                    
                    var name = $("input#nameContact").val();
                    if (name == "" || name == "your name here..") {
                        $("#nameContactError").fadeIn('slow');
                        $("input#nameContact").focus().selectRange(0, 0);
                        return false;
                    }
                    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                    var email = $("input#emailContact").val();
                    if ((email == "" || email == "your email here..")) {
                        $("#emailContactError").fadeIn('slow');
                        $("input#emailContact").focus().selectRange(0, 0);
                        return false;
                    }
                    else if (!emailReg.test(email) && !(email == "") && !(email == "your email here..")) {
                        $("#emailContactWrong").fadeIn('slow');
                        $("input#emailContact").focus();
                        return false;
                    }
                    var message = $("textarea#messageContact").val();
                    if ((message == "" || message.length < 3)) {
                        if(message.length < 3)
                            $("#messageContactShort").fadeIn('slow');
                        else
                            $("#messageContactError").fadeIn('slow');
                        $("textarea#messageContact").focus().selectRange(0, 0);
                        return false;
                    }
                    var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
                    $.ajax({
                        type: "POST",
                        url: 'http://www.amongtheants.com/post.php',
                        data: 'section=contactform&' + dataString,
                        beforeSend: function() {
                            _self.contactStart = true;
                            document.getElementById('disableSendContact').style.display = 'block';
                            $($self).addClass('disabledBtn').val('Sending...');
                        },
                        success: function() {
                            _self.contactStart = false;
                            document.getElementById('disableSendContact').style.display = 'none';
                            if(window.location.pathname.indexOf('en') > -1)
								$('#contactForm').html("<div id='feedback' class='note'><h3>Your message has been send!</h3><p>We will contact you soon.</p></div>");
							else
								$('#contactForm').html("<div id='feedback' class='note'><h3>Το μήνυμά σας εστάλη!</h3><p>Θα επικοινωνήσουμε σύντομα μαζί σας.</p></div>");
                        }
                    });
                }
                return false;
            });
        });
        $("input#nameContact").livequery(function(){
            $(this).keypress(function (){
                $("#nameContactError").fadeOut('slow');
            });
            $(this).blur(function() {
                $("#nameContactError").fadeOut('slow');
            });
        });
        $("input#emailContact").livequery(function(){
            $(this).keypress(function (){
                $("#emailContactError").fadeOut('slow');
                $("#emailContactWrong").fadeOut('slow');
            });
            $(this).blur(function() {
                $("#emailContactError").fadeOut('slow');
                $("#emailContactWrong").fadeOut('slow');
            });
        });
        $("textarea#messageContact").livequery(function(){
            $(this).keypress(function (){
                $("#messageContactShort").fadeOut('slow');
                $("#messageContactError").fadeOut('slow');
            });
            $(this).blur(function() {
                $("#messageContactShort").fadeOut('slow');
                $("#messageContactError").fadeOut('slow');
            });
        });
    },
    init: function(){
        this.externalLinks();
        this.contactForm();
		this.elementsProps();
    }
};

$(document).ready( function(){
    webStart.init();
    var mainHeight   = $('#main').height()+ 50;
    var sideBarHeigth = $('#sidebar').height();
    var sideBarInHeight = $('#sidebarIn').height();
    if(mainHeight < sideBarHeigth)
        {$('#main').height(sideBarHeigth);}
    else if (mainHeight > sideBarHeigth)
        {$('#sidebar').height(mainHeight);}
    else {}
    
    if(mainHeight < sideBarInHeight)
        {$('#main').height(sideBarInHeight);}
    else if (mainHeight > sideBarInHeight)
        {$('#sidebarIn').height(mainHeight + 1);}
    else {}
    
    if(sideBarInHeight < sideBarHeigth)
        {$('#sidebarIn').height(sideBarHeigth + 1);}
    else if (sideBarInHeight > sideBarHeigth)
        {$('#sidebar').height(sideBarInHeight);}
    else {}

});

$(window).load(function () {
	$("#siteLoading").fadeOut('normal', function (){
		$(this).remove();
		document.getElementById('warper').style.display = 'block';
	});
	if($('#contactForm').length > 0) {
		$('#nameContact').clearFields("active", "typing");
		$('#emailContact').clearFields("active", "typing");
		$('#messageContact').clearFields("active", "typing");
	}
});

//------------------------------------------------//
//---------------functions------------------------//
//------------------------------------------------//
if (location.protocol != "file:" && location.host != 'localhost') {
    window.onerror = function(message, file, line){
        errorHandler(message + '\n in line: ' + line + ' located in file: ' + file);
        return true;
    }
}

// this is suppose to display the error messages in the user.
function errorHandler (error){
    if (location.protocol == "file:" && location.host == 'localhost')
        alert(error);
}

/*
 * Creates the input fields effect
 * @param string The class to give in the focused field
 * @param string optional if you want to have a specific value other than the one in the value=""
 */

(function($) {
    $.fn.clearFields = function (focusClass, typingClass, defaultValue) {
        if(!defaultValue)
            defaultValue = defaultValue || $(this).data('value') || $(this)[0].defaultValue;
		if ($(this).val() != "" && $(this).val() != defaultValue) {
			$(this).addClass(typingClass);}
        $(this).focus(function() {
            if ($(this).val() === "" || $(this).val() === defaultValue) {
                $(this).selectRange(0, 0);
                $(this).addClass(focusClass);
            }
        }).blur(function() {
            // restore to the default value if current value is empty
            if ($(this).val() === "" || $(this).val() === defaultValue) {
                $(this).val(defaultValue);
                $(this).removeClass(focusClass);
            }
            // if focusClass is set, remove class
            if (focusClass) {
                $(this).removeClass(focusClass);
            }
        })
        .keydown(function(){
            // clear value if current value is the default
            if ($(this).val() === defaultValue) {
                $(this).val("");
                $(this).removeClass(typingClass);
            }
            // if focusClass is set, add the class
            if (focusClass) {
                $(this).addClass(typingClass);
            }
        });
    };
})(jQuery);
/*
 * Puts cursor in a position if the second param is 0 otherwise selects the text between the two positions
 * @param int The start position
 * @param int The end postion
 */
$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if(this.setSelectionRange && $(this).attr('type') != 'radio') {
            this.focus();
            this.setSelectionRange(start, end);
        }
        else if(this.createTextRange && $(this).attr('type') != 'radio') {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};
