var sUserAgent = navigator.userAgent.toLowerCase();
var isOp = (sUserAgent.indexOf('opera')!=-1)?true:false;

function pop(oAnchor, sProps, sWindow) {
	var sUrl = '';
	if(oAnchor.getAttribute)
            sUrl = oAnchor.getAttribute('href');
	if(sUrl=='')
            sUrl = oAnchor.href;
	if(sUrl=='')
            return true;

	var sWindowName = sWindow?sWindow:'reusedWindow';

	if(!sProps)
            sProps = null;

	if(sUrl)
            var oPopup = window.open(sUrl,sWindowName,sProps);

	if(oPopup && !isOp)
            oPopup.focus();

	return (oPopup)?false:true;
}


// *********************************************************************************
// StyleSwitcher functions written by Paul Sowden - http://idontsmoke.co.uk/2002/ss/
// For the details, visit ALA - http://www.alistapart.com/stories/alternate/

var fontSizes = new Array('0.7em', '0.75em', '0.8em', '0.85em', '0.9em', '0.95em', '1.0em', '1.05em', '1.1em');
var currentFontSize = 0;


function applyStyle() {
    var cookie = readCookie("style");
    var style = cookie?cookie:getPreferredStyleSheet();
    setActiveStyleSheet(style);

    cookie = readCookie("font");
    style = parseInt(cookie)?parseInt(cookie):getPreferredFontSize();
    setActiveFontSize(style);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setActiveFontSize(size) {
    currentFontSize=size;
    document.body.style.fontSize=fontSizes[size];
}


function getActiveFontSize() {
    return currentFontSize;
}

function setActiveStyleSheet(title) {
    var i, a, main;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if(a.getAttribute("title") == title)
                a.disabled = false;
        }
    }
}

function getActiveStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) 
            return a.getAttribute("title");
    }
    return null;
}

function getPreferredStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1
            && a.getAttribute("rel").indexOf("alt") == -1
            && a.getAttribute("title"))
            return a.getAttribute("title");
    }
    return null;
}

function getPreferredFontSize() {
    return 0;
}

function changeFontSize(amount) {
    var newFontSize = getActiveFontSize() + amount;
    if (newFontSize<0)
        newFontSize=0;
    if (newFontSize>fontSizes.length)
        newFontSize=fontSizes.length-1;
    setActiveFontSize(newFontSize);
}

$(document).ready(function(){
    applyStyle();

    $('#tools').css('display', 'block');
    $('a#toolsLiquid').click(function(){setActiveStyleSheet('liquid');return false});
    $('a#toolsFixed').click(function(){setActiveStyleSheet('fixed');return false});
    $('a#toolsFontDown').click(function(){changeFontSize(-1);return false});
    $('a#toolsFontUp').click(function(){changeFontSize(1);return false});
    $('a#toolsPrint').click(function(){window.print();return false});
    $('a#toolsSend').toggle(
        function(){$('form#sendToFriend').show('slow');return false},
        function(){$('form#sendToFriend').hide('slow');return false}
    );
    if ($('form#sendToFriend .error').length>0) {
        $('form#sendToFriend').show();
    }

    $('form#sendToFriend').get(0).style.display='none';
    $('form#sendToFriend').addClass("JS");

    $('a.pop').click(function(){
      return pop(this, "width=760, height=600, top=50, left=50, scrollbars, resizable", "pop");
    });

//alert(fontSizes.length());

//var mycars=new Array('x', 'ccc', 'wwwww', 'sdf');
//alert(mycars.length);

});

$(window).unload(function(){
    createCookie("style", getActiveStyleSheet(), 365);
    createCookie("font", getActiveFontSize(), 365);
});