// ==UserScript==
// @name          Wretch Ajax Album
// @version       1.1
// @description	  Ajaxize the Wretch Albums
// @namespace     http://webdev.yuan.cc/
// @include       http://www.wretch.cc/album/show.php*

// Whats New
// =========
// v1.0	3/5/07 First release
// v1.1	10/5/07 Can see original size photos
//
// Description
// ===========
// Ajaxize the Wretch albums
//
// Credit
// =========
// Codes of unblocking hotkeys are taken from Goston's script
// See http://www.goston.net/2007/04/27/826/ for more info.
//
// Contact
// =======
// CK ( http://webdev.yuan.cc/ )

// ==/UserScript==

(function() {

function _gt(e) { return document.getElementsByTagName(e); }
function _gi(e) { return document.getElementById(e); }
function _ce(e) { return document.createElement(e); }
function _ct(e) { return document.createTextNode(e); }

if(unsafeWindow) w = unsafeWindow;
else w = window;

w.show_hotkey_parameters['first'] = true;
w.show_hotkey_parameters['prev'] = true;
w.show_hotkey_parameters['next'] = true;
w.show_hotkey_parameters['last'] = true;
w.show_hotkey_parameters['origin'] = true;
w.show_hotkey_parameters['slide'] = true;
w.show_hotkey_parameters['exif'] =  true;
w.show_hotkey_parameters['admin'] = false;
  
var a,lastid, url_prefix, first_id, last_id, next_id, prev_id, img, src, _tmp;
var Wretch, PIC, URL;
var _can_see_orig = false;

URL = /(http:\/\/.*.wretch.cc\/album\/show.php\?i=.+&b=\d+&f=)(\d+).*$/;
a = URL.exec(document.location);
url_prefix = a[1];
a = URL.exec(_gi('first').href);
first_id = 1*a[2];
a = URL.exec(_gi('last').href);
last_id = 1*a[2];

Wretch = new Object();
img = _gi('DisplayImage');
src = img.src;
PIC = /(http:\/\/.*.pic.wretch.cc\/photos\/.*\/)(\d+)\.jpg/;
if( PIC.test(src) ) {
    a = PIC.exec(src); 
	prefix = a[1];
    id = 1*a[2];
}

if( URL.test(_gi('next').href) ) {
	a = URL.exec(_gi('next').href);
	next_id = 1*a[2];
} else {
//	if(GM_getValue('lastpage','') == '') GM_setValue('fromlast',1);
//	document.location = _gi('prev').href;
}
if( URL.test(_gi('prev').href) ) {
	a = URL.exec(_gi('prev').href);
	prev_id = 1*a[2];
} else {
//	if(GM_getValue('lastpage','') == '') GM_setValue('fromfirst',1);
//	document.location = _gi('next').href;
}
// GM_log('first_id='+first_id +',prev_id='+ prev_id +',next_id='+ next_id +',last_id='+ last_id);

img.addEventListener('error', function() {
	var str = url_prefix + lastid;
	GM_setValue('lastpage',str);
//	GM_log('Jump to ' + str);
	if(_show_orig) {
		img.src = _tmp;
		return;
	}
	document.location = str;
//	img.src = prefix + id + '.jpg';
} , true);

var showOrig = function() {
	_show_orig = true;
	_tmp = img.src;
	img.src = prefix + 'o' + id + '.jpg';
}
	
if( _gi('orig') ) {
	_can_see_orig = true;
	_gi('orig').href = 'javascript:';
	_gi('orig').addEventListener('click', showOrig , true);
	next_id = 1*a[2];
} else {
	_can_see_orig = false;
}

if(GM_getValue('lastpage','') != '') {
	GM_setValue('lastpage','');
	_show_orig = false;
	if( GM_getValue('dir') == 1 ) {
//		document.location = _gi('next').href;
//		GM_log('Forward');
		img.src = prefix + next_id + '.jpg';
		img.parentNode.href = url_prefix + next_id;
		id = next_id;
//		GM_log('Goto ' + img.src);
	} else {
//		document.location = _gi('prev').href;
//		GM_log('Backward');
		img.src = prefix + prev_id + '.jpg';
		img.parentNode.href = url_prefix + prev_id;
		id = prev_id;
//		GM_log('Goto ' + img.src);
	}
}

var wretchHotKey = new Object();
wretchHotKey.doHotKey = function() {

    var bodyElm = document.getElementsByTagName('body')[0];
    bodyElm.setAttribute('OnKeyUp','');

    var re = /http:\/\/(www\.)?wretch\.cc\/album\/show\.php/;
    if( re.test(document.location) )
    {
        bodyElm.setAttribute('OnKeyUp','show_hotkey(event, show_hotkey_parameters);');
    }
    else
    {
        bodyElm.setAttribute('OnKeyUp','link_hotkey(event, link_hotkey_parameters);');
    }
    bodyElm.setAttribute('onDragStart','');
    bodyElm.setAttribute('oncontextmenu','');
    bodyElm.setAttribute('onSelectStart','');
}
wretchHotKey.doHotKey();


w.show_hotkey = function (event, parameters)
{
  var obj;
  if(!parameters)
    return;

  var srcElement = event.srcElement ? event.srcElement : event.target;
  
  if(!event.shiftKey && !event.altKey && !event.ctrlKey) 
  {
    obj = document.getElementsByTagName("INPUT");
    for (i = 0; i < obj.length; i++)
    {
        if ( obj[i].name == srcElement.name &&
             (obj[i].type == "text" ||
             obj[i].type == "password")
           )
        {
            return true;
        }
    }

    obj = document.getElementsByTagName("TEXTAREA");
    for (i = 0; i < obj.length; i++)
    {
        if (obj[i].name == srcElement.name)
        {
            return true;
        }
    }

    if (event.keyCode == 90)
    {
      if(parameters['first'])
//        document.location = document.getElementById("first").href;
		lastid = id;
		id = first_id;
		_show_orig = false;
		img.src = prefix + id + '.jpg';
    }
    else if (event.keyCode == 88)
    {
      if(parameters['prev']) {
		GM_setValue('dir', -1);
		lastid = id;
		id--;
		if( id >= first_id ) {
			_show_orig = false;
			img.src = prefix + id + '.jpg';
			img.parentNode.href = url_prefix + id;
		}
		else id++;
//		GM_log('first_id=' + first_id + ',lastid=' + lastid +',id='+ id);
	  }
    }
    else if (event.keyCode == 67)
    {
      if(parameters['next']) {
		GM_setValue('dir', 1);
		lastid = id;
		id++;
		if( id <= last_id ) {
			_show_orig = false;
			img.src = prefix + id + '.jpg';
			img.parentNode.href = url_prefix + id;
		} else id--;
//		GM_log('first_id=' + first_id + ',lastid=' + lastid +',id='+ id);
	  }
    }
    else if (event.keyCode == 86)
    {
      if(parameters['last']) {
//        document.location = document.getElementById("last").href;
		lastid = id;
		id = last_id;
		_show_orig = false;
		img.src = prefix + id + '.jpg';
	  }
    }
    else if (event.keyCode == 66)
    {
      document.location = document.getElementById("updir").href;
    }
    else if (event.keyCode == 78)
    {
      if(parameters['origin']) {
//        document.location = document.getElementById("orig").href;
//		if(_can_see_orig) 
		showOrig();
	  }
		
    }
    else if (event.keyCode == 83)
    {
      if(parameters['slide'])
        document.location =  document.getElementById("slide").href 
    }
    else if (event.keyCode == 73)
    {
      if(parameters['exif'])
      {
        var exif_obj = document.getElementById("exif");
        if(exif_obj)
        {
          document.location = exif_obj.href 
        }
      }
    }
    else if (event.keyCode == 77)
    {
      if(parameters['admin'])
      {
        document.cookie = 'sm='+parameters['admin_cookie'];
        document.location.reload();
      }
    }
  }
}

})();


