var initialized = false;

/*
//Google Analytics
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try {
var pageTracker = _gat._getTracker("UA-11993225-1");
pageTracker._setDomainName(".modicum.us");
pageTracker._trackPageview();
} catch(err) {}
*/

//AJAX

       function handleHttpResponse() {   
        if (http.readyState == 4) {
              if(http.status==200) {
                  var results=http.responseText;
              document.getElementById('info').innerHTML = results;
              }
              }
        }
       
function getHTTPObject() {
  var xmlhttp;

  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
   
}
  return xmlhttp;

 
}
var http = getHTTPObject(); // We create the HTTP Object


//DRAGGING
// Determine browser and version.
 
function Browser() {
 
  var ua, s, i;
 
  this.isIE    = false;
  this.isNS    = false;
  this.version = null;
 
  ua = navigator.userAgent;
 
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
 
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
 
  // Treat any other "Gecko" browser as NS 6.1.
 
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}
 
var browser = new Browser();
 
// Global object to hold drag information.
 
var dragObj = new Object();
//dragObj.zIndex = 60;
 
function dragStart(event, id) {
 
  var el;
  var x, y;
 
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.
 
  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;
 
    // If this is a text node, use its parent element.
 
    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }
 
  // Get cursor position with respect to the page.
 
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
 
  // Save starting positions of cursor and element.
 
  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
 
  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
 
  // Update element's z-index.
  preserveindex = dragObj.elNode.style.zIndex;
  dragObj.elNode.style.zIndex = 450;
 
  // Capture mousemove and mouseup events on the page.
 
  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}
 
function dragGo(event) {
 
  var x, y;
 
  // Get cursor position with respect to the page.
 
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
 
  // Move drag element by the same amount the cursor has moved.
 
  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
 
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}
 
function dragStop(event) {
 
 dragObj.elNode.style.zIndex = preserveindex;
 
 /*
 //DRAG TO CORNER FUNCTIONALITY
 if (event.clientX < 100) {
 if (event.clientY < 100) {
 GB_show(dragObj.elNode.title, "album.php?id="+dragObj.elNode.getAttribute("cnumber"));
 }
 }
 */
 
  // Stop capturing mousemove and mouseup events.
 
  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}
 
//]]>


/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_HEIGHT = 600;
var GB_WIDTH = 700;

function GB_show(caption, url, height, width) {
	//smoothScrollUp(true);
	document.body.style.overflow = "hidden";
	document.getElementById('cclogo').style.display = "block";
	if (initialized == false) {document.getElementById('navbar').innerHTML = "Welcome to <b>Modicum</b>. Click on the background to view our full catalog.";}
	else {document.getElementById('navbar').innerHTML = "";}
  GB_HEIGHT = height || 500;
  GB_WIDTH = width || 700;
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"' allowtransparency='true'></iframe></div>");
  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}

function GB_hide() {
  if (document.getElementById('colorpicker')) {closeColor();}
  document.body.style.overflow = "visible";
  document.getElementById('cclogo').style.display = "none";
  if (initialized == false) {populatepage();}
  $("#GB_frame").remove();
  document.getElementById('navbar').innerHTML = "<b>Instructions:</b> Double click any release to view. Feel free to sort or rearrange them as you wish.";
  $("#GB_window,#GB_overlay").hide();
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px" });
  $("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}

//SORT TYPES
//SORT TYPES
sortalbum = "asc";
sortartist = "asc";
sortcnumber = "asc";
sortlength = "asc";
sortrating = "asc";
sortrelease = "asc";
sortcolor = "asc";
globalSort = "asc";

function sortby(attribute,ascdesc) {

if (!ascdesc) {
/*
direction = globalSort;
if (globalSort != "asc") {globalSort = "asc";}
else if (globalSort != "desc") {globalSort = "desc";}
*/
if (attribute == "album") {direction = sortalbum; if (direction != "asc") {sortalbum = "asc";} else {sortalbum = "desc";}}
else if (attribute == "artist") {direction = sortartist; if (direction != "asc") {sortartist = "asc";} else {sortartist = "desc";}}
else if (attribute == "cnumber") {direction = sortcnumber; if (direction != "asc") {sortcnumber = "asc";} else {sortcnumber = "desc";}}
else if (attribute == "color") {direction = sortcolor; if (direction != "asc") {sortcolor = "asc";} else {sortcolor = "desc";}}
else if (attribute == "length") {direction = sortlength; if (direction != "asc") {sortlength = "asc";} else {sortlength = "desc";}}
else if (attribute == "rating") {direction = sortrating; if (direction != "asc") {sortrating = "asc";} else {sortrating = "desc";}}
else if (attribute == "release") {direction = sortrelease; if (direction != "asc") {sortrelease = "asc";} else {sortrelease = "desc";}}
}
else {direction = ascdesc;}

idArray=new Array();
attributeArray=new Array();
total=1;
affected=1;
while (divid = document.getElementById('div'+total)) {
if (divid.style.display != "none") {
idArray[affected] = divid;
attributeArray[affected] = divid.getAttribute(attribute);
affected++;
}
total++;
}
  for(x = 1; x < idArray.length; x++) {
    for(y = 1; y < (idArray.length-1); y++) {
	if (direction == "asc") {
      if(attributeArray[y].toUpperCase() > attributeArray[y+1].toUpperCase()) {
        holder = idArray[y+1];
        idArray[y+1] = idArray[y];
        idArray[y] = holder;
        holder2 = attributeArray[y+1];
        attributeArray[y+1] = attributeArray[y];
        attributeArray[y] = holder2;
      }}
	  if (direction == "desc") {
      if(attributeArray[y].toUpperCase() < attributeArray[y+1].toUpperCase()) {
        holder = idArray[y+1];
        idArray[y+1] = idArray[y];
        idArray[y] = holder;
        holder2 = attributeArray[y+1];
        attributeArray[y+1] = attributeArray[y];
        attributeArray[y] = holder2;
      }
    }}
  }
rearrange(1);
}

function rearrange(z) {
if (z <= idArray.length) {
q = z-1;
perrow = Math.floor((window.innerWidth-20)/100);
padding = (window.innerWidth - (perrow * 100))/2;
idArray[z].style.top = (Math.floor(q/perrow)*100)+225+"px";
idArray[z].style.left = (((Math.floor(q/perrow))+(q-((Math.floor(q/perrow))*perrow)) * 100))+10-(Math.floor(q/perrow))+padding+"px";;
z++
idArray[z].style.visibility = "visible";
setTimeout("rearrange("+z+")", 5);
}
}

function shuffle() {
shuffleArray=new Array();
total=1;
while (divid = document.getElementById('div'+total)) {
shuffleArray[total] = divid;
total++;
}
scatter(1);
}

function scatter(z) {
if (z <= shuffleArray.length) {
shuffleArray[z].style.top = Math.floor(Math.random()*(window.innerHeight-340))+225+"px";
shuffleArray[z].style.left = Math.floor(Math.random()*(window.innerWidth-210))+100+"px";
shuffleArray[z].style.display = "block";
shuffleArray[z].style.visibility = "visible";
z++
setTimeout("scatter("+z+")", 5);
}
}

function viewby(attribute, value) {
if (initialized == false) {populatepage(true,attribute,value);}
else {
total = 1;
while (divid = document.getElementById('div'+total)) {
if (divid.getAttribute(attribute) != value) {
divid.style.display = "none";
divid.style.visibility = "hidden";
}
else {
divid.style.display = "block";
divid.style.visibility = "visible";
}
total++;
}
sortby('cnumber','asc');
GB_hide();
}
}

function viewall() {
total = 1;
while (divid = document.getElementById('div'+total)) {
divid.style.display = "block";
divid.style.visibility = "visible";
total++;
}
}

//NEW POPULATE
function parseXml(xml) {  
  if ($.browser.msie) {  
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
    xmlDoc.loadXML(xml);  
    xml = xmlDoc;  
  }  
  return xml;  
}  
function populatepage(viewby,attribute,value) {
initialized = true;
$.ajax({
  url: 'releases.php',
  success: function(data) {
    $('body').append(data);
	if (viewby == true) {setTimeout("viewby('"+attribute+"','"+value+"')",100);}
	else {sortby('release','desc');viewall();}
  }
});
}