﻿var index = 1;
var maxIndex = 0;

$(document).ready(function () {
    //CountDown
    if ($('#countdown').length > 0) {
        loadCountDown(_year, _month, _day, _hour, _min, 1);
    }

    //MailForm Validation
    if ($('#MailForm').length > 0) {
        MailFormEvents();
    }

    //Send button style
    $("input[value='Skicka']").css("padding", "0px 10px 0px 10px");

    window.setTimeout(windowReady, 500);
});
function windowReady() {
	var top_margin = 40;
	var c_height = $(".content").height();
	var pos = (500 / 2) - (c_height / 2);

	if (pos < top_margin) {
		pos = top_margin;
	}

	$('.content').animate(
        {
        	'margin-top': pos - top_margin
        },
        {
        	duration: 600,
        	easing: 'swing',
        	complete: function () {
        		if (c_height > 500) {
        			includeScrollingScript();
        		}
        	}
        }
    );

	if (document.getElementById('content_left') && document.getElementById('content_right')) {
		window.setTimeout(slideIn_LeftContents, 500);
		window.setTimeout(slideIn_RightContents, 1400);
	}
}
function includeScrollingScript() {
	var topMargin = 0;
	var pageHeight = 300;

	var c_height = $(".content").height() - (460 - pageHeight);

	var positionHeight = Math.floor(187 / Math.ceil(c_height / pageHeight));

	$('#scrollUp').fadeIn('fast');
	$('#scrollUp').click(
        function () {
        	topMargin = parseInt($('.content').css('margin-top'));
        	if (topMargin < 0) {
        		$('.content').animate(
                    {
                    	'margin-top': (topMargin + pageHeight)
                    },
                    {
                    	duration: 400,
                    	easing: 'swing'
                    }
                );
                    var positionTop = parseInt($('#scrollPosition').css("top")) - positionHeight;
        		$('#scrollPosition').animate(
                    {
                    	'top': positionTop
                    },
                    {
                    	duration: 400,
                    	easing: 'swing'
                    }
                );
        	}
        }
    );
	$('#scrollDown').fadeIn('fast');
	$('#scrollDown').click(
        function () {
        	topMargin = parseInt($('.content').css('margin-top'));
        	if (topMargin > -(c_height - pageHeight)) {
        		$('.content').animate(
                    {
                    	'margin-top': topMargin - pageHeight
                    },
                    {
                    	duration: 400,
                    	easing: 'swing'
                    }
                );
        		var positionTop = parseInt($('#scrollPosition').css("top")) + positionHeight;
        		$('#scrollPosition').animate(
                    {
                    	'top': positionTop
                    },
                    {
                    	duration: 400,
                    	easing: 'swing'
                    }
                );
        	}
        }
    );
	$('#scrollPosition').css("height", positionHeight);
	$('#scrollPosition').fadeIn('fast');
}
function slideIn_LeftContents() {
	var top_margin = 40;
	var c_height = $("#content_left").height();
	var pos = (500 / 2) - (c_height / 2);

	if (pos < top_margin) {
		pos = top_margin;
	}

	$('#content_left').animate(
        {
        	top: pos - top_margin,
        	left: 120
        },
        {
        	duration: 300,
        	easing: 'swing'
        }
    );
}

function slideIn_RightContents() {
	var top_margin = 40;
	var c_height = $("#content_right").height();
	var pos = (500 / 2) - (c_height / 2);

	if (pos < top_margin) {
		pos = top_margin;
	}
	$('#content_right').animate(
        {
        	top: pos - top_margin,
        	left: 310
        },
        {
        	duration: 300,
        	easing: 'swing'
        }
    );
}


function nextAddAir() {

	$('#add_air_' + index).fadeIn('slow');
	if (index < maxIndex) {
		index++;
		window.setTimeout(nextAddAir, 300);
	}


}
function nextArrow() {

	$('#menu_arrow_' + index).show().animate(
        {
        	'left': ((index - 1) * 140) + 35,
        	'top': 30
        },
        {
        	duration: 300,
        	easing: 'swing',
        	complete: function () {
        		if (index < maxIndex) {
        			index++;
        			window.setTimeout(nextArrow, 1200);
        		} else {
        			index = 1;
        			nextAddAir();
        		}
        	}
        }
    );
}


/*
jQuery plugin are: 

easein, 
easeinout, 
easeout, 
expoin, 
expoout,
expoinout, 
bouncein,
bounceout, 
bounceinout, 
elasin, 
elasout, 
elasinout, 
backin, 
backout, 
backinout, 
linear

*/

function Left(str, n) {
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return parseInt(String(str).substring(0, n));
}


/* arrow zoom */

$(document).ready(function () {

	$('.menu_arrow > img').mouseover(function () {

		var _this = $('> span', this.parentNode);
		var _img = $(this);

		$(_img).attr('src', $(_img).attr('src').replace('pil_130', 'pil'));

		$(_img).animate(
		{
			'margin-top': -7,
			'margin-left': -15,
			'width': 160,
			'height': 74

		},
		{
			duration: 100,
			easing: 'swing',
			complete: function () {
				$(_this).css('font-size', '8.5pt').css('top', '22px');
			}
		}
		);
	});

	$('.menu_arrow > img').mouseout(function () {

		var _this = $('> span', this.parentNode);
		var _img = $(this);

		$(_img).attr('src', $(_img).attr('src').replace('pil', 'pil_130'));

		$(_img).animate(
            {
            	'margin-top': 0,
            	'margin-left': 0,
            	'width': 130,
            	'height': 60
            },
            {
            	duration: 100,
            	easing: 'swing',
            	complete: function () {
            		$(_this).css('font-size', '5.5pt').css('top', '25px');
            	}
            }
          );
	});

	maxIndex = $('.menu_arrows_wrap > div').length;

	$('.menu_arrow').hide();
	$('.menu_arrows_wrap > img').hide();

	window.setTimeout(nextArrow, 1500);
});

/*************************************************
FORM VALIDATION
*************************************************/
function MailFormEvents() {
    activateSendButton();

    $("#MailForm").find("input[type='text']:not([name*='Email'])").bind('textchange', function (event, previousText) {
            activateSendButton();
        }
    );
    $("#MailForm").find("input[name*='Email']").bind('textchange', function (event, previousText) {
            activateSendButton();
        }
    );
    $("#MailForm").find("input:radio").click(
        function () {
            activateSendButton();
        }
    );
}
function activateSendButton() {
    var valid = true;

    $("#MailForm").find("input[type='text']:not([name*='Email'])").each(
        function () {
            if (validateEmptyTextBox(this)) {
                $(this).removeClass('InValid').addClass('Valid');
            } else {
                valid = false;
                $(this).removeClass('Valid').addClass('InValid');
            }
        }
    );

    $("#MailForm").find("input[name*='Email']").each(
        function () {
            if (validateEmail(this)) {
                $(this).removeClass('InValid').addClass('Valid');
            } else {
                valid = false;
                $(this).removeClass('Valid').addClass('InValid');
            }
        }
    );

    if ($("#MailForm").find("input:radio").length > 0) {
        if ($("#MailForm").find("input:radio:checked").length == 0) {
            valid = false;
            $(this).removeClass('Valid').addClass('InValid');
        } else {
            $(this).removeClass('InValid').addClass('Valid');
        }
    }

    if (valid) {
        $("#MailForm").find("input[type='submit']").removeAttr('disabled');
    } else {
        $("#MailForm").find("input[type='submit']").attr('disabled', 'disabled');
    }
    
}

function validateEmptyTextBox(target) {
    var value = target.value;
    if (value.length > 1) {
        return true;
    } else {
        return false;
    }
}
function validateEmail(target) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = target.value;
    if (reg.test(address) == false) {
        return false;
    } else {
        return true;
    }
}

/************************************************
COUNTDOWN
*************************************************/
function loadCountDown(year, month, day, hour, minute, format) {
    countdown(year, month, day, hour, minute, format);

    var contentHeight = $('#countdown').height();
    var windowHeight = $(window).height();
    var marginTop = ($(window).height() / 2) - 150;
    $('#countdown').css('margin-top', marginTop + 'px');
}
function countdown(year, month, day, hour, minute, format) {
    Today = new Date();
    Todays_Year = Today.getFullYear() - 2000;
    Todays_Month = Today.getMonth();

    //Convert both today's date and the target date into miliseconds.                           
    Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                                Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
    Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();

    //Find their difference, and convert that into seconds.                  
    Time_Left = Math.round((Target_Date - Todays_Date) / 1000);

    if (Time_Left < 0)
        Time_Left = 0;

    var innerHTML = '';

    switch (format) {
        case 0:
            //The simplest way to display the time left.
            innerHTML = Time_Left + ' seconds';
            break;
        case 1:
            //More datailed.
            days = Math.floor(Time_Left / (60 * 60 * 24));
            Time_Left %= (60 * 60 * 24);
            hours = Math.floor(Time_Left / (60 * 60));
            Time_Left %= (60 * 60);
            minutes = Math.floor(Time_Left / 60);
            Time_Left %= 60;
            seconds = Time_Left;

            dps = 's'; hps = 's'; mps = 's'; sps = 's';
            //ps is short for plural suffix.
            if (days == 1) dps = '';
            if (hours == 1) hps = '';
            if (minutes == 1) mps = '';
            if (seconds == 1) sps = '';

            innerHTML = days + ' day' + dps + ' ';
            innerHTML += hours + ' hour' + hps + ' ';
            innerHTML += minutes + ' minute' + mps + ' and ';
            innerHTML += seconds + ' second' + sps;
            break;
        default:
            innerHTML = Time_Left + ' seconds';
    }

    if (days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        document.location.replace('http://www.silvent.com/www/live/special/landingpage.aspx?TreeID=839')
    }
    else if (days == 0 && hours == 0 && minutes == 0 && seconds <= 10) {
        innerHTML = seconds;
        document.getElementById('countdown').innerHTML = innerHTML;
        setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
        $('#countdown').fadeIn(0);
        $('#countdown').css('font-size', '0px');
        $('#countdown').animate
        (
		    {
		        'font-size': 200
                //'opacity': 0
		    },
		    {
		        duration: 200,
		        easing: 'swing',
		        complete: function () {
                    //COMPLETE
		        }
		    }
		).delay(400).fadeOut(400);
		
    
    } else {
        document.getElementById('countdown').innerHTML = innerHTML;
        //Recursive call, keeps the clock ticking.
        setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
    }
}
