var initialized = false;

/* 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'></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").html("<iframe id='GB_frame' src='"+url+"' allowtransparency='true'></iframe>");
  $("#GB_overlay").fadeIn(500, function() {
	GB_position();
	$("#GB_window").slideDown(500);
  });
  
}

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").fadeOut(1000,function(){$('div.transparent.active').removeClass('active');});
}

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");
}

var advanced = true;
var superior = /webkit/.test( navigator.userAgent.toLowerCase() );
if ($.browser.msie){advanced = false;}

//SORT TYPES
//SORT TYPES
var sortalbum = "asc";
var sortartist = "asc";
var sortcnumber = "asc";
var sortlength = "asc";
var sortrating = "asc";
var sortrelease = "asc";
var sortcolor = "asc";
var 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;
if (advanced) {
	var id = idArray[z].id;
	$('#'+id).animate({top:(Math.floor(q/perrow)*100)+225+"px",
	left: (((Math.floor(q/perrow))+(q-((Math.floor(q/perrow))*perrow)) * 100))+10-(Math.floor(q/perrow))+padding+"px"}, 250).css('visibility','visible');
}
else {
	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";
	idArray[z].style.visibility = "visible";
}
z++;
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) {
if (advanced) {
	var id = shuffleArray[z].id;
	$('#'+id).show(100).animate({top: Math.floor(Math.random()*(window.innerHeight-340))+225+"px",
	left: Math.floor(Math.random()*(window.innerWidth-210))+100+"px"}, 250);
}
else {
	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";
	idArray[z].style.opacity = "1.0";
}
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.id).hide(100);
}
else {
$('#'+divid.id).show(100);
}
total++;
}
sortby('cnumber','asc');
GB_hide();
}
}

function viewall() {
total = 1;
while (divid = document.getElementById('div'+total)) {
$('#'+divid.id).show(100);
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();}
	$('.release').draggable({ stack: '.release' });
	if (superior) {$('img.disc').css('display','block');}
  }
});
}
