Ext.onReady(function(){
	Ext.get( 'startTab' ).addClass( 'selected' );
	
	Ext.get('dataGoesHere').load({															// Load the start category post list
		url: '../AJAX/fetchPosts.php',
		text: 'Loading...'
	}).show();
	
	Ext.get( 'dataGoesBig' ).load({															// And the last post in the start category
		url: '../AJAX/fetchPost.php',
		text: 'Loading...',
		params: 'post_category=Inlägg'
	}).show();
	
	Ext.get('startTab').on('click', function( e ){
		Ext.select( '.selected' ).removeClass( 'selected' );
		Ext.get( e.target ).addClass( 'selected' );
		Ext.get('dataGoesHere').load({															// Load the start category post list
			url: '../AJAX/fetchPosts.php',
			text: 'Loading...',
			params: 'post_category=Inlägg'
		}).show();
		
		Ext.get( 'dataGoesBig' ).load({															// And the last post in the start category
			url: '../AJAX/fetchPost.php',
			text: 'Loading...',
			params: 'post_category=Inlägg'
		}).show();
	});
	
	Ext.get('projectTab').on('click', function( e ){
		Ext.select( '.selected' ).removeClass( 'selected' );
		Ext.get( e.target ).addClass( 'selected' );
		Ext.get('dataGoesHere').load({															// Load the start category post list
			url: '../AJAX/fetchPosts.php',
			text: 'Loading...',
			params: 'post_category=Projekt'
		}).show();
		
		Ext.get( 'dataGoesBig' ).load({															// And the last post in the start category
			url: '../AJAX/fetchPost.php',
			text: 'Loading...',
			params: 'post_category=Projekt'
		}).show();
	});
	
	Ext.get('filmTab').on('click', function( e ){
		Ext.select( '.selected' ).removeClass( 'selected' );
		Ext.get( e.target ).addClass( 'selected' );
		Ext.get('dataGoesHere').load({															// Load the start category post list
			url: '../AJAX/fetchPosts.php',
			text: 'Loading...',
			params: 'post_category=Filmrecension'
		}).show();
		
		Ext.get( 'dataGoesBig' ).load({															// And the last post in the start category
			url: '../AJAX/fetchPost.php',
			text: 'Loading...',
			params: 'post_category=Filmrecension'
		}).show();
	});
	
	Ext.get( 'photoTab' ).on( 'click', function( e ){
		Ext.select( '.selected' ).removeClass( 'selected' );
		Ext.get( e.target ).addClass( 'selected' );
		Ext.get('dataGoesHere').load({															// Load the start category post list
			url: '../AJAX/fetchPosts.php',
			text: 'Loading...',
			params: 'post_category=Fotografi'
		}).show();
		
		Ext.get( 'dataGoesBig' ).load({															// And the last post in the start category
			url: '../AJAX/fetchPost.php',
			text: 'Loading...',
			params: 'post_category=Fotografi'
		}).show();
	});
	
	Ext.get( 'logInOut' ).on( 'click', function( e ){
		Ext.select( '.selected' ).removeClass( 'selected' );
		Ext.get( e.target ).addClass( 'selected' );
		Ext.get('dataGoesHere').load({															// Load the start category post list
			url: '../AJAX/member.php',
			text: 'Loading...'
		}).show();
	});
});

// Globals
xmlHttp = null;


// From W3Schools
function GetXmlHttpObject(handler) {
	var objXMLHttp = null

	if( window.XMLHttpRequest ) {
		objXMLHttp = new XMLHttpRequest()
	} else if( window.ActiveXObject ) {
		objXMLHttp = new ActiveXObject( "Microsoft.XMLHTTP" )
	}

	return objXMLHttp
}


// Generic internet find
function urlencode( str ) {
    var v;

    try {
    	v = encodeURIComponent( str );
    } catch ( e ) {
    	v = escape( str );
    }

    return v.replace( /%20/g, "+" );
}


function toggleGenreVisibility( anElementId ) {
	if( document.getElementById( anElementId ).style.display == 'none' ) {
		document.getElementById( anElementId ).style.display = 'block';
		document.getElementById( anElementId ).previousSibling.previousSibling.style.backgroundImage = 'url( css/collapse.gif )';
	} else {
		document.getElementById( anElementId ).style.display = 'none';
		document.getElementById( anElementId ).previousSibling.previousSibling.style.backgroundImage = 'url( css/expand.gif )';
	}
}


function addReview( aMovieId, aRating, aReview, anUpdateId ){ //compose get URL
	activeAJAXTarget = 'addReviewExistingMovie';

	xmlHttp = GetXmlHttpObject()
	if( xmlHttp == null ){
		alert( "Browser does not support HTTP Request" )
		return
	}
	var url = 'AJAX/newMovieReview.php'
	url = url + "?add_movieid=" + aMovieId
	url = url + "&add_rating=" + document.getElementById( aRating ).value
	url = url + "&add_review=" + urlencode( document.getElementById( aReview ).value )
	url = url + "&updateid=" + anUpdateId
	url = url + "&sid=" + Math.random()
	xmlHttp.onreadystatechange = stateChanged
	xmlHttp.open( "GET", url, true )
	xmlHttp.send( null )
}


function selectOption( aSelectBox, aValue ) {
	options = document.getElementById( aSelectBox )
	option = -1;

	for( i = 0; i < options.length; i++ ) {
		if( options[i].value == aValue ) {
			option = i;
			break;
		}
	}

	document.getElementById( aSelectBox ).selectedIndex = option;
}


function selectTab( obj ) {
	currentSelectedTab = document.getElementById( 'selectedTab' );
	currentSelectedTab.id = '';
	obj.id = 'selectedTab';
}


function fetchData( aScript, aTargetId, someInput, aConfirmationText, aFollowFunction ) {
	xmlHttp = GetXmlHttpObject()

	if( xmlHttp == null ) {
		alert( "Browser does not support HTTP Request" )
		return
	}

	url = "AJAX/" + aScript + ".php?sid=" + Math.random()

	if( someInput ) {
		for( i = 0; i < someInput.length; i += 2 )
			url += "&" + urlencode( someInput[i] ) + "=" + urlencode( someInput[i+1] )
	}
	
//	alert( url );

	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 1 ) {
			if( aTargetId )
				document.getElementById( aTargetId ).innerHTML = "<p>Loading...</p>"
		} else if( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) {
			if( aTargetId )
				document.getElementById( aTargetId ).innerHTML = xmlHttp.responseText
			if( aConfirmationText )
				alert( aConfirmationText )
			if( aFollowFunction )
				aFollowFunction();
			if( xmlHttp.responseText == "reloading..." )
				window.location.reload();
		}
	}

	xmlHttp.open( "GET", url, true )
	xmlHttp.send( null )
}

