	
$(document).ready(function () {
	
	var DOMAIN = "SHRIEK MUSIC & SOUND DESIGN";
		
	bind	=	function(){
		var r 	= new Date().getTime();
		$(".item").bind("click", function(e){
				$(this).animate({opacity: 0}, 250);
				$(this).siblings().animate({opacity: 0}, 250);
				$(this).unbind("mouseover");
				$(this).unbind("mouseout");
				$("#info").empty();
		});
	};
	
	fade	=	function(){
		$('.item').animate({opacity: 0.25}, 1000);
	};
	
	
	init	=	function(){
		removeVideo();
		bind();
		$('.item').css({opacity:".25", filter:"alpha(opacity = 25)"}).hide().EffectChain({duration:.1,delay:0,effect:"fadeIn",onComplete:fade});
		
			
		$(".item").bind("mouseover", function(e){
			$(this).css({opacity:"1", filter:"alpha(opacity = 100)"});
			$(this).siblings().css({opacity:".25", filter:"alpha(opacity = 25)"});
			var top = $(this).offset().top - $(this).parent().offset().top;
			var id = $(this).attr("id").split("-")[1];
			$("#info").css({"display":"block","padding-top":top+"px"}).html($("#info-"+id).html());
		});
			
		$(".item").bind("mouseout", function(e){
			//$(this).animate({opacity: 0.25}, 200);
			$(this).css({opacity:".25", filter:"alpha(opacity = 25)"});
			$(this).siblings().css({opacity:".25", filter:"alpha(opacity = 25)"});
			$("#info").empty();
		});
	};
	
	emptyItems	=	function(){
		$('#items').html('');
	};
	
	
	videoTitle	=	function(){
		document.title = document.title +' / '+ $('h2').text();
	};
		
	closeVideo	=	function(){
		$("#video").fadeTo("slow", 0, function(){
			$("#cont").html('');
			$(this).unbind();
			$(this).attr("style", "");
		});
	};
	
	removeVideo	=	function(){
		$("#video").attr("style", "").unbind();
		$("#cont").html('');
	};

	nextVideo	=	function(id, page){
		loading(true);
		$.get("/"+page+"/video/" + id + "/" ,function(reply){
			$("#preview").replaceWith(reply);
			loading(false);
		});
	};
	
		
	toTitleCase	= function(str) {
		return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
	} ;     
	
	// Custom SWFAddress and Ajax handling
	getTransport	=	function() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				return new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				return new ActiveXObject('Microsoft.XMLHTTP');
			}
		}
	};
	
	updateChange = function(xhr, video) {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				if(video){
					var json = $.parseJSON(xhr.responseText);
					$("#video").effect("size", {to: {width: 644,height: 480} }, 500, function(){$("#container"+json.id).html(json.player);emptyItems();});
					$("#cont").html(json.reply);
					
				}
				else{
					$('#content').html(xhr.responseText);
				}
				
				loading(false);
			} else {
				$("#content").html('<h2>Page not found</h2>');
			}
		}
	};
	
	formatTitle  = function(title) {
		activeMenu(title);
		return DOMAIN + ' ' + (title != '/' ? ' / ' + toTitleCase(title.substr(1, title.length - 2).replace(/\//g, ' / ')) : '');
	};
	
	
	activeMenu	=	function(t){
		t = (t != '/' ? t.substr(1, t.length - 2).replace(/\//g, '') : '');
		$("#menu li").each(function(){$(this).removeClass('active');});
		if('' == t){
			$("#branding").addClass("active");	
		}
		else{
			$("#"+t).addClass("active");	
		}
	};
	
	handleChange	= function(event) {
		
		loading(true);
		
		var r = new Date().getTime();
		
		var videoCall = false;

		var parameters 	= '';
		for (var p in event.parameters) {
			if(p == 'id'){
				videoCall 	= true;
			}
			parameters += '&' + p + '=' + event.parameters[p];
		}
		parameters += '&r=' + r;
		
		var xhr = getTransport();
		xhr.onreadystatechange = function() {
			updateChange(xhr, videoCall);
		};
		
		xhr.open('get', 'data/?page=' + event.path + parameters, true);
		xhr.send('');
		
		SWFAddress.setTitle(formatTitle(event.path)); 
		
	};
	
	
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
	
	
	
	Gload	=	function(){
		var __center_x	=	"51.511186";
		var	__center_y	=	"-0.134368";
		
			
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
			
		//var point = new GlatLng(__center_x, __center_y);
			
		map.setCenter(new GLatLng(__center_x, __center_y), 16);
			
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.shriek-music.com/_img/icon-shadow.png";
		baseIcon.iconSize = new GSize(30, 35);
		baseIcon.shadowSize = new GSize(37, 35);
		baseIcon.iconAnchor = new GPoint(12, 40);
		baseIcon.infoWindowAnchor = new GPoint(12, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);	
		var homeicon = new GIcon(baseIcon);
		homeicon.image = "http://www.shriek-music.com/_img/icon.png";
			
		var marker = new GMarker(new GLatLng(__center_x, __center_y), {icon: homeicon});
			
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("<p>39 Great Windmill St, London</p><p>W1D 7LX</p><p>T: +44(0)20 7734 5558</p>");
		});		
			
		map.addOverlay(marker);
	};
	
	preloadImages = function(){
		for(var i = 1; i<arguments.length; i++){
			$("#img"+i).attr("src", arguments[i-1]);
		}
		init();
	}	
	
	
	$('a[rel*=spemail]').spemail('|,:','linkbase');	


});
	
loading	=	function(start){
	if(start == true){
		$('#loading').addClass('loading').html('loading');
	}
	else{
		$('#loading').removeClass('loading').html('');
	}
};




$.fn.spemail = function(opt,type){
	// Added this to be able to change what the options by default the | will be used as the first parameter an initiate the '@' and the : will initiate the '.'
	var opt_in = opt.split(",");
	var type;
	
	if (type == "linkbase")
	{
		this.each(function() {
			el = $(this);
			el.each(function(){
			// Finds the href with rel: 'whatever you want' and inserts mailto: and replaces the characters for real ones.
			// Done this way to keep consistancy throughout the emails on the site.
			el.attr('href','mailto:' + el.attr('href').replace(opt_in[0],'@').replace(opt_in[1],'.'));
			// Will add a title attribute to the link like: Email: whoever@whereever.com
			el.attr('title',el.attr('href').replace(opt_in[0],'@').replace(opt_in[1],'.').replace('mailto.','Email: '));
			// Will change the html of the link itself to the email address of the person.
			el.html(el.attr('href').replace(opt_in[0],'@').replace(opt_in[1],'.').replace('mailto.','').replace('mailto:',''));
			});
		});
	}
	else if(type == "mailbase") {
		this.each(function() {
			el = $(this);
			el.each(function(){
		    // Will create the whole href link with title and everything for the text provided. (probably the best option - to me at least
			el.html('<a href="mailto:'+el.html().replace(opt_in[0],'@').replace(opt_in[1],'.')+'" title="Email: '+el.html().replace(opt_in[0],'@').replace(opt_in[1],'.')+'">'+el.html().replace(opt_in[0],'@').replace(opt_in[1],'.')+'</a>');
			});
		});
	}
	else if(type == "plainbase") {
		// WILL JUST CHANGE TEXT TO THE EMAIL ON DOM REGULAT TEXT TO REGULAR TEXT	
		this.each(function() {
			el = $(this);
			el.each(function(){
			el.html(el.html().replace(opt_in[0],'@').replace(opt_in[1],'.'));
			});
		});
	}
};


/*======================================================================*\

	hacked original json.js into a jQuery plugin.
	It adds the two functions:
	$.toJSON(value)
	$.parseJSON(json_str, [safe]).
	http://jollytoad.googlepages.com/json.js

\*======================================================================*/

(function ($) {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            'array': function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            'number': function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            'object': function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            'string': function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

	$.toJSON = function(v) {
		var f = isNaN(v) ? s[typeof v] : s['number'];
		if (f) return f(v);
	};
	
	$.parseJSON = function(v, safe) {
		if (safe === undefined) safe = $.parseJSON.safe;
		if (safe && !/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(v))
			return undefined;
		return eval('('+v+')');
	};
	
	$.parseJSON.safe = false;

})(jQuery);
