
var curId = 0;
var lastY = 0;

function formatTitle(title) {

	//	title = title.toUpperCase();
	title = title.replace( /-/g, ' ' );
	title.replace( '/', '' );

	return '779ltd | ' + title;
}


function sliderClick(id){
	SWFAddress.setValue( "/works/view/" + id );
}

function handleChange(event) {

	var path = event.path;

	if( path == "/" ){
		hideModal();
		hideOverlay();
		//SWFAddress.setTitle( formatTitle( '') );
		return;
	}
	// add trailing slash if needed
	if (path.substr(path.length - 1) != '/') {
		path += '/';
	}

	if( path != '/' ){

		var arr = path.split( '/' );
		var title = arr[ arr.length-2 ];

		var adLink = document.getElementById( 'ADLink')
		if( adLink != null ){
			$("ADLink").share.url = "<" + baseUrl + "/#" + path + ">";
		}


		//	SWFAddress.setTitle( formatTitle( title ) );
		path = path.replace( '//', '/' );
		new Ajax.Updater( 'ajax', prefix + path, 
		{asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'ajax']} );
		showModal();
	}else{
		hideModal();
		hideOverlay();
	}
}

SWFAddress.addEventListener( SWFAddressEvent.CHANGE, handleChange );

/////// rollovers for the grid view //////


function over(divId){
	var div = document.getElementById( divId );
	div.style.background="#93bdbd";
}
function out(divId){
	var div = document.getElementById( divId );
	div.style.background="#fff";
}


////////// panel functions /////////

function showModal(){
	//showOverlay();
	document.getElementById( 'spinner' ).style.display = "";

	if( document.getElementById( 'modal' ).style.display == "none"){
		new Effect.Appear("modal", {duration: .6 });
	}
}

function hideModal(){

	//	SWFAddress.setValue( '' );
	new Effect.Fade("modal", {duration: .2});	
	document.getElementById( 'spinner' ).style.display = "none";
}


function showOverlay(){

	lastY = window.pageYOffset;

	var ajax = document.getElementById( 'ajax' );
	var anIn = false;

	if( ajax.style.display == "none" ){
		anIn = true;
	}


	if( $( 'sliderSwf' ) != null ){
		//			$( 'sliderSwf' ).style.display = "none";
		new Effect.Fade("sliderSwf", {duration:.5});	
		//	$( 'sliderSwf' ).disable();
	}
	//		if( slider.disable != null ){	
		//			slider.disable();
		//		}

		var share = document.getElementById( 'share_holder' );
		share.style.left = "50%";
		share.style.top = "50%";
		share.style.marginTop = "-185px";
		share.style.marginLeft = "450px";
		share.style.display = "none";
		new Effect.Appear("share_holder", {duration:.5, delay:.4});



		if( anIn ){

			if( navigator.appName == "Microsoft Internet Explorer"){
				ajax.style.display = "block";	
			}else{
				new Effect.Appear("ajax", { duration: .6 });	
			}

			//				

		}else{
			ajax.style.display = "block";
		}
		document.getElementById( 'spinner' ).style.display = "none";
	}

	function hideOverlay(){

		var share = document.getElementById( 'share_holder' );
		share.style.left = "0";
		share.style.top = "100%";
		share.style.marginLeft = "0";
		share.style.marginTop ="-35px";
		share.style.display = "none";
		new Effect.Appear(share, { duration: .3 });

		var item = document.getElementById( 'work_item_' + curId );
		if( item != null ){
			new Effect.Highlight(item);
		}

		if( $( 'sliderSwf' ) != null ){
			new Effect.Appear("sliderSwf", {duration:.5});
		}
		//	if( slider.enable != null ){	
			//		slider.enable();
			//	}

			if( document.getElementById( 'ajax' ).style.display != 'none' ){
				hideModal();
				if( navigator.appName == "Microsoft Internet Explorer"){
					clearAjax();
					$( 'ajax' ).style.display = "none";
				}else{

					new Effect.Fade("ajax", { duration: .6, afterFinish:clearAjax });
				}
				document.getElementById( 'spinner' ).style.display = "none";
			}
		}

		function formatForSingle(){

			var ajax = document.getElementById( 'ajax' );
			ajax.style.width = "720px";
			ajax.style.marginLeft = "-365px";

			var close = document.getElementById( 'closeBtn' );
			close.style.left = "690px";


			var share = document.getElementById( 'share_holder' );
			share.style.marginLeft = "334px";
		}

		function clearAjax(){
			var ajax = document.getElementById( 'ajax' );
			ajax.innerHtml = "";
			window.scroll( 0,lastY);
		}

		////////////////////////// UTILITIES ////////////////////////////////////
		function clearField( field, defaultVal ){
			if( field.value == defaultVal ){
				field.value = "";
			}
		}

