$(document).ready( function() {
	
	function removeLastSlash( text ) {
		
		// Get last symbol
		var lastS = text.charAt( text.length - 1 );
		
		// If last symbol is / then remove it to create even links
		if( lastS == "/" ) {
			text = text.substring( 0, href.length - 1 );
		}
		return text;
	}

	var href = window.location.href;

	href = removeLastSlash( href );
	
	href_no_domain = href.replace( "http://" + document.domain, "" ).replace( /\/([a-zA-Z0-9_]+)\.html/, "" );

	// Get all navigation links
	var nav_links = $( '#nav li a' );

	for( var i = 0; i < nav_links.length; i++ ) {

		var a_href = $( nav_links[i] ).attr( "href" );

		a_href = a_href.replace( /\/([a-zA-Z0-9_]+)\.html/, "" );
		
		// IE7 issue
		href_no_domain = removeLastSlash( href_no_domain );
		
		if( a_href.indexOf( href_no_domain ) != -1 ) {
			
			$( nav_links[i] ).addClass( "active" );

			if( $( nav_links[i].parentNode ).hasClass( "toggle" ) ) {
				
				$( nav_links[i].parentNode ).addClass( "opened" );
				$( nav_links[i] ).removeClass( "active" ).addClass( "menu_button_toggle" );

				// Get last li from the toggled menu
				var last_li = $( "li:last", nav_links[i].parentNode );
				// Add CSS class to it so the design can continue
				$( last_li ).addClass( "footer" );
			}
			break;

		}
		

	}
	
	$( '#read_and_play_section h2' ).click( function() {
		var content = $( "~ div.content", this );
		$( content ).slideToggle('slow', function() {
			var display = this.style.display;
			var h2 = $( "h2", this.parentNode );

			if( display == "block" ) {
				$( h2 ).css({ backgroundImage : "url( ../images/h2_bg_active.png )" });
			} else {
				$( h2 ).css({ backgroundImage : "url( ../images/h2_bg.png )" });
			}

		});
	});

	$( '#nav .toggle a' ).click( function() {
		
		// Hard-coded : about link should be opened directly
		if( this.href.indexOf( "parents/about" ) != - 1) {
			window.location = this.href;
			return false;
		}

		var li = $( this.parentNode );
		if( $( li ).hasClass( "toggle" ) ) {
			
			if( $( li ).hasClass( "opened" ) ) {

				$( li ).removeClass( "opened" );
				$( this ).removeClass( "menu_button_toggle" );

			} else {

				$( li ).addClass( "opened" );
				
				$( this ).addClass( "menu_button_toggle" );
				
				// Get last li from the toggled menu
				var last_li = $( "li:last", li );
				// Add CSS class to it so the design can continue
				$( last_li ).addClass( "footer" );

			}
		
		return false;
		}

	});
	
	var myloc = window.location.href;
	var locarray = myloc.split("/");
	var currentFolder = locarray[(locarray.length-2)];
	
	newImage = "url(../images/menu_button_hover.png)";
	if (currentFolder == 'uk')
	newImage = "url(../../images/menu_button_hover.png)";
	
	var contentHeight = $('#content').height();
	$('#left').height(contentHeight+30);
	//$( '#left' ).css({ minHeight : contentHeight });
});