/*\------------------------------------------------------------------------

	>> panagiotislinardakis.com
	>> 14/07/09				
	>> Kostis Sakkas				
		
------------------------------------------------------------------------\*/


window.onload	= function() {emailCloak();externalLinks();}






/*\------------------------------------------------------------------------
	
	Opens new windows via rel="external"
	url : http://www.brucelawson.co.uk/index.php/2005/opening-links-in-new-windows-in-xhtml-strict/
	
------------------------------------------------------------------------\*/

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors .length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
			anchor.title = (anchor.title != "") ? anchor.title+" (opens in new window)" : "opens in new window";
			anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
		}
	}
}


/*\------------------------------------------------------------------------
	
	Replaces email addresses with the title attr.
	credits : initial idea from http://www.badboy.ro/articles/2005-01-25/index.php
	
------------------------------------------------------------------------\*/

emailCloak = function() {
	if (document.getElementById) {
		var alltags = document.all? document.all : document.getElementsByTagName("*");
		for (i=0; i < alltags.length; i++) {
			if (alltags[i].className == "emailCloak") {
				var oldText = alltags[i].firstChild;
				var emailAddress = alltags[i].firstChild.nodeValue;
				var user = emailAddress.substring(0, emailAddress.indexOf("("));
				var website = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);
				var newText = user+"@"+website;
			  	var a = document.createElement("a");
			  	a.href = "mailto:"+newText;
				var address = document.createTextNode(newText);
				a.appendChild(address);
				alltags[i].replaceChild(a,oldText);
			}
		}
	}
}


initPL		=	function(){
		
			
			
	/*\------------------------------------------------------------------------
				
		Drop Shadow
				
	------------------------------------------------------------------------\*/
		
	$("img.dropshadow").wrap("<div class='wrap1'><div class='wrap2'>" +  "<div class='wrap3'></div></div></div>");
		
	
			
	/*\------------------------------------------------------------------------
				
		THICKBOX
				
	------------------------------------------------------------------------\*/
			
	tb_init('a.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
};






$(document).ready(function () {
	
	
	
	/*\------------------------------------------------------------------------
		
		Creates the tooltips on the brain page
		
	------------------------------------------------------------------------\*/

	
	$('#l1 a').tipsy({gravity: 's'}); 
	$('#l2 a').tipsy({gravity: 'e'}); 
	
	$('#l3 a').tipsy({gravity: 'w'}); 
	$('#l4 a').tipsy({gravity: 'e'}); 
	
	$('#l5 a').tipsy({gravity: 'w'}); 
	$('#l6 a').tipsy({gravity: 'e'}); 

	$('#l7 a').tipsy({gravity: 'w'}); 
	$('#l8 a').tipsy({gravity: 'e'}); 
	$('#l9 a').tipsy({gravity: 'w'}); 



	
	
	/*\------------------------------------------------------------------------
		
		The Gallery Ajax Call
		
	------------------------------------------------------------------------\*/

	imageGallery	=	function(what, p){
		$.post("/data/"+what+"/",
			{page:p},
			function(reply) {
				$("#main").replaceWith(reply);
			});
	};
	
	

	/*\------------------------------------------------------------------------
		
		png fix
		
	------------------------------------------------------------------------\*/

	//$('img[@src$=.png]').ifixpng(); 
	
	
	$("img.dataimage").wrap("<div class='wrap1'><div class='wrap2'>" +  "<div class='wrap3'></div></div></div>");


});

/*======================================================================*\

	hacked original json.js into a jQuery plugin.
	It adds the two functions:
	$.toJSON(value)
	$.parseJSON(json_str, [safe]).
	http://jollytoad.googlepages.com/json.js

\*======================================================================*/

(function ($) {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            'array': function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            'number': function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            'object': function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            'string': function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

	$.toJSON = function(v) {
		var f = isNaN(v) ? s[typeof v] : s['number'];
		if (f) return f(v);
	};
	
	$.parseJSON = function(v, safe) {
		if (safe === undefined) safe = $.parseJSON.safe;
		if (safe && !/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(v))
			return undefined;
		return eval('('+v+')');
	};
	
	$.parseJSON.safe = false;

})(jQuery);


