//
// Tech Haven Network - Items DB - THN API
// (c) 2008-2010 - Tech Haven Network - http://www.techhaven.org
// Written by Pete 'Brammers' Hatton 
// 
// This file is part of THNapi.
// 
// THNapi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// THNapi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with THNapi.  If not, see <http://www.gnu.org/licenses/>.
//

// Open link in popup window
function pu(url)
{
	window.open(url,"","scrollbars=yes,width=500,height=500");
	return false;
}

// Open link in parent window or a new window if the user has closed the window
function ol(url)
{
	try
	{ 
	    window.opener.document.title = window.opener.document.title; 
		window.opener.location.href=url;
	}
	catch(e)
	{
		// Todo: Add code to detect window with URL open
		window.open(url,"","");
	}
	return false;
}

// Jump menu
function jumpMenu(element)
{
	var newIndex = element.selectedIndex;
	if (newIndex!=0)
	{
		url = element.options[newIndex].value; 
		window.location.assign(url);
	}
}

// JQuery code to setup the Item DB pages

$kjq(document).ready(function($)
{
	$kjq.tablesorter.addParser({ 
        // set a unique id 
        id: 'armour', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s){
         
            //console.log(s);
            if (s=='-&nbsp;&nbsp;')
            {
            	return $kjq.tablesorter.formatFloat('999999999');
            }
            else
            {
            	return $kjq.tablesorter.formatFloat(s);;
            }
        }, 
        // set type, either numeric or text 
        type: "numeric" 
    });
 
 	$kjq.tablesorter.addParser({ 
        // set a unique id 
        id: 'reqs', 
        is: function(s)
        { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s)
        { 
           	if (s.length==0)
           	{
           		return '';
           	}
           	
           	s=s.substr(0,s.indexOf('&nbsp;'));

           	req=s.substr(0,s.indexOf(':'));
           	value=s.substr(s.indexOf(':')+1);
   			if (value.length==1)
   			{
   				value='00'+value;
   			}
   			if (value.length==2)
   			{
   				value='0'+value;
   			}
   			//console.log(req+value);
			return req+value;
        }, 
        // set type, either numeric or text 
        type: "text" 
    });
    
  	$kjq.tablesorter.addParser({ 
        // set a unique id 
        id: 'mods', 
        is: function(s)
        { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s)
        { 
           	if (s.length==0)
           	{
           		return '';
           	}
           	req=s.substr(0,s.indexOf(':'));
           	value=s.substr(s.indexOf(':')+1);

           	pos=value.substr(0,1)

           	value=value.substr(1);
           	//console.log(req);
   			if (value.length==6)
   			{
   				value='0'+value;
   			}
     		if (value.length==5)
   			{
   				value='00'+value;
   			}
   			if (value.length==4)
   			{
   				value='000'+value;
   			}
   			//console.log(req+pos+value);
			return req+pos+value;
        }, 
        // set type, either numeric or text 
        type: "text" 
    });
    
    
    // Apply table sorter
	$kjq("table.items").tablesorter();
	

	
	$kjq("tr.items_row").bind("mouseover", function(event)
	{
		$kjq(this).addClass('items_row_hi');

	});

	$kjq("tr.items_row").bind("mouseout", function(event)
	{
		$kjq(this).removeClass('items_row_hi');
	});	

	$kjq("tr.items_row").bind("click", function(event)
	{
		url=$kjq(this).find("a.item_db_show").attr( 'href' );
		url=url.replace('/item/','/showitem/')
		window.open(url,"","scrollbars=yes,width=500,height=500");
		return false;
	});
	
	// Click popup links
	$kjq("a.item_db_show_click").bind("click", function(event)
	{
		url=$kjq(this).attr( 'href' );
		url=url.replace('/item/','/showitem/')
		window.open(url,"","scrollbars=yes,width=500,height=500");
		return false;
	});
	
	// Stop pop windows opening opening when user clicks on an article link
	$kjq("a.article_info").bind("click", function(event)
	{
		event.stopPropagation();
	});
	
	// Cluetips for the items, boxed items
	$kjq("td.item_db_show,span.item_db_show").cluetip(
	{
		sticky: true,
		width:490,
		closePosition: 'bottom',
		showTitle:false,
		arrows: true
	});
	
	// Apply cluetips to icons
	$kjq("span.item_db_icon").cluetip(
		{splitTitle: '|'
  			//hoverIntent:
  			//{
		    //	sensitivity:  3,
		    //	interval:     50,
		    //	timeout:      500    
  			//},
	});
	

});




