﻿var imgURLs = new Array();
var delayLength = 7000;
var thisImg;
var timer;

function IsThisBrowserIE6() {
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
}

$(document).ready(function () {
    //Portfolio images
    $("#image").show();
    if($("#thumbs").length > 0) {
	    $("#thumbs > li > a").each(function () {
		    imgURLs.push($(this).attr("href"));
	    });
    	
	    thisImg = imgURLs.length - 1;
    	
    	//Check to see if there is more than one image to rotate through
    	if(imgURLs.length > 1) { 
    	    $("#thumbs").show();
	        changeImage() //inital load
	        timer = setInterval("changeImage()", delayLength); //automatically rotate
	    }
	    else {
	        showImage(imgURLs[0]);
	    }
    	
	    $("#thumbs > li > a").click(function () { 
		    clearInterval(timer); //remove automation
		    showImage($(this).attr("href"));
		    $("#thumbs > li > a").removeClass("active");
		    $(this).addClass("active");
		    var nextImg;
		    for(x=0; x<imgURLs.length; x++) {
			    if(imgURLs[x] == $(this).attr("href")) {
				    thisImg = [x]; //start rotation again using the next image in the sequence
			    }
		    }
		    timer = setInterval("changeImage()", delayLength);
		    return false;
	    });
	}


	//IE6 warning
    /*
    if(IsThisBrowserIE6()) {
        $.cookie('cookie_IE6warning', 'xxxxx', { expires: 7 });
        if($.cookie('cookie_IE6warning') == null) {
            $('body').append(
                '<div id="IE6warning" style="display:none">' +
                  '<div id="modal-content">' +
                    '<h1>Your web browser is outdated</h1>' +
                    '<p class="upgrade">You can easily upgrade to the latest version</p>' +
                    '<a href="http://www.microsoft.com/windows/internet-explorer"><img src="images/IE.jpg" alt="Internet Explorer 8" /></a>' +
		            '<p class="ie"><a href="http://www.microsoft.com/windows/internet-explorer">Internet Explorer 8</a></p>' +
                    '<div class="why-upgrade">' +
                      '<h3>Why should I upgrade?</h3>' +
                      '<dl>' +
                        '<dt>Web sites load faster</dt>' +
                        '<dd>often double the speed of this older version.</dd>' +
                        '<dt>Web sites render correctly</dt>' +
                        '<dd>with more web standards compliance.</dd>' +
			            '<dt>Tabs Interface</dt>' +
			            '<dd>lets you view multiple sites in one window.</dd>' +
			            '<dt>Safer browsing</dt>' +
			            '<dd>with better security and phishing protection.</dd>' +
			            '<dt>Convenient Printing</dt>' +
			            '<dd>with fit-to-page capability.</dd>' +
                      '</dl>' +
                    '</div>' +
		            '<div class="other-browsers">' +
		              '<h3>Explore other browsers</h3>' + 
		              '<ul>' +
		                '<li><a href="http://www.google.com/chrome" style="background:url(images/Chrome.jpg) no-repeat;">Google Chrome</a></li>' +
		                '<li><a href="http://getfirefox.com" style="background:url(images/Firefox.jpg) no-repeat;">Mozilla Firefox</a></li>' +
		                '<li><a href="http://www.opera.com/download/" style="background:url(images/Opera.jpg) no-repeat;">Opera</a></li>' +
		                '<li><a href="http://www.apple.com/safari/download/" style="background:url(images/Safari.jpg) no-repeat;">Apple Safari</a></li>' +
		              '</ul>' +
		            '</div>' +
		            '<div style="clear:both;"><a href="#" class="simplemodal-close">close</a></div>' +
	              '</div>' +
                '</div>'
            );
            $('#IE6warning').modal();
        }
    }
    */
        
	//Open external links in a new window
	$('a[href^="http://"]')
		.attr({
			target: "_blank", 
			title: "Opens in a new window"
		});
	
	//tag cloud font sizing
		$('a[class="2"]').css("font-size", "1.2em");
		$('a[class="3"]').css("font-size", "1.4em");
		$('a[class="4"]').css("font-size", "1.6em");
		$('a[class="5"]').css("font-size", "1.8em");
		$('a[class="6"]').css("font-size", "2em");
		$('a[class="7"]').css("font-size", "2.2em");
		$('a[class="8"]').css("font-size", "2.4em");
		$('a[class="9"]').css("font-size", "2.6em");
		$('a[class="10"]').css("font-size", "2.8em");

});

function nextImage () {
	var low = 0;
	var high = imgURLs.length - 1;
	
	thisImg++;
	if (thisImg == imgURLs.length) {
		thisImg = 0;
	}
	return imgURLs[thisImg];
}

function changeImage () {
	var t = imgURLs[thisImg];
	var n = nextImage();
	
	if (t != n) {
		showImage(n);
	} else { 
		changeImage();
	}
}

function showImage(src) {
	var largeImage = new Image();
	$(largeImage).load(function() {
		$(this).hide();
		$("#image img").fadeOut("slow").remove();
		$("#image").append(this)
		$(this).fadeIn("slow");              
	});    
	$(largeImage).attr("src", src);
	$("#thumbs > li > a").removeClass("active");
    $("#thumbs > li > a").each(function () {
	    if($(this).attr("href") == src) {
	        $(this).addClass("active");
	    }
    });
}

//Konami code
var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e) {
    kkeys.push( e.keyCode );
    if ( kkeys.toString().indexOf( konami ) >= 0 ){
        $(document).unbind('keydown',arguments.callee);
        alert("Eventually I'll think of something cool to do here...");
    }
});
