/*js functions for glossa-z admin*/

/*get the instance of the last "/" in the url - this gives us portability*/

var docUrl = document.URL.substring(0,(document.URL.lastIndexOf('/')));

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", parseInt( $(window).width() - this.width() ) / parseInt(2+$(window).scrollLeft()) + "px");
    return this;
}


function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

jQuery.extend(
    jQuery.expr[':'], {
        regex: function(a, i, m, r) {
            var r = new RegExp(m[3], 'i');
            return r.test(jQuery(a).text());
        }
    }
);


function get_gloss_data(id,fct) {
ajaxurl = docUrl+"/wp-content/plugins/glossa-z/glossaz-ajax.php";

//drop jquery AJAX requests here - may not need the above createXMLHttpRequest
jQuery.ajax({
type:"POST",
url: ajaxurl,
data: "id="+id+"&fn="+fct,
success: function(html){
document.getElementById('listcontent').innerHTML = html;
}
});
}//end function


jQuery(window).load(function() {//stuff to do once the actual page has loaded.
var str = getQuerystring('goto');
if (str.length > 0) {
//var splitstr = str.replace(/\+/g,' ');
var splitstr = unescape(str);
$("#wpglosssummary > dt:regex('^"+splitstr+"$'):first").each(function() {
var tpos = $(this).position();
$(this).css('background-color','#ffaaaa');
var t = tpos.top;
window.scrollTo(0,t);
});
}

});


jQuery(document).ready(function($) {

if ($('#listcontent').length > 0) {
//create loader div
$('body').append('<div id="loader"><p>loading</p></div>');  
}
//$('.letterbox').click(function() {var t = $(this).html();get_gloss_data(t,'view')});

//permanently register the DOM elements with the jQuery using .live

$('.edit').live("click",function() {var t = $(this).attr('id');get_gloss_data(t,'edit')});
$('.delete').live("click",function() {if (confirm("Do you really want to delete?")) {get_gloss_data(t,'delete')};});
$('.expand').live("click",function() {//accordion effect here
$('#glosssummary > dd').slideUp(200);
$(this).parent().next().slideToggle(200)});

//custom zoomimage because the "off the shelf" stuff won't work with ajax-generated content
$('.zoomimg').click(function(ev) {ev.preventDefault()});

$('.zoomimg').hover(function() {
h = $(this).attr('href');
t = $(this).attr('title');
$('body').append('<img class="popup" src="'+h+'"/>');
$('#loader').center().fadeIn('fast').dropShadow({right:0,bottom:0,blur:4,opacity:0.5,color:"#000000"});
$('.popup').hide().load(function() {$(this).center().css('z-index','99').fadeIn(500,function() {$(this).dropShadow({right:0,bottom:0,blur:6,opacity:0.5,color:"#000000"});$('#loader').fadeOut().removeShadow()})});
},
function() {
$('.popup').fadeOut(300,function(){$('.popup').removeShadow().remove();$('#loader').fadeOut().removeShadow()});
});


$('.lg_link').click(function() {
//use the contents to define the background colour.
var pos = $(this).parent().position();
var posleft = pos.left + 497+"px";
var postop = pos.top+"px";

var l = $(this).html();
switch (l) {
case "0":
case "1":
var bg = '#99cc00';
break;
case "2":
case "3":
var bg = '#ffaf18';
break;
case "4":
case "5a":
case "5b":
case "5c":
var bg = '#f06157';
break;
default:
var bg = '#ffffff';

}//end switch


var closeurl = "url(http://www.crow-pie.co.uk/wp-content/themes/GargoylePie/img/close.gif)";

var obj = $(this).parent().next();            

while (obj.is('dd')) {var c = obj.attr('class');if (c == 'leg_grade') {obj.css('background-image',closeurl).css('background-repeat','no-repeat').css('background-position','282px 4px').css('background-color',bg).css('position','absolute').css('top',postop).css('left',posleft).fadeIn().dropShadow({right:0,bottom:0,blur:6,opacity:0.5,color:"#000000"});}obj = obj.next();} 
});//end leg_grade live binding

$('.leg_grade').click(function() {
$(this).removeShadow().fadeOut();
});


});//end ready



