var introShown = false;

// Custom utility functions
function toTitleCase(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
}      

function formatTitle(title) {
    newTitle = title.replace("php","");
    newTitle = 'Orca im Hafen.' + (newTitle != '/' ? ' / ' + toTitleCase(newTitle.substr(1, newTitle.length - 2).replace(/\//g, ' / ')) : '');
    return newTitle;
}




// Custom SWFAddress and Ajax handling

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}
function updateChange(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
			$("#content").html(xhr.responseText);
			$.prettyLoader.hide();
			$("#content").fadeIn("slow", function() {
				initSection(newSection);
			});
        } else {
            //alert('Error: ' + xhr.status + '!');
        }
    }
}

function movieResize() {
	if(introShown == false) {
		$("html").css("overflow-y","hidden");
		window.resizeTo(screen.availWidth,screen.availHeight);
		window.moveTo(0,0);
	}
}


function showBasicSiteStuff() {
	$("html").css("overflow-y","scroll");
	introShown = true;
	$("#introMovie").fadeOut("fast", function() {
		$('#introMovie').html("");
	});
	$("#backgroundImage").delay(0).fadeIn(200);
	$("#logo").delay(0).fadeIn(200);
	$("#metanavigation").delay(0).fadeIn(200);
	$("#footer").fadeIn('slow');
	$("#textcontent").delay(500).fadeIn(200);
	$("#pagefooter").delay(0).fadeIn(200);
	$("#switcher").delay(0).fadeIn(200);
}

function handleChange(event) {
	
	$("#movie").remove();

	$.prettyLoader.show();
	
	$.scrollTo(0,{duration:300});
	
	$("#content").delay(300).fadeOut("fast", function() {

    var index, rel, links = document.getElementsByTagName('a'), path = event.path;
    if (path.substr(path.length - 1) != '/') {
        path += '/';
    }
    for (var i = 0, l, link; link = links[i]; i++) {
        index = link.rel.indexOf('?');
        rel = (index > -1) ? link.rel.substr(0, index) : link.rel;
        link.className = (rel == path) ? 'selected' : '';
    }
    var parameters = '';
    for (var p in event.parameters) {
        parameters += '&' + p + '=' + event.parameters[p];
    }
    var xhr = getTransport();
    xhr.onreadystatechange = function() {
        updateChange(xhr);
    }
    
    eventpathstripped = event.path.replace("/","");
    eventpathstripped = eventpathstripped.replace("/","");
    newSection = eventpathstripped;
    
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
    	dontShowIntro = true;
    } else {
   		dontShowIntro = false;
    }
    
    if(!newSection) {
		newSection = "home";
    	SWFAddress.setValue(newSection);
    } else {
    	if(newSection != "home") {
    		$("#homecontent").fadeOut(200);
			showBasicSiteStuff();
		} else {
			$("#homecontent").delay(100).fadeIn(200);
		}
    }
    
	changeImageLoop(newSection);
    
    xhr.open('get', 'callbacks.php?section=' + eventpathstripped + parameters, true);
    xhr.send('');
    
    underscoreFind = eventpathstripped.search("_");
	if(underscoreFind == -1) {
		changeNavImage(eventpathstripped);
		
		var query = "section="+eventpathstripped;
		
		$.get("titles.php",query, function(data){
			setWindowTitle(data);
		});

	} else {
		
		sectionArray = eventpathstripped.split("_");
		changeNavImage(sectionArray[0]);
		
	}
	});	

}

function copyLink() {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('Text', SWFAddress.getBaseURL() + SWFAddress.getValue());
    } else {
        //alert('Unsupported browser.');
    }
}





SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
