	
// Preload images + add non-standard attributes
$(document).ready(function(){
    $.preloadCssImages(); // preload the images; takes a little longer up-front, but ensures everything displays consistently
    //$('#entryform').populateFromQuery(['subscriber_firstname', 'subscriber_lastname', 'email', 'subscriber_state', 'subscriber_phone']); // fill out the entry form fields based on the incoming query
    $('#firstname, #lastname, #phone, #email, #state, #confirm').attr('accept', 'true'); // mark all of those fields as required
    $('#firstname').attr('defval', 'First Name'); // set default value for validation script to ignore
    $('#lastname').attr('defval', 'Last Name'); // set default value for validation script to ignore
    $('#email').attr('mask', 'email'); // mark as requiring valid email addresses
    $('#phone').attr('mask', 'phone'); // mark as requiring phone data
    $('.external').attr('target', '_blank'); // open these links in new window on click

	// Clears Input Fields
    $('.clearField').bind('focus', function(){
        if( this.value == this.defaultValue ) {
            this.value = "";
        }
    });
    $('.clearField').bind('blur', function(){
        if( !this.value.length ) {
            this.value = this.defaultValue;
        }
    });

});

function animationSequence(elementID) {
	//var element = $(elementID);
	i = 0;  
	// If using jQuery 1.4, you don't need to do || [].  
	(function() {  
	$(elementID[i++] || []).fadeTo(750,1, arguments.callee);
	
	})();
}

$(window).bind('load', function() {
    $('#loader').fadeOut('slow'); // fade out the loading indicator

    if($.browser.msie){ // Hide Select field in IE on transition, this works around an IE 6 form element display bug
        setTimeout(function () { $("#state").css({visibility: 'visible'}) }, 1800);

			
    } else {
        $('#state').css({visibility: 'visible'});
		$('#stars .starimg').css({opacity: 0.7});
    }


	$('#entryform').formValidation({
        alias: 'title',
        phone: /[^a-zA-Z]/,
        required: 'accept',
        err_list: true,
        callback: function () {
            // Hide Select field in IE on transition
            if($.browser.msie){
				$('#entryform').trigger('validated'); // trigger the TaguchiMail submit process for the entry form
                $("#state").css({ visibility: 'hidden' });
				
				$('#boarding-text, #entryform').hide(500, function(){
					$('#boarding-text-thanks, #boarding-pass-bottom-thanks').show('fast');
					showDestinationImage("star9");
					//$('#slideshow').css('left','-9999px');
				});
				
				
							
            } else {
				$('#entryform').trigger('validated'); // trigger the TaguchiMail submit process for the entry form
				$('#boarding-text, #entryform').fadeOut(1000, function(){
					$('#boarding-text-thanks, #boarding-pass-bottom-thanks').fadeIn(1000);
						showDestinationImage("star9");
						//$('#slideshow').css('left','-9999px'); 		
				});
		
			
		}
		
	    try {
		var pageTracker = _gat._getTracker("UA-196595-5");
		pageTracker._trackPageview('/win/thankyou');
	    } catch(err) {}

            return !$.fn.tmform.defaults.debug; // if debug, don't submit the form; otherwise, do
        }
    });
	
	$('#boarding-pass-bottom-thanks').hide();
	$('#boarding-pass-top #boarding-text-thanks').hide();
	
	//hide all the lines on start up
	function hideAll() {	
		$('ul#stars li img').each (function (e) {
	
			$('#'+this.id)
			.css({
				left: '-9999px'
			});			
		});
		
		$('#destination').hide();
	}

	hideAll();
	
	var starLines;		
	$('#stars li img.starimg').hover(
	  function () {
	    starLines = $('#'+$(this).parent().attr('id')+'line');
		if($.browser.msie) {
			$('#slideshow').css('left','-9999px');
			$(this).css('opacity','1');
			starLines.css('left','').show(1000);
			showDestinationImage($(this).parent().attr('id'));
			
		} else {
			$('#slideshow').css('left','-9999px');
			$(this).css('opacity','1');
			starLines.css('left','').fadeTo(1000,1);
			showDestinationImage($(this).parent().attr('id'));
		}
	  }, 
	  function () {
		if($.browser.msie) {
			$('#destination').hide();
			$('#slideshow').css('left','');
			$(this).css('opacity','1');
			starLines.css('left','-9999px');
			
		
		} else {
			$('#destination').hide();
			$('#slideshow').css('left','');
	    	starLines.fadeTo(0,0, function(){
			starLines.css('left','-9999px');
			
			});
			
			
		}
	  }
	);
		
	$('#destination li').each (function () {		
		$(this).hide();
	})
		
	function showDestinationImage(imageID){
	
		switch(imageID) {		
			case "star1":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star2":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star3":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star4":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star5":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star6":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star7":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star8":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			case "star9":
			$('#destination').show();
			for(i=0;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#slideshow').css('left','-9999px');
			$('#destination li#destination-'+imageID).show('fast');
			break;
			
			default:
			$('#destination').show();
			for(i=1;i < 9; i++){
				$('#destination li#destination-star'+(i+1)).hide();
			}			
			$('#destination li#destination-'+imageID).show('fast');
		}
		
	}
	
	
	//trigger loading of divs
	if($.browser.msie){ 
		$('#stars .starimg').css({opacity: 1});
			$('#boarding-pass-bottom,#boarding-pass-curve').show('slow');
			$('#boarding-pass-top #boarding-text').show('fast', function() {
				this.style.removeAttribute('filter');
			});

			$("#boarding-pass-top, #boarding-pass-bottom").css({display: 'block'})

		    $("#header, #boarding-text, #entryform, #frame, #stars, #slideshow") // animation to run on page load; show the logos at the top
			       .css({ display: 'block' });

			$('#header').show('slow');
			$('#boarding-pass-top, #boarding-pass-bottom, #boarding-pass-curve').show();
			animationSequence($("#entryform, #frame, #stars, #slideshow, #boarding-pass-curve"));
			
			
			
	} else {
		$('#boarding-pass-bottom,#boarding-pass-curve').show('slow');
		$('#boarding-pass-top #boarding-text').show('slow');
		
		$("#boarding-pass-top, #boarding-pass-bottom").css({opacity: 0.5, display: 'block'})

	    $("#header, #boarding-text, #entryform, #frame, #stars, #slideshow") // animation to run on page load; show the logos at the top
		       .css({ opacity: 0.1, display: 'block' });

		$('#header').delay(300).fadeTo(1000,1);
		$('#boarding-pass-top, #boarding-pass-bottom, #boarding-pass-curve').fadeTo(1500,1);

		animationSequence($("#boarding-text, #entryform, #frame, #stars, #slideshow"));
		
	}
	
    // Update the TaguchiMail plugin defaults with the correct values for this campaign
    $.fn.tmform.defaults = $.extend($.fn.tmform.defaults, {
        instance: 'webjet.taguchimail.com', // set to the client's instance name (<instance>.taguchimail.com)
        campaign_id: 53, // campaign ID of the viral campaign, refer to TaguchiMail Admin > Campaigns
        list: 'newsletter.nz', // list ID of the destination list for competition entrants, refer to TaguchiMail Admin > Subscribers > Lists
        debug: false, // to prevent data being saved to TaguchiMail, change this to true
        bind: 'validated' // remove this line (and the comma on the line above) if no form validation is used
    });

    $('#entryform').tmform({ 
        type: 'enter', 
        enter: { 
            event_id: 'e', // ID of the entry event, refer to TaguchiMail Admin > Campaigns > Tracked Events, and look up the ID of the 'Entered' event (create one if necessary)
            activity_id: 1815  // ID of the TaguchiMail Activity to send to the entrant, refer to TaguchiMail Admin > Campaigns > (name of viral campaign), and insert the ID of the Entry Confirmation message
        }
    });
});

$(document).ready(function(){
	//Activate the Slideshow
	$('#slideshow').innerfade({
		speed: 'slow',
		timeout: 4000,
		containerheight: '272px'
	});
	
	// PNG Fix
	if($.browser.msie){ 
		//DD_belatedPNG.fix('#star1line,#star2line,#star3line,#star4line,#star5line,#star6line,#star7line,#star8line');
	}
});


