﻿var useAjax = false;
var histURL = null;
var loadURL = false;
var allDone = false;

window.onload = function() {
	if( useAjax ) {
		ResolveLinks();
		document.getElementById('loading').innerHTML = "Sidan laddas...";
		if( typeof( dhtmlHistory ) == "object" ) {
		
			dhtmlHistory.initialize();
			dhtmlHistory.addListener(historyChange);
			if (dhtmlHistory.isFirstLoad()) {
				entryURL = location.href;
				if( entryURL.indexOf( "#" ) != -1 ) {
					tmp = entryURL.split( "#", 2 )
					if( tmp[1] != '' ) {
						entryURL = tmp[0];
						loadURL = true;
						AjaxLoad( tmp[1] );
					}
				}
			}
		}
		allDone = true;
	}
}

function historyChange(newLocation, historyData) {
	var historyMsg = historyData;
	if( typeof( entryURL ) == "undefined" ) {
		entryURL = location.href;
	}
	if( historyData != null ) {
		loadURL = true;
		AjaxLoad( historyMsg );
	} else {
		loadURL = true;
		AjaxLoad( entryURL );
	}
}

function AjaxLoad( href ) {
	LoadStart();
	if( href.indexOf( "#" ) != -1 ) {
		tmp = href.split( "#", 2 )
		href = tmp[0];
	}

	try {
		console.debug( "AjaxLoad( '" + href + "' );" );
	} catch(e) {}

	//alert( "AjaxLoad( '" + href + "' );" );
	histURL = href;
	//alert( "AjaxLoad()" );
	AjaxRequest.get(
		{
			'url':href,
			'queryString':'contents=true',
			'generateUniqueUrl':false,
			'onError':function(req){ alert('Sidan kunde inte hittas.'); LoadStop(); },
			'onSuccess':function(req) { AjaxInnerHTML( req, 'content' ); ResolveLinks(); }
		}
	);
	if( BrowserDetect.browser == "Explorer" ) {
		try {
			//alert( "1" );
			if( window.event ) {
				window.event.returnValue = false;
			}
			//alert( "2" );
		} catch( e ) {
		}
	}
	return false;
}

function AjaxInnerHTML( req, div ) {
	document.getElementById( div ).innerHTML = req.responseText;
	document.getElementById( div ).scrollTop = 0;
	if( loadURL == false && typeof( dhtmlHistory ) == "object" ) {
		dhtmlHistory.add( histURL, histURL );
	} else {
		loadURL = false;
	}
	ResolveLinks();
	LoadStop();
	//alert( "Finished in AjaxInnerHTML()" );
}

function ResolveLinks() {
	var anchors = document.getElementsByTagName( "a" );
	for( var i = 0; i < anchors.length; i++ ) {
		if( anchors[i].className == "mainnav" || anchors[i].className == "pagelink" ) {
			anchors[i].onclick = function() { return AjaxLoad( this.href ); };
			//anchors[i].title = 'Link has been replaced!';
		}
	}
}

function LoadStart() {
	loadingdiv = document.getElementById('loading');
	loadingdiv.style.display = 'block';
}

function LoadStop() {
	loadingdiv.style.display = 'none';
}

function printMail( n, d, t ) {
	document.write( '<a href=\"mailto:' + n + '@' + d + '.' + t + '\" class=\"pmail\">' );
	document.write( n + '@' + d + '.' + t + '</a>');
}


var lastpage = -1;

function GallerySelect( n ) {
	e = document.getElementById( "photosize" );
	if( e ) {
		c = e.options[ e.selectedIndex ].value;
	}
	if( n == -1 ) {
		if( lastpage == -1 ) {
			n = 1;
		} else {
			n = lastpage;
		}
	}
	lastpage = n;
	if( useAjax ) {
		AjaxLoad( 'galleri.php?page=' + n + '&cat=' + c + "&contents=true" );
	} else {
		location = 'galleri.php?page=' + n + '&cat=' + c;
	}
}

function CategorySelect( n ) {
	if( n > 0 ) {
		if( useAjax ) {
			AjaxLoad( 'galleri.php?cat=' + n + "&contents=true" );
		} else {
			location = 'galleri.php?cat=' + n;
		}
	} else {
		if( useAjax ) {
			AjaxLoad( 'galleri.php?contents=true' );
		} else {
			location = 'galleri.php';
		}
	}
}

