TLH.pgHome = {};

Event.observe(window, 'load', pageLoad);
function pageLoad()
{
	if( ! TLH.scriptsloaded )
	{
		setTimeout('pageLoad()',60);
		return false;
	}

	TLH.pgHome.fade = new TLH.Fade(100,5);
	TLH.pgHome.fade._in('menuitems_open',0);

	tlhLogo.run();
    TLH.initNavEffects();
    TLH.pgHome.initMenu();

    Event.observe(window, 'resize', searchMenu.resizefr.bindAsEventListener(searchMenu));

	TLH.pgHome.popCL.req(1,6); //rand
	TLH.pgHome.popCL.req(2,6); //new
}

TLH.pgHome.initMenu = function()
{
	var mlinks = gE('menuitems').getElementsByTagName('a');
	for( var i=0; i < mlinks.length; i++ )
	{
		mlinks[i].onmouseover = function() { this.className = this.id+'_act'; };
		mlinks[i].onmouseout = function() { this.className = ( searchMenu.currOpen == this.id ) ? this.id+'_act menuitembig' : this.id; };
		mlinks[i].onclick = function() { searchMenu.open(this); };
	}
}

var searchMenu = {
    currOpen : 0, // id of current seach
    searchMenuId : 'targetIframeWrap',
    searchMenuFrId : 'targetIframe',
    open : function(it)
    {
        if( this.currOpen != it.id )
        {
        	// hide site synop window
            view.dispHide('sitesynop');
            // hide content list
            view.dispHide('contentlist');
            view.dispHide('newcontent');

            // reduce top margin for menu
            gE('menuitems_open').style.marginTop = '2%';

            // hide nav
            if( chkDisp('navmenu_open') )
                TLH.tog.nav();

            if( $(this.searchMenuId) && this.currOpen==0 )
                TLH.pgHome.fade._in(this.searchMenuId);

			this.chgMenuBtnFocus(it.id);
			this.resizefr();
        }
        return false;
    },
    close : function()
    {
        this.currOpen=0;
        TLH.tog.disp(this.searchMenuId);

        // reset src to blank - as specified in html
        $(this.searchMenuFrId).src=$(this.searchMenuFrId).src;

        return false;
    },
    resizefr : function()
    {
        if( this.currOpen!=0 )
        {
            var xy = TLH.getWinXY();
            var z = Position.cumulativeOffset($(this.searchMenuId))[1];

            $(this.searchMenuFrId).style.height = (xy[1] - z - 40) + 'px';
        }

        return false;
    },
    chgMenuBtnFocus : function(newBtn)
    {
    	$(this.currOpen).className = this.currOpen; // revert prev. menu focus' class to default
    	this.currOpen = newBtn;
    }
}

var tlhLogo = {
    obj : Object,
    frames : 20,
    speed : 30,
    color : 202,
    lspace : -5,
    timer : null,
    run : function(id)
    {
        if( gE('tlhLogo') )
        {
            this.obj = gE('tlhLogo');
            this.timer = setTimeout("tlhLogo.go()",this.speed);
        }
    },
    go : function()
    {
        if( this.frames )
        {
            this.frames--;
            this.lspace++;
            this.color-=2;

            this.obj.style.color = "rgb("+this.color+",199,"+this.color+")";
            this.obj.style.letterSpacing = this.lspace+"px";

            this.timer = setTimeout("tlhLogo.go()",this.speed);
        }
        else
            this.end();
    },
    end : function()
    {
        clearTimeout(this.timer);
        this.obj.className = "tlhLogo";
        this.obj.style.color = "#66CC33";
    }

    /*
        this.ds();
    },
    ds : function()
    {
    	//var fds = new Fade();
    	var ds = this.obj.cloneNode(1);
    	ds.style.color = "#000";
    	ds.style.marginBottom = "-60px";
    	//ds.style.top = (this.obj.offsetTop - 2) + "px";
    	ds.style.marginLeft = (this.obj.offsetLeft + 2) + "px";
    	this.obj.parentNode.insertBefore(ds,this.obj);
    }
    */
}


function DS_process(e) {
    // Make a duplicate of this element, with all its subelements
    var nel = e.cloneNode(1);
    // Set its class to shadowed
    nel.className = "shadowed";
    nel.className += e.className.replace('dropshadow','');
    // Set floating text colour
    textColour = e.getAttribute("textColour");
    if (textColour) nel.style.color = textColour;
    textColor = e.getAttribute("textColor");
    if (textColor) nel.style.color = textColor;
    // Add it to the document
    e.parentNode.insertBefore(nel,e);
    i++;
    nel.style.top = (e.offsetTop - 1) + "px";
    nel.style.left = (e.offsetLeft - 2) + "px";
}



TLH.pgHome.popCL = {
	url : "async.php?c=content&by=",
	req : function(by,num)
	{
		var by = ( by == 1 ) ? 'rand' : 'new';
		if( by == 'rand' )
		{
			TLH.pgHome.fade._in('contentlist',0);
			$('contentlist').update(TLH.loading);
			TLH.ajax_get(this.url+by+"&num="+num, this.fill_rnd, 1);
		}
		else if( by == 'new' )
		{
			TLH.pgHome.fade._in('newcontent',0);
			$('newcontent').update(TLH.loading);
			TLH.ajax_get(this.url+by+"&num="+num, this.fill_new, 1);
		}
	},
	fill_rnd : function(xml)
	{
		var cl = "<h3>Taste Samples:</u></h3>";
        cl += "<div class=\"lbl_hide margprevln\" style=\"width:99%;\"><span class=\"pointer more\" onclick=\"TLH.pgHome.popCL.req(1,6);\">...More!</span></div>";
		cl += "<ul class=\"infobox_inner\">";

		TLH.pgHome.popCL.end_fill('contentlist', TLH.pgHome.popCL.build(xml.documentElement.childNodes, cl, 'rnd'));
	},
	fill_new : function(xml)
	{
		var cl = "<h3>Newest Tasties:</u></h3>";
		cl += "<div class=\"lbl_hide margprevln\" style=\"width:99%;\"><span class=\"rss\"><a href=\"?rss&new\">Subscribe </a></span></div>";
		cl += "<ul class=\"infobox_inner\">";

		TLH.pgHome.popCL.end_fill('newcontent', TLH.pgHome.popCL.build(xml.documentElement.childNodes, cl, 'new'));
	},
	build : function(kids,cl,ty)
	{
		for(var i=0; i < kids.length; ++i)
		{
			var ttl = kids[i].getAttribute('title');
			var tst_id = kids[i].getAttribute('taste_id');
			var cname = kids[i].getAttribute('cname');
			var pic = ( kids[i].getAttribute('pic') != "" ) ? " <img src=\"images/pic.gif\">" : "";
			var cmts = kids[i].getAttribute('cmts');
			cmts = ( cmts != 0 ) ? ( TLH.spcr+cmts+"<img src=\"images/icons/"+TLH.plrl('cmt',cmts)+".png\" title=\""+cmts+" "+TLH.plrl('comment',cmts)+"\">" ) : "";
			var score = kids[i].getAttribute('rating');
			var votes = kids[i].getAttribute('votes');
			var time = kids[i].getAttribute('time');
			var rating = TLH.getRatingStr(score,votes);
			cl += "<li>"+( (ty=='new')?time:cname )+TLH.spcr+"<a href=\"?tasty&"+tst_id+"\" rel=\"tbox\" id=\"tst"+tst_id+"\" title=\""+ttl+"\">"+ttl.substr(0,40)+"</a>"+pic+cmts+rating+"</li>";
		}
		return cl;
	},
	end_fill : function(div,c)
	{
		$(div).update( c + "</ul>" );
		make_tbox_links(div);
	}
}
