$(function() {

    $("#tabs").tabs();

   $(".trigger").tooltip({ effect: 'slide'});

    $('#trynow').click(function() {
        $('#trymebox').show('slide', { direction: "up" }, 400);
    });

    $('.closeme').click(function() {
        $('#trymebox').hide('slide', { direction: "up" }, 400);
    });

    $('.trialkey').click(function() {
        $('#requestBox').fadeIn(250, 'easeInQuart');
    });

    $('.back2').click(function() {
       $('#trymebox').hide('slide', { direction: "up" }, 400);
    });

    $('.back1').click(function() {
       $('#videoDiv').hide('slide', { direction: "up" }, 400);
	   	$("#videoDiv object").remove();
    });

    $('#intro').click(function() {
        $('#videoDiv').show('slide', { direction: "up" }, 400);
        startVideo();
    });



	$('#countrycode').change(function() {
		var prefix = $('#countrycode').val();
	  $('#phonePrefix').html('('+prefix+')');
		$('#phonePrefix').effect('highlight', 'green', 500);
	});

    function startVideo() {
        setTimeout(function() {
            $('#previewV').flash({
                swf: 'video1.swf', 
                width: 776,
                height: 630
            });
        },
        1000);
    }; 

    function checkForm() {
        var res = true;
        var e = $('#fullname');
        var p = e.parent('div');
        if (e.val() == '') {
            p.addClass('error');
			p.effect('shake', 100);
            res = false;
        }
        else {
            p.removeClass('error');
        }

		var e = $('#phone');
        var p = e.parent('div');
        if (e.val() == '') {
            p.addClass('error');
			p.effect('shake', 100);
            res = false;
        }
        else {
            p.removeClass('error');
        }


        var regexp = /..*\@..*\...*/;
        e = $('#email');
        p = e.parent('div');
        if (e.val().search(regexp) == -1) {
            p.addClass('error');
			p.effect('shake', 100);
            res = false;
        }
        else {
          var email = e.val().toLowerCase();
          if (email.indexOf("@gmail.com") != -1 ||
              email.indexOf("@googlemail.com") != -1 ||
              email.indexOf("@yahoo.com") != -1 ||
              email.indexOf("@ymail.com") != -1 ||
              email.indexOf("@rocketmail.com") != -1 ||
              email.indexOf("@hotmail.") != -1) {
              p.addClass('error');
              p.effect('shake', 100);
              alert("Please, enter a corporate email address.");
              res = false;
          }
          else {
            p.removeClass('error');
          }
        }

        return res;
    }

    function submitForm() {
		$("#requestForm").fadeOut(400,
			function() {
				$("#downloadarrow").effect('shake', {direction: 'up', times:7}, 500);
			    $("#downloading").fadeIn(400);
				setTimeout(function() {
				$('#downloading').fadeOut(200);
				$('#trymebox').hide('slide', { direction: "up" }, 600);
				$("#requestForm").show();
				}, 4000);
				// CASPER: Insert download function call here...
				
				var fullname = $("#fullname").val();
				var email = $("#email").val();
				var phone = $("#countrycode").val() + " " + $("#phone").val();
				var gds = $("#gds").val();
				var param = "fullname=" + escape(fullname) + "&email=" + escape(email) + "&phone=" + escape(phone) + "&gds=" + escape(gds);
				$.ajax({
          url: "http://login.speedfares.com/register.aspx?" + param,
          crossDomain: true,
          dataType: "script"
        });
			});
    }

    function OnFailed(error) {
       // Alert user to the error.
       alert(error.get_message());
    }

    $('#submitButton').click(function() {
        if (checkForm())
        {
            submitForm();
        }
    });

	$('#trynow2').click(function() {
        $.scrollTo( '#posterwrapper', 800, {easing:'easeInOutCubic'});
		setTimeout(function() {
		$('#trynow').click()}, 1000);
    });
	
	if(location.hash.substring(1) == "ONEMONTHFREE"){
		$('.days').text("1 month");
		Cufon.set('fontFamily', 'ColaborateLight').replace('h2')('.pitch')('#suppliers a')('h4')('.span1')('.span3')('.back')('h3');
 
		Cufon.set('fontFamily', 'Colaborate-Medium').replace('.pitch strong')('.span2')('.span3 strong')('#submitButton')('#tryspeedfaresBox h3')('#suppliers h4')('#downloadtext');
 
		Cufon.set('fontFamily', 'ColaborateLight').replace('#information h3')('#suppliers h3')('#contactbox h3')('h1');
  
		Cufon('.squib', {
		fontFamily: 'ColaborateLight',
		color: '-linear-gradient(#ffd400, #ffbf00)'
		});
		Cufon.now(); 
	} else{
		Cufon.set('fontFamily', 'ColaborateLight').replace('h2')('.pitch')('#suppliers a')('h4')('.span1')('.span3')('.back')('h3');
 
		Cufon.set('fontFamily', 'Colaborate-Medium').replace('.pitch strong')('.span2')('.span3 strong')('#submitButton')('#tryspeedfaresBox h3')('#suppliers h4')('#downloadtext');
 
		Cufon.set('fontFamily', 'ColaborateLight').replace('#information h3')('#suppliers h3')('#contactbox h3')('h1');
  
		Cufon('.squib', {
		fontFamily: 'ColaborateLight',
		color: '-linear-gradient(#ffd400, #ffbf00)'
		});
	}
	
	function setContactInfo()
	{
		var site = window.location.hostname.toLowerCase();
		if (site.endsWith(".fr"))
		{
			$('#contactbox_com').hide();
			$('#contactbox_fr').show();
		}
		else if (site.endsWith(".co.za"))
		{
			$('#contactbox_com').hide();
			$('#contactbox_za').show();
		}
	}
	
	setContactInfo();
});

String.prototype.endsWith = function(str)
{
	return (this.match(str+"$")==str)
}	

