/*
**
**	Some "global" variables for this script
**
*/
cursubmenu = "";
acttabmenu = "";
cursubpage = "";
showcandy = false;


/*
**	DHTML History (works properly only in firefox)
**
**	window.dhtmlHistory.create();
*/


/*
**
**	Things to be done when document is loaded...
**
*/
$( document ).ready( function() {

	/*
	**	DHTML History (works properly only in firefox)
	**
	**	if( true ) {
	**		dhtmlHistory.initialize();
	**		dhtmlHistory.addListener( function( newLocation, historyData ) {
	**			var historyMsg = (typeof historyData == "object" && historyData != null ? historyStorage.toJSON(historyData) : historyData ); 
	**			var msg = "A history change has occured:\n newLocation=" + newLocation + "\n historyData=" + historyMsg ; 
	**			// myRestorePage(oldLocation, historyData) 
	**			alert(msg);		
	**		});
	**	}
	*/

	if( typeof sIFR == "function" ) {
		sIFR.replaceElement( named( {
			sSelector: "body h1.header",
			sFlashSrc: "zapfino.swf",
			sColor: "#000000",
			sLinkColor: "#000000",
			sBgColor: "#FFFFFF",
			sHoverColor: "#CCCCCC",
			nPaddingTop: 0,
			nPaddingBottom: 0,
			xHeight: 9,
			sFlashVars: "textalign=left&offsetTop=6"
		} ) );
	};

	/*
	**
	**	We have javascript, so show the proper navigation menu
	**
	*/
	$( "#jsnav" ).show();


	/*
	**
	**	If we haven't already, show a "postit" of how to
	**	use the navigation bar
	**
	*/
	if( null == readCookie( "postit-shown" ) ) {
		$( "#postit" ).html( '<a title="Klicka f&ouml;r att st&auml;nga" id="postit-close" href="#" onclick="closePostit();"></a><span id="postit-top"></span><span id="postit-text">V&auml;lj sektion p&aring; nedre raden och d&auml;refter sida p&aring; den &ouml;vre raden. Aktuell sida markeras med r&ouml;d pil, och sektionen visas med orange f&auml;rg.</span><span id="postit-bot"></span>' );
		$( "#postit" ).show();
		var int = self.setInterval( "fadePostit()", 15000 );
	}

	/*
	**
	**	Patch for Opera not properly adhering to top AND bottom
	**	values on a div like Firefox and IE7
	**
	*/
	if( $.browser.opera ) {
		$(window).resize(function(){
			$( "#content" ).height( ( $( "#container" ).height() - 52 ) + "px" );
		});
	}

	/*
	**
	**	Just for fun, show the coordinates within #content div
	**
	*/
	$( "#content" ).mousemove( function( e ) {
		var clientCoords = e.clientX + ", " + e.clientY;
		$( "#coords" ).text( clientCoords );
	});

	/*
	**
	**	This takes care of showing/hiding the spinner whenever
	**	the #content div gets to loading some new content
	**
	*/
	$( "#content" ).ajaxStart( function() {
		$( "#spinner" ).show();
	})
	$( "#content" ).ajaxStop( function() {
		$( "#content" ).scrollTop( 0 );
		$( "#spinner" ).hide();
		
//		$( "#innercontent h1.header" ).text( "" );
//		$( "#innercontent h1.header" ).html( "<span>" + $( "#innercontent h1.header" ).text() + "</span>" );
//		$( "#innercontent h1.header" ).addClass( "headerGfx" );
		if( typeof sIFR == "function" ) {
			sIFR.replaceElement( named( {
				sSelector: "body h1.header",
				sFlashSrc: "zapfino.swf",
				sColor: "#000000",
				sLinkColor: "#000000",
				sBgColor: "#FFFFFF",
				sHoverColor: "#CCCCCC",
				nPaddingTop: 0,
				nPaddingBottom: 0,
				xHeight: 9,
				sFlashVars: "textalign=left&offsetTop=6"
			} ) );
		};

		$( "#innercontent a.mainnav" ).click( function() {
			$( "#content" ).load( this.href + " #innercontent" );
			$( "#docname" ).text( this.href );
			return false;
		})

		$( "#innercontent img" ).each( function() {
			if( this.title == "" ) {
				this.title = this.alt;
			}
		})

	})

	/*
	**
	**	Make sure all navigation links has a prepended
	**	span element that we can play around with
	**
	*/
	$( "#navigation ul a" ).prepend( "<span></span>" );
//	$( "#innercontent h1.header" ).text( "" );
//	$( "#innercontent h1.header" ).html( "<span>" + $( "#innercontent h1.header" ).text() + "</span>" );
//	$( "#innercontent h1.header" ).addClass( "headerGfx" );


	/*
	**
	**	Set the "loading" text of the spinner, and hide it
	**
	*/
	$( "#spinner" ).append( "H&auml;mtar inneh&aring;ll..." );

	/*
	**
	**	Set up the clock thingy to update every second
	**
	*/
	var int = self.setInterval( "updateClock()", 1000 );
	updateClock();

	/*
	**
	**	For "debug" purpose, show which box model is in use
	**
	*/
	$( "#debug" ).text( $.boxModel );

	/*
	**
	**	Get rid of the focus-borders on links
	**
	*/
	$( "#navigation a" ).focus( function() { $( this ).blur(); } );

	/*
	**
	**	Set up showing/hiding of submenus
	**
	*/
	$( "#navigation a.mainnav" ).click( function() {
		if( cursubmenu != "" ) {
			$( "#ul" + cursubmenu ).hide();
			$( "#" + cursubmenu ).removeClass( "curtab" );
		}
		$( "#ul" + this.id ).show();
		$( "#" + this.id ).addClass( "curtab" );
		cursubmenu = this.id;
		
	})

	/*
	**
	**	Take care of showing which page is loaded as
	**	well as the actual AJAX loading of the pages
	**
	*/
	$( "#navigation ul ul a" ).click( function() {
		if( cursubpage != "" ) {
			$( "#" + cursubpage ).removeClass( "redarrow" );
		}
		$( this ).addClass( "redarrow" );
		cursubpage = this.id;
		if( acttabmenu != "" ) {
			$( "#" + acttabmenu ).removeClass( "active" );
		}
		$( "#" + cursubmenu ).addClass( "active" );
		acttabmenu = cursubmenu;

		/*
		**	DHTML History (works properly only in firefox)
		**
		**		found = this.href.match(/.*(?:\?|&)tag=(.+)(?:\&.*)*$/);
		**		if( found ) {
		**			alert( found[1] );
		**			dhtmlHistory.add( found[1] );
		**		}
		*/

		$( "#content" ).load( this.href + " #innercontent" );
		$( "#docname" ).text( this.href );
		return false;
	});

	$( "#innercontent a.mainnav" ).click( function() {
		$( "#content" ).load( this.href + " #innercontent" );
		$( "#docname" ).text( this.href );
		return false;
	})

	$( "#innercontent img" ).each( function() {
		if( this.title == "" ) {
			this.title = this.alt;
		}
	});

	/*
	**
	**	Whenever the address div is clicked, relocate to mailto link
	**
	*/
	$( "#address" ).click( function() {
		location.href = "mailto:info@nossebromediaproduktion.se";
	})

	/*
	**
	**	Make it possible to toggle showing of the crop,
	**	registration marks etc by clicking the logo
	**
	*/
	$( "#logo" ).click( function() {
		if( showcandy ) {
			showcandy = false;
			$( "#eyecandy" ).fadeOut("slow");
		} else {
			showcandy = true;
			$( "#eyecandy" ).fadeIn("slow");
		}
	})

});

function fadePostit() {
	$( "#postit" ).fadeOut( 3000 );
}

function closePostit() {
	$( "#postit" ).fadeOut( "slow", function() {
		createCookie( "postit-shown", "yes", 30 );
	} );
}

/*
**
**	Simple helper function to update the clock to current time
**
*/
function updateClock() {
	$( "#docdate" ).text( formatDate( new Date(), "yyyy-MM-dd HH:mm:ss" ) );
}

/*
**
**	Cookie handling functions
**
*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
