$(document).ready(function() {
    init();
});

function init() {
    allCategories();
    validateBottomBoxes();
    $('input[type=text]').clearDefault();
    tabs();
    $('.content-box .contact-left-content a.small-button').click(function(){
    	var company = $('.contact-left-content input').eq(0).val();
	var contactPerson = $('.contact-left-content input').eq(1).val();
	var phone = $('.contact-left-content input').eq(2).val();
	var email= $('.contact-left-content input').eq(3).val();
	var travelTo = $('.contact-left-content select').val();
	var nrOfPersons = $('.contact-left-content #slider-amount').html();
	var dateToTravel = $('#txtUtresa').val();
	var dateForHome = $('#txtHemresa').val();
	var theme = $('.contact-left-content input').eq(6).val();
	var range = $('.contact-left-content #range-amount').html();
	sendOffert(company , contactPerson , phone , email,travelTo , nrOfPersons , dateToTravel , dateForHome , theme , range , " ");
return false;
	});

	shareOnFacebook();
	shareOnTwitter();
}

function tabs() {
    $('div.contact-left-content div.inner').hide();
    $('div.inner').first().show();		

    $('ul.tabs li').click(function() {
        var i = $(this).index();
        $(this).parent().parent().parent().find('.inner').hide();
        $(this).parent().parent().parent().find('.inner').eq(i).fadeIn(350);

        $(this).siblings('li').removeClass('selected');
        $(this).addClass('selected').append('<div class="arrow"></div>');
    }); 
}

function allCategories() {
    $('ul.all-categories li').hover(function() {
        $(this).children('h3').css('color', '#d3118c');
        $(this).children('img').stop().animate({
            marginTop: 10
        }, 200, 'easeOutCirc');
        
        $(this).children('img').css('border', '1px solid #d3118c');

    }, function() {
        $(this).children('h3').css('color', '#858585');

        $(this).children('img').stop().animate({
            marginTop: 15
        }, 200);
        $(this).children('img').css('border', '1px solid #fff');
    });

    $('ul.all-categories li').click(function() {
        var linkUrl = $(this).attr('link');
        window.location = linkUrl;
    });
}

$.fn.clearDefault = function() {
    return this.each(function() {
        var default_value = $(this).val();
        $(this).focus(function() {
            if ($(this).val() == default_value) $(this).val("");
        });
        $(this).blur(function() {
            if ($(this).val() == "") $(this).val(default_value);
        });
    });
};

function validateBottomBoxes() {
    $('div.tipfriend-container a#submitTipFriend').click(function(e) {
        e.preventDefault();
        $txtTipFriend = $('input#txtTipFriend').val();
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

        if ($txtTipFriend == null || !emailPattern.test($txtTipFriend)) {
            $('input#txtTipFriend').val("Ogiltig epostadress!");
            $('input#txtTipFriend').addClass("error");
            return false;
        }
        else {
            sendTipFriend($txtTipFriend,'input#txtTipFriend');
        }

    });

    $('div.newsletter-container a#submitGetNewsletter').click(function(e) {
        e.preventDefault();
        $txtGetNewsletter = $('input#txtGetNewsletter').val();
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

        if ($txtGetNewsletter == null || !emailPattern.test($txtGetNewsletter)) {
            $('input#txtGetNewsletter').val("Ogiltig epostadress!");
            $('input#txtGetNewsletter').addClass("error");
            return false;
        }
        else {
            getNewsLetter($txtGetNewsletter);
        }

    });

	$('div.tipfriend-container a#submitTipFriend2').click(function(e) {
        e.preventDefault();
        $txtTipFriend = $('input#txtTipFriend2').val();
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

        if ($txtTipFriend == null || !emailPattern.test($txtTipFriend)) {
            $('input#txtTipFriend2').val("Ogiltig epostadress!");
            $('input#txtTipFriend2').addClass("error");
            return false;
        }
        else {
            sendTipFriend($txtTipFriend,'input#txtTipFriend2');
        }

    });



}

function sendTipFriend(email , element) {

    $.ajax(
        {
            type: 'POST',
            url: "services.asmx/TipaFriend",
            success: function(data) {
                !data.d.error
                ? $(element).val("Skickat!").addClass('sent') 
                : $(element).val("Något gick fel.").addClass('error');
            },
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{email: '" + email + "', send: true}"
        });
    
}

function getNewsLetter(email) {
    $.ajax(
        {
            type: 'POST',
            url: "services.asmx/Newsletter",
            success: function(data) {
                !data.d.error
                ? $('input#txtGetNewsletter').val("Skickat!").addClass('sent') 
                : $('input#txtGetNewsletter').val("Något gick fel.").addClass('error');
            },
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{email: '" + email + "'}"
        });
}

function sendOffert(Company, ContactPerson, Phone, Email ,TravelTo, NumberOfPersons, DateForTravel, DateForHome, Theme, BudgetPerPerson, ExtraField)
{
	var offering = 
	{"Company": Company,
 	"ContactPerson": ContactPerson,
	"Phone": Phone,
 	"Email": Email ,
        "TravelTo": TravelTo,
        "NumberOfPersons": NumberOfPersons,
        "DateForTravel": DateForTravel,
        "DateForHome": DateForHome,
        "Theme": Theme,
        "BudgetPerPerson": BudgetPerPerson,
       	"ExtraField": ExtraField};

	var test =  JSON.stringify(offering);
	var sendThis = "{ offeringData:" + test + " }";

	$.ajax(
        {
            type: 'POST',
            url: "services.asmx/RequestOffering",
            success: function(data) {
                $('div.inner').hide();
		$('div.thankyou').fadeIn(350);
            },
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: sendThis
        });
}

function shareOnFacebook() {
    
    var URL = "http://www.facebook.com/sharer.php?u="+ window.location+ "&t=Event Tours!";
    $('a.facebook-sharer').click(function(e) {
        e.preventDefault();
        var facebookwindow = window.open(URL, 'Dela Event Tours på Facebook!', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=850,height=550, left=300, top=200');
    });
}

function shareOnTwitter() {

    var URL = "http://twitter.com/home?status=" + window.location;
    $('a.twitter-sharer').click(function(e) {
        e.preventDefault();
        var facebookwindow = window.open(URL, 'Dela Event Tours på Twitter!', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=850,height=550, left=300, top=200');
    });
}