//$('ul#menu li ul li').css({'border':'1px solid black', 'font-size':'20px'});

var hash_locked = false;
var next_button = 'selected_works';
var prev_button = 'selected_works';

// alters HREF attributes for next/previous navigation arrows
// and refreshes the onClick event
function rebind_navi_btns() {
	// unbind click events
	$('#arrow-left > a').unbind('click');
	$('#arrow-right > a').unbind('click');

	// set the appropriate HREFs
	$('#arrow-left > a').attr('href', prev_button+'.html');
	$('#arrow-right > a').attr('href', next_button+'.html');

	// bind new click events
	$('#arrow-left > a').bind('click', function(){
		pageload(prev_button);
		return false;
	});
	$('#arrow-right > a').bind('click', function(){
		pageload(next_button);
		return false;
	});
}

// callback function called once content is loaded
function add_borders() {
	/* adds teal border round selected work boxes */
	$("#box-content .box-work").mouseover(function() {
		$(this).addClass('box-work-border');
	});

	$("#box-content .box-work").mouseout(function() {
		$("#box-content .box-work").removeClass('box-work-border');
	});

	// now we need to get rid of loading span
	$('#load').fadeOut('normal');
	// ... and show the actual content
	$('#box-content').show('normal');

	

	// focus the changed content
	//$('#box-content').focus();
	
	/* why use pixel scene testimonials */
	$('.random').randomChild();
	
	// rebind onClick events for next/previous arrows
	rebind_navi_btns();

	hash_locked = false;
	}

function pageload(hash) {
	if (!hash_locked) {
		// since for some reason we fire load function twice,
		// we need to strip the full URL that's being returned
		// second time
		if (hash.indexOf('://') > -1) {
			hash = hash.split('/');
			hash = hash[hash.length - 1];
		}
		hash_locked = true;

		$("#menu > li").removeClass('current');
		$('#load').remove();
		$('#wrapper').append('<span id="load"><img src="images/ajax-loader.gif" width="43" height="11" alt="loading" /></span>');
		$('#load').fadeIn('normal');
		$('#box-content').hide('fast', function(){
			// hash doesn't contain the first # character.
			if (hash && (hash != '.html')) {
				if (hash.substr((hash.length - 5), 5) != '.html') {
					hash = hash + '.html';
				}
				
				$("#menu > li").removeClass('current');
				$("#menu > li > ul > li").removeClass('msm current');
				$("#menu > li > ul > li").removeClass('msm');
				$("#menu > li > ul > li").addClass('msm');
				if (hash == "index.html") {
					$("#m01").addClass("current");
					$("#p1").removeClass('msm');
					$("#p1").addClass("msm current");
					next_button = 'selected_works';
					prev_button = 'selected_works';
					rebind_navi_btns();
				}
				else 
					if (hash == "selected_works.html") {
						$("#m01").addClass("current");
						$("#p2").removeClass('msm');
						$("#p2").addClass("msm current");
						next_button = 'index';
						prev_button = 'index';
						rebind_navi_btns();
					}
					else 
						if (hash == "clients.html") {
							$("#m02").addClass("current");
							$("#c1").removeClass('msm');
							$("#c1").addClass("msm current");
							next_button = 'clients02';
							prev_button = 'clients02';
							rebind_navi_btns();
						}
						else 
							if (hash == "clients02.html") {
								$("#m02").addClass("current");
								$("#c2").removeClass('msm');
								$("#c2").addClass("msm current");
								next_button = 'clients';
								prev_button = 'clients';
								rebind_navi_btns();
							}
							else {
								$("#m03").addClass("current");
							}
				// restore ajax loaded state
				$.post(hash,function(data){
					data = data.substring((data.indexOf('<title>') + 7), data.indexOf('</title>'));
					document.title = data;
				},'html');
				$("#box-content-height-wrapper").load(hash + " #box-content", {}, add_borders);
			}
			else {
				// start page
				$("#menu > li").removeClass('current');
				$("#menu > li > ul > li").removeClass('msm current');
				$("#menu > li > ul > li").removeClass('msm');
				$("#menu > li > ul > li").addClass('msm');
				$("#m01").addClass("current");
				$("#p1").removeClass('msm');
				$("#p1").addClass("msm current");
				// start page
				$.post('index.html',function(data){
					data = data.substring((data.indexOf('<title>') + 7), data.indexOf('</title>'));
					document.title = data;
				},'html');
				$("#box-content-height-wrapper").load('index.html #box-content', {}, add_borders);
			}
		});
	}
}

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

var all_pages = 2;
var current_page = 1;


$(document).ready(function() {
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
	
	// set onlick event for buttons
	$("a[@class='history']").click(function(){
	 
		// moves to a new page. 
		// pageload is called at once. 
		$.historyLoad(this.href);
		$("#menu > li").removeClass('current');
		$(this).parent().parent().addClass('current');
		return false;
	});



/* gallery */

$('#gallery').cycle({
    fx: 'scrollHorz',
    timeout: 0,
    speed:250,
    next:   '#next', 
    prev:   '#prev'
});


$('#gallery-text').cycle({
    fx: 'scrollUp',
    timeout: 0,
    speed:250,
    next:   '#next', 
    prev:   '#prev' 
});

$('#arrow-left-gallery').show();
$('#arrow-right-gallery').show();

});

function nextX(){
	$('#next').click();
}

setInterval("nextX()",25000);
