var old_id = 1;

/* Configs */
var hiRezImageString="1920x1080";
var lowRezImageString="1280x720";
var first = 0;
var speed = 700;
var pause = 3500;
var start = false;

var gal_width = 630;
var gal_height = 330;



function ShowVideo(){
	$(".photosContent").hide();
	$(".photos").hide();
	
	$(".flashContent").show();
	$(".videos").show();
	$(".video_tab").removeClass("type_unselected").addClass("type_selected");
	$(".photo_tab").removeClass("type_selected").addClass("type_unselected");
	$(".play_pause").hide();
}
function ShowPhoto(){
	$(".flashContent").hide();
	$(".videos").hide();
	
	$(".photosContent").show();
	$(".photos").show();
	$(".play_pause").show();
}
function StartScroll(){
	/* TEXT TICKER */
	$('#news_ticker').innerfade({
		animationtype: 'slide',
		speed: 750,
		timeout: 4000,
		type: 'random',
		containerheight: '1em'
	});
}
/* DOCUMENT READY */
$(document).ready(function() {  
	var area = "64";
	if($.browser.safari){
		$(".download_wrapper").css("height","21px");
	}
	$(".small_block").hover(function(){  $(".wallpaperResolution").html(lowRezImageString);  }, function(){ $(".wallpaperResolution").html(""); });
	$(".large_block").hover(function(){  $(".wallpaperResolution").html(hiRezImageString);  }, function(){ $(".wallpaperResolution").html(""); });
	$(".sponsorImage").hover(function(){
		$(".Note").html($(this).attr("alt"));
		$("#DetailsNote").html($(this).attr("alt"));
	}, function(){
		$("#DetailsNote").html("This is a note. The note will change on Hover on the sponsors.");
		$(".Note").html("This is a note. The note will change on Hover on the sponsors.");
	});
	
	
	$(".build64").show();
	/* GENERAL - USE THIS */
	$(".type").click(function(){
		$(".type").removeClass("type_selected").addClass("type_unselected");
		$(this).removeClass("type_unselected").addClass("type_selected");
	});
	
	$(".build").click(function(){
		$(".build").removeClass("build_selected").addClass("build_unselected");
		$(this).removeClass("build_unselected").addClass("build_selected");
		ShowVideo();
	});
	
	
	$(".video_tab").click(function(){
		//other actions 
		//...
		ShowVideo();
	});
	$(".photo_tab").click(function(){
		//other actions 
		//...
		ShowPhoto();		
		if (area=="64")
		{		
			StartGalery64();
		}
		else
		{			
			StartGalery32();
		}
	});
	
	$(".flickr-thumb img").click(function(){
		$(".flickr-thumb img").removeClass("sel");
		//$(this).addClass("sel");
	});
	
	$(".video").click(function(){
		$(".videoImg").removeClass("sel");
		//$(this).children(".videoImg").addClass("sel");
	});	
	/*
	$(".videoImg").click(function(){
		$(".videoImg").removeClass("sel");
		$(this).addClass("sel");
	});	
	*/
	$(".build32_tab").click(function(){
		$(".build64").hide(); 
		$(".build32").show();
		area = "32";
		ShowVideo();		
	});
	$(".build64_tab").click(function(){
		$(".build32").hide();
		$(".build64").show();
		area = "64";
		ShowVideo();		
	});
	
	$(".flickr-thumb img").click(function(){
		var url  = $(this).attr("src").replace("_s","");
		$(".loading").css("z-index","2");
		$.loadImages(url,
			function(){
				$(".photosContent .focused_image").attr({ 
					src: url
				});
				$(".loading").css("z-index","-2");
			}
		);
	});
	
	$(".play_tab").click(function(){
		try{  
			var gallery = Galleria.get(0);gallery.play(2000);
		} catch(err){ }	
		try {  
			var gallery = Galleria.get(1); gallery.play(2000);
		} catch(err) {}
		$(this).addClass("type_selected");
		$(".pause_tab").removeClass("type_selected");
	});
	$(".pause_tab").click(function(){
		try{  
			var gallery = Galleria.get(0);gallery.pause();
		} catch(err){ }	
		try {  
			var gallery = Galleria.get(1); gallery.pause();
		} catch(err) {}
		$(this).addClass("type_selected");
		$(".play_tab").removeClass("type_selected");
	});
	
	
});
function closeModal(id) {
	$.modal.close(); 
	return false;
}

//win form
function winForm() {
	$.get(
        siteUrl+"ajax/win_form.html", 
        function(data)
        {
			$('#window').html(data);
            $('#form-content').modal();
        }, 
        "html");
}

function send() {
	$('#contestData').css('display', 'none');	
	$('#thanksContest').css('display', 'block');
}

//brakes
function brakes() {
	$.get(
        siteUrl+"ajax/brakes.html", 
        function(data)
        {
			$('#window').html(data);
            $('#brakes').modal();
        }, 
        "html");
}

/*function terms() {
	$.get(
        siteUrl+"ajax/terms.html", 
        function(data)
        {
			$('#temsWindow').html(data);
            $('#terms').modal();
        }, 
        "html");
}
*/
function family(id) {
	$.get(
        siteUrl+"ajax/sponsor.html", 
        function(data)
        {
			$('#window').html(data);
			if ($(window).height()>800) {
	            $('#sponsor').modal({
	            	position: [157,'']
	            });
			} else {
				$('#sponsor').modal();
			}

			showSponsor(id);
        }, 
        "html");
}

function showSponsor(id) {
	$('#logo_'+old_id).attr('class','hidden'); 
	$('#logo_'+id).attr('class',''); 
	old_id = id;
}

function showSize(size) {
	$('#downSize').html(size);
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

function sendMail() {
    if ($('#full_name').val().length == 0) {
         alert('Please enter your name.');
         $('#full_name').focus();
         return;
    }
    if ($('#date').val().length == 0 || $('#date').val() == 'mm/dd/yyyy') {
         alert('Please enter the date.');
         $('#date').focus();
         return;
    }
    /*if ($('#business_name').val().length == 0) {
         alert('Please enter the business name.');
         $('#business_name').focus();
         return;
    }*/
    if ($('#address').val().length == 0 || $('#address').val() == 'No PO Boxes') {
         alert('Please enter your address.');
         $('#address').focus();
         return;
    }
    if ($('#city').val().length == 0 || $('#city').val() == 'City') {
         alert('Please enter your city.');
         $('#city').focus();
         return;
    }
    if ($('#state').val().length == 0) {
         alert('Please select your state.');
         $('#state').focus();
         return;
    }
    if ($('#zip').val().length == 0 || $('#zip').val() == 'ZIP Code') {
         alert('Please enter your ZIP code.');
         $('#zip').focus();
         return;
    }
    numberPattern = new RegExp(/[0-9].*/i);
    if ($('#p1').val().length == 0 || $('#p2').val().length == 0 || $('#p3').val().length == 0 || !numberPattern.test($('#p1').val()) || !numberPattern.test($('#p2').val()) || !numberPattern.test($('#p3').val())) {
         alert('Please enter a valid phone number.');
         $('#p1').focus();
         return;
    }

    if (!isValidEmailAddress($('#email').val())) {
         alert('Please enter a valid e-mail address.');
         $('#email').focus();
         return;
    }

    if ($('#agree').is(':checked')) {
        $.post(
            "script/send_contest_participant_info.php",
            {
                full_name       : $('#full_name').val(),
                day             : $('#date').val(),
//                business_name   : $('#business_name').val(),
                address         : $('#address').val(),
                city            : $('#city').val(),
                state           : $('#state ').val(),
                zip             : $('#zip').val(),
                phone1          : $('#p1').val(),
                phone2          : $('#p2').val(),
                phone3          : $('#p3').val(),
                email           : $('#email').val()
            },
            function(data)
            {
                data = eval( '(' + data + ')' );
                if (data.status == "ok") {
                    $('#contestData').css('display', 'none');
                    $('#thanksContest').css('display', 'block');
                } else {
                    alert(data.reason);
                }
            }
        );

    } else {
        alert('You must agree with the Terms of service.');
    }
}

function OpenPopup(id) {
	pause();
	var page_url="";
	switch(id){
		case "news": page_url="ajax/news.html"; break;
		case "tour": page_url="ajax/tour.html";break;
		case "prize": page_url="ajax/prize.html";break;
		//case "prize": page_url="ajax/prize_not_ready.html";break;
		case "family": page_url="ajax/family.html";break;
		case "submit": page_url="ajax/submit.html";break;
		case "brakes": page_url="ajax/raybestos_brakes.html";break;
		default: alert("Unknown page!");break;
	}	
	$.get(		
        siteUrl+page_url, 
        function(data)
        {
			$('#window').html(data);
			$('#popupWindow').modal();
			
			/*if ($(window).height()>800) {
	            $('#popupWindow').modal({
	            	position: [157,'']
	            });
			} else {
				$('#popupWindow').modal();
			}*/
        }, 
        "html");
}

function StartGalery32()
{
	//Galleria.loadTheme('js/galleria.classic.js');		
	$('#galleria_build32').galleria(
		{
			width: 630,	
			height: 330, 
			debug: true
	});		
	$(".play_tab").removeClass("type_selected");
	$(".pause_tab").removeClass("type_selected");
	$(".pause_tab").addClass("type_selected");	
}
function StartGalery64()
{
	//Galleria.loadTheme('js/galleria.classic.js');						
	$('#galleria_build64').galleria(
		{
			width: 630,	
			height: 330, 
			debug: true
	});							
	$(".play_tab").removeClass("type_selected");
	$(".pause_tab").removeClass("type_selected");
	$(".pause_tab").addClass("type_selected");	
}
