$(document).ready(function(){

	var nav_coll=new Array(4);
	current_hash_ary = new Array();
	
	nav_coll[0]="digimags";
	nav_coll[1]="about";
	nav_coll[2]="digimags";
	nav_coll[3]="about";
	
	
	clickhash = false;
	
	$("div.readmore").hide();
	
	//has url got a hash or is it clean.
	current_hash = window.location.hash.replace("#","");
	
	if(current_hash.length>0) 
	{//is hashed aka deeplink
		current_hash_ary = current_hash.split("-");

		if (current_hash_ary.length>1) 
		{ //hash includes a sub-nav section
			
			//alert(current_hash_ary[1]);
			displaySection(current_hash_ary[0]);
			showsubnav(current_hash_ary[1]);
			window.location.hash = current_hash;
			
			//$(window).scrollTop();return false;		
		} 
		else 
		{ //just a main section

			displaySection(current_hash_ary[0]); //just has the inital main nav hash
		
			//$(window).scrollTop();return false;	
		}
		
	} 
	else 
	{ //a direct hit to the site, so need to find/create random starting page
				
		//alert($.cookie('homenav'));
		if($.cookie('homenav') == true) 
		{
				i = $.cookie('homenav');
				
				
				//$(window).scrollTop();return false;
				
		} 
		else 
		{
		        i = Math.floor(Math.random()*4);
				$.cookie("homenav", i, { expires: 30 });
				
				//displaySection(nav_coll[i]);
				//$(window).scrollTop();return false;
		}
		displaySection(nav_coll[i]);
	}

	var hash = location.hash;

	setInterval(function()
	{
	    if (location.hash != hash)
	    {
			
	        if(clickhash == false) {
				//alert("Changed from " + hash + " to " + location.hash);
				current_hash = window.location.hash.replace("#","");
				current_hash_ary = current_hash.split("-");
				
				

				if (current_hash_ary.length>1) 
					{ //hash includes a sub-nav section

						//alert(current_hash_ary[1]);
						 displaySection(current_hash_ary[0]);
						 showsubnav(current_hash_ary[1]);
						//window.location.hash = current_hash;

						//$(window).scrollTop();return false;		
					} 
					else 
					{ //just a main section
						
						displaySection(current_hash_ary[0]); //just has the inital main nav hash

						//$(window).scrollTop();return false;	
					}
				}
	        hash = location.hash;
			clickhash = false;
	
	
	    }
	}, 100);
	
	$('#main_nav a').click(function(){


		navid = $(this).attr("id").substring(4,99);
		

		displaySection(navid);
		goTop();


	});
	
	
	
	$('#sixtysecspic').click(function(){
		showsixtysecs();
		goTop();
	});
	
	/*
	$('#demopic').click(function(){
		showdemo();
		goTop();
	});
	*/
	

	$(".popboxclose").click(function(event){

		$("#popcontainer2").hide();
		$("#popbox div").hide();
	});		

	
	$('.readmorelink').click(function(){
		section_id = $(this).attr("rel");
		
		
		$('#'+section_id+ ' .readmore').toggle('slow', function() {
		});
		$(this).html()=="read more" ? $(this).html("show less") : $(this).html("read more");
	    //goTop();
	});
	
	
	$("a#tc").click(function(event){
		//is it on the maintuser page
		$("#popbox form div").hide();

		$("div#popcontainer2").fadeIn("slow");
		goTop();
		$("#tc_form").show();
		$("#tc_form").load("common/tc.php");	
	});

	
	$("a.sixtysec").click(function(event){
		//is it on the maintuser page
		$("#popbox form div").hide();

		$("div#popcontainer2").fadeIn("slow");
		goTop();
		$("#bigfigvid_form").show();
		$("#bigfigvid_form").load("common/60secs.php");	
	});



				
});


function removeclip(){

	$("#bigfigvid_form").html("");	
	$("#popcontainer2").hide();
	$("#popbox div").hide();
}

function displaySection(id){


	if(id=="sixtysecs") {
		showsixtysecs();
	} 
	else 
	{ 
		$(".section").show();
		$("#sixtysecs_copy").hide();
		$("#sixtysecs a").removeClass("current");
		$("#main_nav a").removeClass("current");
		$("#nav_"+id).addClass("current");
		$(".sub").hide();
	
		$("div.readmore").hide();
		$('.readmorelink').html("read more")
		$("#"+id).show("fast");
		$("#"+id +' .subnav a').removeClass("current");
		$("#"+id +' .subnav a').first().addClass("current");
		subnavid = $("#"+id +' .subnav a').attr("id").substring(4,99);
		$('.submain').hide();
		$('#section_'+subnavid).show();
		window.location.hash = id;
		clickhash = true;
		goTop();
		
		/*
		if(id=="about") {
			var v = document.getElementById("about_movie");
		    if (v.paused) {v.play();}   
		}
		*/

	}
	
	
}

function showsubnav(id) {

	$('.submain').hide();
	$('#section_'+id).show();
	$('.subnav a').removeClass("current");
	$("#nav_"+id).addClass("current");
	clickhash = true;
}

function showsixtysecs() {
	$(".section").hide();
	$("#main_nav a").removeClass("current");
	$("#sixtysecs a").addClass("current");


	$("#sixtysecs_copy").load("sixtysecs.php", function()
		{	
			$("#sixtysecs_copy").show('slow');
		});
	
}

function showdemo() {
	$(".section").hide();
	$("#main_nav a").removeClass("current");



	$("#sixtysecs_copy").load("demos/planet.html", function()
		{	
			$("#sixtysecs_copy").show('slow');
		});
	
}


function showwhy(id) {
	$("#whydigi"+id).toggle('fast', function() {

	});
}
	
function showbio(id) {
	$("#bio"+id).toggle('fast', function() {

	});
}

function poppic(pic) {

	
	$("#popbox form div").hide();

	$("div#popcontainer2").fadeIn("slow");
	$("#picarea").show();
	$("#picarea #imgsrc").html('<img alt="'+pic+'" src="images/'+pic+'"/>');

}

function footer_link(current_hash) {
	
	//current_hash = window.location.hash.replace("#","");
	current_hash_ary =current_hash.split("-");
	if (current_hash_ary.length>1) { //hash includes a sub-nav section

	displaySection(current_hash_ary[0]);
	showsubnav(current_hash_ary[1]);
	
	} else {
		displaySection(current_hash_ary[0]); //just has the inital main nav hash
	}
}

function goTop() {

	
	if($.browser.safari){ bodyelem = $("body") } else{ bodyelem = $("html") } bodyelem.animate({scrollTop:0},1);
	//$('html').animate({scrollTop:0}, 1);
}


	



/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
