var loaded = 0;

onload = function () {
    if(loaded == 0) {
        setbuybuttons();
        loaded = 1;
    }
    
    if(document.body.getAttribute('class') == 'artist-live') {
		setLiveBkgd();

	}
}

function setbuybuttons() {
    
    var buttondivs = getElementsByClassName("buyrelease", "div");

    for( i = 0 ; i < buttondivs.length ; i++ ) {
        identifier = buttondivs[i].id.substring(9);
        ahah("/proxy/buybuttons.php?identifier=" + identifier, buttondivs[i].id);
    }

}


function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

function ahah(url,target) {
    document.getElementById(target).innerHTML = 'loading...';
    if (window.XMLHttpRequest) {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function() {ahahDone(target, req);};
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        var req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {ahahDone(target, req);};
            req.open("GET", url, true);
            req.send();
        }
    }
} 

function ahahDone(target, req) {
   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200 || req.status == 304) {
           results = req.responseText;
           document.getElementById(target).innerHTML = results;
       } else {

       }
   }
}


function fieldFocus(element, text)
{
    if(text == element.value) {
        element.value = "";
    }
}
function fieldBlur(element, text)
{
    if("" == element.value) {
        element.value = text;
    }
}			

var timeout = 0;

function toggleArtistMenu() {
	clearTimeout(timeout);
	var artistMenu = document.getElementById('artistmenu');
	
	if((artistMenu.getAttribute('class') == 'active') || (artistMenu.getAttribute('className') == 'active')) {
		artistMenu.setAttribute('class', '');
		artistMenu.setAttribute('className', '');
	} else {
		artistMenu.setAttribute('class', 'active');
		artistMenu.setAttribute('className', 'active');
	}
}

function showArtistMenu() {
	timeout = window.setTimeout("showArtistMenuAction()", 400);
}

function cancelArtistMenu() {
	clearTimeout(timeout);
}

function showArtistMenuAction() {
	var artistMenu = document.getElementById('artistmenu');
	artistMenu.setAttribute('class', 'active');
	artistMenu.setAttribute('className', 'active');
}

var timeout_twitter = 0;

function toggleTwitterMenu() {
	clearTimeout(timeout_twitter);
	var twitterMenu = document.getElementById('twittermenu');
	
	if((twitterMenu.getAttribute('class') == 'active') || (twitterMenu.getAttribute('className') == 'active')) {
		twitterMenu.setAttribute('class', '');
		twitterMenu.setAttribute('className', '');
	} else {
		twitterMenu.setAttribute('class', 'active');
		twitterMenu.setAttribute('className', 'active');
	}
}

function showTwitterMenu() {
	timeout_twitter = window.setTimeout("showTwitterMenuAction()", 400);
}

function cancelTwitterMenu() {
	clearTimeout(timeout_twitter);
}

function showTwitterMenuAction() {
	var twitterMenu = document.getElementById('twittermenu');
	twitterMenu.setAttribute('class', 'active');
	twitterMenu.setAttribute('className', 'active');
}

/* map stuff */

function setLiveBkgd() {
	
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
		 myWidth = window.innerWidth;
		 myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
	}
	
	document.getElementById("largemap").style.height = (myHeight-50) + 'px';
	
	
	
	map.checkResize();
	
	map.setCenter(new GLatLng(51.5153261, -0.2073428), 7);
}

var mapCentred = 0;

function plotAddress(map, geocoder, address, html) {
        
        
        zoom = 7;
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              //alert(address + " not found");
              
            } else {
              
              var marker = new GMarker(point);
              map.addOverlay(marker);
              
              
              GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			  });
              
            }
          }
        );
    }
    
function plotLatLong(map, lat, long, html) {
	
	zoom = 7;
	
	var point = new GLatLng(lat, long);
	  
	  var marker = new GMarker(point);
	  map.addOverlay(marker);
	  
	  GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	  });
	
}