$(document).ready(init);

showtips = getCookie("NEWshowcovertips");
if (showtips == null) showtips = 'true';

/*
 * menuExpandable.js - implements an expandable menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 * N.B. Altered for mixKylie.co.uk
 */

function initializeMenu(menuId, actuatorId) {
 var menu = document.getElementById(menuId);
 var actuator = document.getElementById(actuatorId);
    
 actuator.onclick = function() {
  var className = menu.className;
  if (className == "openMenu") {
   $("#"+menuId).slideUp("normal",function() {menu.className = "menu";});
  } else {
   /*menu.className = "openMenu";*/
   $("#"+menuId).slideDown("normal",function() {menu.className = "openMenu";});
  }
  return false;
 }
}

function init() {
 initializeMenu("discoChoice", "discoOpener");
 initializeMenu("guideChoice", "guideOpener");
 initializeMenu("collectionChoice", "collectionOpener");

 $("img.coverPic").colorbox({href: function() {var ref=$(this).attr('alt');return 'coverpic.php?ref='+ref;}});
 $("dl.quiz a").colorbox({iframe: true,innerWidth: '500px',innerHeight: '400px'});
 $("a.audio_link").click(function() {play_audio(this);return false;});
 $(".autoclear").autoclear();
 $("a.zoom").colorbox();
 
 if (showtips == 'true') {
  $("a.jTipInfo").hover(function(){JT_show(this,1);},function(){$('#JT').remove()}).click(function(){return false});
  $("a.jTipUnco").hover(function(){JT_show(this,2);},function(){$('#JT').remove()}).click(function(){return false});
  $("a.jTipUnof").hover(function(){JT_show(this,3);},function(){$('#JT').remove()}).click(function(){return false});
  $("a.jTipScan").hover(function(){JT_show(this,4);},function(){$('#JT').remove()});
  $("a.jTipComment").hover(function(){JT_show(this,5);},function(){$('#JT').remove()}).click(function(){return false});
  $("a.jTipRelease").hover(function(){JT_show(this,6);},function(){$('#JT').remove()}).click(function(){return false});
  $("a.jTipGuide").hover(function(){JT_show(this,7);},function(){$('#JT').remove()}).click(function(){return false});
  $("a.jTipUpdates").hover(function(){JT_show(this,8);},function(){$('#JT').remove()}).click(function(){return false});
 }
 
 $.widget("custom.catcomplete", $.ui.autocomplete, {
  _renderMenu: function(ul,items) {
   var self = this,currentCategory = "";
   $.each(items,function(index,item) {
    if (item.type != currentCategory) {
     if (item.type == 'r') {
      ul.append("<li class='ui-autocomplete-category'>Releases</li>");
     } else {
      ul.append("<li class='ui-autocomplete-category'>Tracks</li>");
     }
     currentCategory = item.type;
    }
    self._renderItem(ul,item);
   });
  }
 });
 
 $("#search_releases").autocomplete({source: "ajax/search_disco.php?type=r",minLength: 2,select: function(event, ui) {if (ui.item.type == 'r') {window.location='index.php?p=discotitle&ref='+ui.item.id;} else {window.location='index.php?p=track&ref='+ui.item.id;}}});
 $("#search_tracks").autocomplete({source: "ajax/search_disco.php?type=t",minLength: 2,select: function(event, ui) {if (ui.item.type == 'r') {window.location='index.php?p=discotitle&ref='+ui.item.id;} else {window.location='index.php?p=track&ref='+ui.item.id;}}});
 $(".search_disco").catcomplete({source: "ajax/search_disco.php",minLength: 2,select: function(event, ui) {if (ui.item.type == 'r') {window.location='index.php?p=discotitle&ref='+ui.item.id;} else {window.location='index.php?p=track&ref='+ui.item.id;}}});

 $('#albums_button').click(toggle_menu);
 $('#singles_button').click(toggle_menu);
 $('.load_note').fadeOut();
}


function toggle_menu(event) {
 if (event.target.id=='albums_button') {
  if ($('#albums_menu').is(':visible')) {
   $('#albums_menu').fadeOut();
  } else {
   $('#singles_menu:visible').fadeOut();
   $('#albums_menu').fadeIn();
  }
 } else {
  if ($('#singles_menu').is(':visible')) {
   $('#singles_menu').fadeOut();
  } else {
   $('#albums_menu:visible').fadeOut();
   $('#singles_menu').fadeIn();
  }
 }
}



function play_audio(link_obj) {
 url = link_obj.href;
 ref = url.substr(url.indexOf('ref=')+4);
 $("#container").animate({marginBottom: '44px'},'fast');
 $("#audio_track").html("Loading track title...");
 $("#audio_track").load("ajax/track_title.php?ref="+ref);
 $("#audio_container").fadeIn('fast',function() {top_player = document.getElementById("audio_player");top_player.sendEvent("load","audio/"+ref+".mp3");top_player.sendEvent("play");});
}


function JT_show(tipObject,tipType){
 url = tipObject.href;

 var width = 250;
 
 if (tipType == 1) {
  title = "mixKylie track information";
  width = 320;
 } else if (tipType == 2) {
  title = "mixKylie unconfirmed track";
 } else if (tipType == 3) {
  title = "mixKylie unofficial track";
 } else if (tipType == 4) {
  title = "mixKylie cover image";
  ref = url.substr(url.indexOf('ref=')+4);
  url = "ajax/popup_cover.php?ref="+ref;
 } else if (tipType == 5) {
  title = "mixKylie user comments";
  width = 320;
 } else if (tipType == 6) {
  title = "mixKylie release information";
  width = 320;
 } else if (tipType == 7) {
  title = tipObject.title;
  width = 320;
 } else if (tipType == 8) {
  title = "mixKylie site updates";
  width = 320;
 } else {
  title = "&nbsp;";
 }
 
 var de = document.documentElement;
 var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
 var hasArea = w - getAbsoluteLeft(tipObject);
 clickElementy = getAbsoluteTop(tipObject) - 3;

 if (hasArea > width + 90 && tipType!=7) {
  $("body").append("<div id='JT' style='width:"+width+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");
  var arrowOffset = getElementWidth(tipObject) + 11;
  var clickElementx = getAbsoluteLeft(tipObject) + arrowOffset;
 } else {
  $("body").append("<div id='JT' style='width:"+width+"px'><div id='JT_arrow_right' style='left:"+(width+1)+"px'></div><div id='JT_close_right'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");
  var clickElementx = getAbsoluteLeft(tipObject) - (width + 15);
 }

 $('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
 
 if (tipType == 4) {
  $('#JT').fadeIn("fast",function() {$.get(url,handleResponse);});
 } else {
  $('#JT').fadeIn("fast",function() {$('#JT_copy').load(url,function() {adjustPosition(clickElementy)});});
 }
}

function handleResponse(xmlDocument) {
 filename = $("filename",xmlDocument).text();
 title = $("title",xmlDocument).text();
 label = $("label",xmlDocument).text();
 catno = $("catno",xmlDocument).text();
 ean = $("ean",xmlDocument).text();
 
 if (filename != "") {
  $('#JT_copy').empty().append("<img style='float: left; margin-right: 5px;' src='images/covers/"+filename+"' /><p><strong>"+title+"</strong></p><p>"+label+" "+catno+"<br />"+ean+"</p><p style='clear: both;'></p>");
  adjustPosition(clickElementy);
 } else {
  $('#JT_copy').empty().append("<p>No cover image available.</p><p>If you have this release and could scan the cover for me, please let me know via the Contact form.</p>")
  adjustPosition(clickElementy);
 }
}

function adjustPosition(clickElementy) {
 var jtip_height = $('#JT').height();

 if (window.innerHeight) {
  scroll_position = window.pageYOffset;
 } else if (document.documentElement && document.documentElement.scrollTop) {
  scroll_position = document.documentElement.scrollTop;
 } else if (document.body) {
  scroll_position = document.body.scrollTop;
 }
 window_height = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;

 if ((scroll_position + window_height) - clickElementy < jtip_height ) {
  var adjusted_top = (window_height - jtip_height) - 6 + scroll_position;
  if ( adjusted_top - scroll_position < 0 ) {
   $('#JT').css({top: scroll_position + 1});
  } else {
   $('#JT').css({top: adjusted_top});
  }
  var amount_adjusted = clickElementy - adjusted_top;
  $('#JT_arrow_left').css({marginTop: amount_adjusted});
  $('#JT_arrow_right').css({marginTop: amount_adjusted});
 }
}

function getElementWidth(o) {
 return o.offsetWidth;
}

function getAbsoluteLeft(o) {
 oLeft = o.offsetLeft;
 while (o.offsetParent!=null) {
  oParent = o.offsetParent;
  oLeft += oParent.offsetLeft;
  o = oParent;
 }
 return oLeft;
}

function getAbsoluteTop(o) {
 oTop = o.offsetTop;
 while (o.offsetParent!=null) {
  oParent = o.offsetParent;
  oTop += oParent.offsetTop;
  o = oParent;
 }
 return oTop;
}


function displayMessage() {
 $('.filter_note').hide();
 country = document.getElementById('filter_form').country.value;
 if (country == 'EU') {
  $('#filter_note_EU').show();
 } else if (country == 'AS') {
  $('#filter_note_AS').show();
 } else if (country == 'FR' || country == 'GE' || country == 'NL' || country == 'SW' || country == 'GR' || country == 'BG' || country == 'IT' || country == 'SL') {
  $('#filter_note_EU2').show();
 } else if (country == 'SG' || country == 'TW' || country == 'TH' || country == 'IN' || country == 'HK' || country == 'CH') {
  $('#filter_note_AS2').show();
 } else {
  $('#filter_note').show();
 }
}

function resetOptions() {
 $('.filter_note').fadeOut();
 $('#filter_note').fadeIn();
 filterForm = document.getElementById("filter_form");
 filterForm.type.value='0';
 filterForm.country.value='0';
 filterForm.release.value='0';
 filterForm.media.value='0';
 if (filterForm.year) filterForm.year.value='0';
 if (filterForm.show) filterForm.show.value='coll';
 if (filterForm.display) filterForm.display.value='image';
}

function setCookie(name, value, expire) {
 document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

function getCookie(Name) {
 var search = Name + "=";
 if (document.cookie.length > 0) {
  offset = document.cookie.indexOf(search);
  if (offset != -1) {
   offset += search.length;
   end = document.cookie.indexOf(";", offset)
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(offset, end));
  }
 }
}

function deleteComment(idNum) {
 sure = confirm("Are you really sure you want to delete this comment?\n\nYou won't be able to get it back!");
 if (sure) {
  document.getElementById('deleteForm').id.value=idNum;
  document.getElementById('deleteForm').submit();
 }
}
