
window.onhashchange = funcRef;
lasthash='';
function funcRef(){
if(window.location.hash != lasthash) {
loadSite(window.location.hash);
}
}

$(document).ready(function() {

});

function contactMe() {
var name=document.getElementById('contact_me_name').value;
var email=document.getElementById('contact_me_email').value;
var message=document.getElementById('contact_me_message').value;

var url = '#!/';
var params = 'site=contact-me&sent=1&name='+encodeURIComponent(name)+'&email='+encodeURIComponent(email)+'&message='+encodeURIComponent(message);
loadSite(url,params);

return false;
}
function loadSite(site, post) {
if(typeof site=='object') {
site=site.href;
}

site=site.split('#');
site=site[1];
site=site.substr(1);
_gaq.push(['_trackPageview','#!'+site]);
if(typeof post == 'undefined') {
post=null;
}
lasthash='#!'+site;
if(window.location.hash=='') {
location.replace('/#!'+site);
}else{
window.location.hash='#!'+site;
}

var content=document.getElementById('content');
var content2=document.getElementById('content2');
content.style.height=content.offsetHeight+'px';
$(content2).animate({opacity:0},'fast',function() {

var xmlHttp = null;
try {
xmlHttp = new XMLHttpRequest();
} catch(e) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
xmlHttp = null;
}
}
}

if (xmlHttp) {
if(post) {
post=post+'&blank=1';
xmlHttp.open('POST', site, true);
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlHttp.setRequestHeader('Content-length', post.length);
xmlHttp.setRequestHeader('Connection', 'close')
} else {
if(site.indexOf('?')>-1) {
site=site+'&blank=1';
}else{
site=site+'?blank=1';
}
xmlHttp.open('GET', site, true);
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 404) {
window.location.href='error-404';
}else{
content.style.backgroundImage='none';
content2.innerHTML='<div style="overflow:hidden"></div>'+xmlHttp.responseText+'<div style="overflow:hidden"></div>';

var title_div=document.getElementById('title_div');
document.title=title_div.getAttribute('title');
title_div.parentNode.removeChild(title_div);

var textscript=content2.getElementsByTagName('script');
if (textscript.length>0) {
eval(textscript[0].innerHTML);
}

$(content).animate({height:content2.offsetHeight+'px'},function(){
content.style.height='auto';
$(content2).animate({opacity:1})
});
}
}
};
xmlHttp.send(post);
content.style.backgroundImage='url(img/ajax-loader.gif)';
}
return false;


});
return false;


}
function older_news() {
var show_oldernews=document.getElementById('show_oldernews');
var oldernews=document.getElementById('oldernews');
var oldernews2=document.getElementById('oldernews2');
var picnews=document.getElementById('picnews');
$(show_oldernews).animate({opacity:0,height:0},'fast',function(){$(oldernews).animate({height:oldernews2.offsetHeight})});
$(picnews).animate({opacity:0,height:0},'fast');

return false;
}

document.onkeyup = doSomething;

function doSomething(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code==37) {
remove_textnews();
return gallery_next(false);
} else if (code==39) {
remove_textnews();
return gallery_next(true);
}
}

function loadImage(obj,txt,bold) {
if(typeof bold == 'undefined') {
bold=false;
}

var img=document.createElement('img');
img.onload=function(){obj.appendChild(this);this.height=parseInt(this.offsetHeight);}
img.src='?'+(bold ? 'bold=1&' : '')+'txt2img='+encodeURIComponent(txt);
img.alt=txt;

}

function remove_textnews() {
var textnews=document.getElementById('textnews');
if(textnews) {
$(textnews).animate({height:0,opacity:0},'fast',function(){
textnews.parentNode.removeChild(textnews);
});
}
}
function gallery_next(fore) {

var picture=document.getElementById('picture');
var picture_box=document.getElementById('picture_box');
if (!picture) {
return false;
}
var add=(fore ? 1 : -1);
if (gallery_current+add<0) {
return false;
}

$(picture).animate({opacity:0},'fast',function() {
gallery_current=gallery_current+add;
if (gallery_current>=gallery.length) {
gallery_current=0;
}else if (gallery_current<0) {
return false;
}

var whash=(gallery_code == 'news' ? '' : 'gallery-')+gallery_code+(gallery_current > -1 ? '-'+gallery[gallery_current].auto_id : '');
if(window.location.pathname!='/'+whash) {
lasthash='#!'+whash;
var whash2='#!'+whash;
if(whash2.indexOf(window.location.hash)==0) {
location.replace('/#!'+whash);
} else {
window.location.hash='#!'+whash;
}
}else{
if(window.location.hash.length>1) {
lasthash='#';
window.location.hash='#';
}
}
if(!document.getElementById('textnews')) {
document.title=(gallery[gallery_current].person != '' ? gallery[gallery_current].person : gallery[gallery_current].location + ' ' + gallery[gallery_current].year)+' | BATUDISTE › '+(gallery_name != 'Photo Stream' ? 'Galleries › ' : '')+gallery_name;
}
var description_div=document.getElementById('description_div');
while(description_div.firstChild) {
description_div.removeChild(description_div.firstChild);
}
var p=document.createElement('p');
p.className='person';

description_div.appendChild(p);
var pNode=p;
var pNode2=p;
if (gallery[gallery_current].person_url != '') {
var a=document.createElement('a');
a.target='_blank';
a.onfocus=function(){this.blur();}
a.href=gallery[gallery_current].person_url;
p.appendChild(a);
pNode=a;
}
if(gallery[gallery_current].person != '') {
var span=document.createElement('span');
pNode.appendChild(span);
loadImage(span,gallery[gallery_current].person,true);
}

var span=document.createElement('span');
pNode2.appendChild(span);
var iDate2 = gallery[gallery_current].date.split(' ');
iDate2 = iDate2[0].split('-');
var iDate=new Date(iDate2[0],iDate2[1]-1,iDate2[2]);
loadImage(span,(gallery[gallery_current].person != '' ? '. ' : '') + gallery[gallery_current].location + ' ' + iDate.getFullYear()  + '.');

if (gallery[gallery_current].sample_title) {
var p=document.createElement('p');
p.className='description';
var span=document.createElement('span');
p.appendChild(span);
loadImage(span,'Sound contains a sample from ');

var a=document.createElement('a');
a.href=gallery[gallery_current].sample_url;
a.onfocus=function(){this.blur();}
a.target='_blank';
var span=document.createElement('span');
a.appendChild(span);
p.appendChild(a);
loadImage(span,'‘' + gallery[gallery_current].sample_title + '’');

var span=document.createElement('span');
p.appendChild(span);
loadImage(span,' by ');

var a=document.createElement('a');
a.href=gallery[gallery_current].sample_artist_url;
a.target='_blank';
a.onfocus=function(){this.blur();}
var span=document.createElement('span');
a.appendChild(span);
p.appendChild(a);
loadImage(span,'' + gallery[gallery_current].sample_artist + '');
var span=document.createElement('span');
p.appendChild(span);
loadImage(span,'.');
description_div.appendChild(p);
}
var media=document.getElementById('media');
var video=document.createElement('img');
video.alt=(gallery[gallery_current].person ? gallery[gallery_current].person : gallery[gallery_current].location + ' ' + iDate.getFullYear());
video.onclick=function(){
remove_textnews();
gallery_next(true);
}
var vWidth=gallery[gallery_current].width;
var vHeight=gallery[gallery_current].height;
video.width=vWidth;
video.height=vHeight;
while(media.firstChild) {
media.removeChild(media.firstChild);
}
media.appendChild(video);
video.onload=function(){
picture_box.style.backgroundImage='none';
$(picture_box).animate({height:picture.offsetHeight},function(){
$(picture).animate({opacity:1})
});
if (gallery[gallery_current].type=='video') {
var video=document.createElement('video');
var vWidth=gallery[gallery_current].width;
var vHeight=gallery[gallery_current].height;video.setAttribute('poster',gallery[gallery_current].url+'.jpg');
video.setAttribute('width',vWidth);
video.setAttribute('height',vHeight);
video.setAttribute('autoplay','autoplay');
video.setAttribute('controls','controls');
video.setAttribute('onplay','this.removeAttribute(\'controls\');');
video.onclick=function(){video.play()};
var types=[{extension: 'mp4',type:'video/mp4'},{extension: 'webm',type:'video/webm'},{extension: 'ogv',type:'video/ogg'}];
for (var i=0;i<types.length;i++) {
var source=document.createElement('source');
source.setAttribute('src',gallery[gallery_current].url+'.'+types[i].extension);
source.setAttribute('type',types[i].type);
video.appendChild(source);
}

var embed=document.createElement('embed');
embed.setAttribute('allowfullscreen','false');
embed.setAttribute('allowscriptaccess','always');
embed.setAttribute('wmode','transparent');
embed.setAttribute('src','flash/player.swf');
embed.setAttribute('width',vWidth);
embed.setAttribute('height',vHeight);
embed.setAttribute('flashvars','controlbar=over&autostart=true&file='+escape('../'+gallery[gallery_current].url+'.mp4')+'&image='+escape('../'+gallery[gallery_current].url+'.jpg'));
video.appendChild(embed);

media.replaceChild(video,media.childNodes[0]);
}
};
picture_box.style.backgroundImage='url(img/ajax-loader.gif)';
video.src=gallery[gallery_current].url+(gallery[gallery_current].type=='video' ? '.jpg' : '');
if(document.getElementById('fore_btn').getElementsByTagName('img').length>0) {
var fore_btn=document.getElementById('fore_btn').getElementsByTagName('img')[0];
}else{
var fore_btn=document.getElementById('fore_btn').getElementsByTagName('span')[0];
}
if (gallery_current<gallery.length-1) {
var img_preload=document.createElement('img');
img_preload.src=gallery[gallery_current+1].url+(gallery[gallery_current+1].type=='video' ? '.jpg' : '');
if (fore_btn.alt == 'Repeat') {
fore_btn.alt = 'Next';
fore_btn.src='?txt2img=Next+»';
}
}else{
fore_btn.alt = 'Repeat';
fore_btn.src='?txt2img=Repeat';
}

});
return false;
}





var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16022597-9']);
_gaq.push(['_gat._anonymizeIp']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

function smallsize(vWidth,vHeight) {

var maxLength=(window.innerWidth>window.innerHeight ? window.innerHeight : window.innerWidth) - 10;
if(vWidth>=vHeight && vWidth>maxLength) {
var factor=(maxLength)/vWidth;
vWidth=maxLength;
vHeight=vHeight*factor;
} else if (vWidth<vHeight && vHeight>maxLength) {
var factor=(maxLength)/vHeight;
vHeight=maxLength;
vWidth=vWidth*factor;
}
return [vWidth,vHeight];
}

function debug() {
alert(document.getElementById('media').innerHTML);
}




$(function(){

// Bind the event.
$(window).hashchange( function(){
// Alerts every time the hash changes!

})

// Trigger the event (useful on page load).
//$(window).hashchange();

});




