var _log0n_ = UserCookie.isLogin();
var _fontsizec_ = UserCookie.get('font',10)  * 1;

if (isNaN(_fontsizec_) || _fontsizec_ < 7 || _fontsizec_ > 15) {
    _fontsizec_ = _fontsize_; UserCookie.set('font',_fontsizec_);
}
_fontsize_ = _fontsizec_;

var motto_hd=[];
motto_hd.push([_basetheme_+'images/s_ar.gif', "Laa ilaaha illa`Allah, Muhammad rasulullah", ""]);
motto_hd.push([_basetheme_+'images/s_en1b.gif', "There is no god but Allah,", ""]);
motto_hd.push([_basetheme_+'images/s_en2b.gif', "and Muhammad is His messenger", ""]);
motto_hd.push([_basetheme_+'images/s_ar.gif', "Laa ilaaha illa`Allah, Muhammad rasulullah", ""]);
motto_hd.push([_basetheme_+'images/s_id1.gif', "Tiada tuhan selain Allah,", ""]);
motto_hd.push([_basetheme_+'images/s_id2.gif', "dan Muhammad utusan Allah", ""]);

var motto_hd_op=[];
motto_hd_op.push([_basetheme_+'images/s_op_ar.gif', "", ""]);
motto_hd_op.push([_basetheme_+'images/s_op_en1b.gif', "", ""]);
motto_hd_op.push([_basetheme_+'images/s_op_en2b.gif', "", ""]);
motto_hd_op.push([_basetheme_+'images/s_op_ar.gif', "", ""]);
motto_hd_op.push([_basetheme_+'images/s_op_id1.gif', "", ""]);
motto_hd_op.push([_basetheme_+'images/s_op_id2.gif', "", ""]);

var oTheme = function() {};
Object.extend(oTheme, {
        init: function() {
            oTheme.pageDate();
            oTheme.qSearch();
            oTheme.hMenu();
            oTheme.hts();
        },
        initCallback: function(x,y) {
            if (!x && !y) {
                oTheme.opHeader(); oTheme.motto(); 
            }
        },
        intro: function() {
            oTheme.parsetime(0);
            if (!_opacity_ ) {
                $('lop').hide();
                return;
            }
            var str = '<div class="lwrapper"><table border="0" cellspacing="0" cellpadding="0"><tr>';
                str +='<td width="100"><div id="logo_op"><img src="'+_basetheme_+'images/spacer.gif" width="50" height="50" alt=" " title=" " /></div></td>';
                str +='<td width="500" align="left" valign="bottom" class="ltpad">';
                str +='<div id="motto_op"><img id="_motto_op" name="_motto_op" src="'+_basetheme_+'images/s_op_ar.gif" width="320" height="50" alt="Syahadah" title="Syahadah" /></div>';
                str +='<div><img src="'+_basetheme_+'images/spacer.gif" width="480" height="25" alt=" " title=" " /></div>';
                str +='<div><img src="'+_basetheme_+'images/home_logotype_op.gif" width="480" height="26" alt=" " title=" " /></div>';
                str +='</td>';
                str +='<td class="lhor_op"><img src="'+_basetheme_+'images/spacer.gif" width="120" height="1" alt=" " title=" " /></td>';
                str +='</tr></table></div>';
            $('lop').update(str);
        },
        opHeader: function() {
            if (!_opacity_) {
                $('hop').hide();
                return;
            }
            var str = '<div id="hop_logo"><img src="'+_basetheme_+'images/spacer.gif" width="137" height="52" alt="LOGO" title="LOGO" /></div>';
                str +='<div class="h_r">';
                str +='<div id="hop_logotype"><img src="'+_basetheme_+'images/op_logotype_'+_lang_+'.gif" width="620" height="25" alt="" title="" /></div>';
                str +='<div id="hop_motto">';
                str +='<div><img id="ltype_op" name="ltype_op" src="'+_basetheme_+'images/s_op_ar.gif" width="320" height="50" alt="" title="" /></div>';
                str +='</div>';
                str +='</div>';
            $('hop').update(str);
        },
        pageDate: function() {
            if ($('hdate')) $('hdate').update(new DateFormat(new Hash().set('lang', _lang_)).toFormat("%d %B %Y"));
        },
        qSearch: function() {
            if ($('quickSearch')) $('quickSearch').observe('focus', function() {this.value = this.value == 'search' ? '' : this.value;});
        },
        hMenu: function() {
            if (!$('pmenu')) return;
            $('pmenu').select('td').each(function(item, i) {
                item.observe('mouseover', function() {
                        if (this.className == "hd_menu") this.className = "hd_menu_over";
                        else if (this.className == "hd_menu_on") this.className = "hd_menu_over_on";
                });
                item.observe('mouseout', function() {
                        if (this.className == "hd_menu_over") this.className = "hd_menu";
                        else if (obj.className == "hd_menu_over_on") this.className = "hd_menu_on";
                });
            });
        },
        mottoTimeout:null,
        motto: function() {
            if (!$('hl_motto_rel')) return;
            $A(motto_hd).each(function(item, i) {
                if (i > 0) {
                    $('hl_motto_rel').appendChild(new Element('div').update(new Element('img', {'src':item[0], 'alt':item[1], 'title':item[1]})).hide());
                    if (_opacity_) $('hop_motto').appendChild(new Element('div').update(new Element('img', {'src':motto_hd_op[i][0], 'alt':'', 'title':''})).setOpacity(0));
                }
            });
            if (_opacity_) ($('hop_motto').select('div')[0]).setOpacity(0.15); 
            oTheme.mottoTimeout = setTimeout(function() {return setTimeout(oTheme.mottoAnimation(0), 10000);}, 2000);
        },
        mottoAnimation: function(sF) {
            var opm = function(i, len, start, stop) {
                if (_opacity_) {new Effect.Opacity($('hop_motto').select('div')[i], {duration:len, from:start, to:stop});}
            }
            return (function() {
                    var lis = $('hl_motto_rel').select('div');
                    sA = (sF >=  lis.length - 1) ? 0 : sF + 1;
                    Effect.Fade(lis[sF], {duration:3, 
                        beforeStart: function() {opm(sF, 2, 0.15, 0);} 
                    });
                    Effect.Appear(lis[sA], {duration:2.8,
                        beforeStart: function() {opm(sA, 3, 0, 0.15);} 
                    });
                    oTheme.mottoTimeout = setTimeout(oTheme.mottoAnimation(sA), 12000);
            });
        },
        fontSizer: function(n) {
            var r = (n.name=='font-min') ? -0.5 : 0.5;
            _fontsize_ = _fontsize_ + r;
            if (_fontsize_ > 15 || _fontsize_ < 7) {
                _fontsize_ = _fontsize_ - r;
                return alert('Font size is too '+ ((r > 0)?'big':'small') +' !');
            }
            $(n).down('img').title = _fontsize_ + ' pt' + (n.name=='font-min' ? ' --' : ' ++');
            UserCookie.set('font',_fontsize_);
            $('main_content').style.fontSize = _fontsize_+'pt';
        },
        hts: function() {
            var hts_c = $('hts_c');
            if (!hts_c) return;

            Event.observe('hts_hides','mouseover', function() {oTheme.htsOptions.mouse=true;});
            Event.observe('hts_hides','mouseout', function() {oTheme.htsOptions.mouse=false;});
                
            $A($('hts_hides').childElements()).each(function(item) {item.hide();});
            $('hts_hide_bg').hide().style.top = $('hts').getHeight() + 'px';
            $('hts_hides').hide().style.top = $('hts').getHeight() + 'px';
            $A($('hts_fontsize').getElementsByTagName('a')).each(function(item) {Event.observe(item, 'click', function(evt) {oTheme.fontSizer(item); Event.stop(evt); return true;});});
            $A($('hts_c').getElementsByTagName('a')).each(function(item) {if (!($(item).readAttribute('rel'))) Event.observe(item,'click', function(evt) {oTheme.htsShow(item);Event.stop(evt); return true;});});
            
            if (document.body.parentNode && document.body.parentNode.getElementsByTagName) {
                $A($(document.body.parentNode.getElementsByTagName('head')[0]).select('link[type="text/html"]')).each(function(item) {
                        if (item.getAttribute("hreflang") && item.getAttribute("hreflang") != '') {
                            $A($('hts_language').select('a[name="'+item.getAttribute("hreflang")+'"]')).each(function(sel) {
                                    $(sel).href=item.getAttribute("href");
                                    if (item.getAttribute("rel")=='bookmark') {Event.observe(sel, 'click', function(evt){alert("You are currently on this page!");Event.stop(evt);return;});}
                            }); 
                            
                        }
                        
                });
            }
        },
        htsOptions: {timeout:false,mouse:false},
        htsShow: function(typ) {
            if (oTheme.htsOptions.timeout === null) return;
            if (oTheme.htsOptions.timeout) clearTimeout(oTheme.htsOptions.timeout);
            Effect.BlindDown('hts_hides', {duration:0.5, 
                    beforeStart:function(obj) {
                        $A($('hts_hides').childElements()).each(function(item){if ((item.id).indexOf(typ.name) != -1) item.show(); else item.hide();});
                    },
                    beforeFinish:function(obj) {
                        $A($('hts_hides').childElements()).each(function(item){if ((item.id).indexOf(typ.name) != -1) item.show(); else item.hide();});
                    }
            });
            Effect.BlindDown('hts_hide_bg', {duration:0.4});
            oTheme.htsCall();
        },
        htsCall: function() {
            oTheme.htsOptions.timeout =  setTimeout(function() {oTheme.htsFade();}, 3000);
        },
        htsFade: function() {
            if (oTheme.htsOptions.timeout === null) return;
            if (oTheme.htsOptions.timeout) clearTimeout(oTheme.htsOptions.timeout);
            if (!oTheme.htsOptions.mouse) {
                Effect.BlindUp('hts_hides', {duration:0.4, beforeStart:function(){oTheme.htsOptions.timeout=null;}, afterFinish:function(){oTheme.htsOptions.timeout=false;}});
                Effect.BlindUp('hts_hide_bg', {duration:0.5, beforeStart:function(){oTheme.htsOptions.timeout=null;}, afterFinish:function(){oTheme.htsOptions.timeout=false;}});
            } else oTheme.htsCall();
        },
        boxLoad: function() {
            oTheme.parsetime(1);
            oTheme.box_watchlist();
            oTheme.box_tagcloud();
        },
        parsetime: function(ac) {
            var url = _baseuri_ + 'xml/box_account.xml';
            var debug = $('parsetime');

            var param = new Hash();
            param.set('fID', _fID);
            param.set('pID', _pID);
            param.set('account', ac);
            
            new Ajax.Request(url, {
                    method: 'get',
                    parameters: param,
                    asynchronous: false, 
                    onSuccess: function(transport) {
                        //alert(transport.responseText);
                        var docRoot = null;
                        try {docRoot = transport.responseXML.documentElement;} catch(e){debug.innerHTML = e; return;};
                        if (!docRoot) {debug.innerHTML = 'xml error'; return;};
                        
                        $('parsetime').update('Parsetime: '+getNodeValue(docRoot.getElementsByTagName('parsetime')[0])+' Seconds');
                        try{
                            $('hts_c_account').href = getNodeValue(docRoot.getElementsByTagName('logoutlink')[0]);
                            $('hts_c_account').update('logout').observe('click', function(event) {
                                    if (!(confirm('Are you sure want to logout?'))) {
                                        Event.stop(event);
                                        return false;
                                    };
                                    return true;
                            });
                        } catch(e) {}
                        if (ac==1 && $('box_account')) {
                            ac = docRoot.getElementsByTagName('account');
                            if (ac && ac[0]) {
                                $('box_account').update('<h6>My Account</h6><div class="box_body"><div id="_myaccount_"><ul class="list_none" id="_menuaccount_"></ul></div></div>');
                                try {$A(ac[0].getElementsByTagName('li')).each(function(item, i){$('_menuaccount_').appendChild(new Element('li').update(getNodeValue(item)));});}catch(e){$('box_account').hide()}
                            } else {
                                $('box_account').hide();
                            }
                        }
                    }
            });
        },
        box_tagcloud: function() {
            var url = _baseuri_ + 'xml/box_tagcloud.xml';
            var box = $('box_tagcloud');
            if (!box) return;
            var box_body = box.down('div');

            var param = new Hash(); param.set('fID', _fID); param.set('pID', _pID);
            
            new Ajax.Request(url, {
                    method: 'get',
                    parameters: param,
                    asynchronous: false, 
                    onSuccess: function(transport) {
                        var docRoot = null;
                        try {docRoot = transport.responseXML.documentElement;} catch(e){alert(e); return;};
                        if (!docRoot) {box.hide(); return;};
                        if (!docRoot.getElementsByTagName('link')) {box.hide();return;}
                        var link = getNodeValue(docRoot.getElementsByTagName('link')[0]);
                        var tag  = getNodeValue(docRoot.getElementsByTagName('tag')[0]);
                        var list = docRoot.getElementsByTagName('list')[0].getElementsByTagName('li');
                        var content = '';
                        for (var i=0; i<list.length; i++) {
                            content += '<span class="cloud2"><a href="' + link + '">' + getNodeValue(list[i]) + '</a></span> ';
						}
						box_body.update(content);
                    }
            });
        },
        box_watchlist: function() {
            var url = _baseuri_ + 'xml/box_watchlist.xml';
            var box = $('box_watchlist');
            var box_add = $('add_watchlist');
            if (box && box_add && _fID && _pID) {
                Event.observe(box_add, 'click', function(event){oTheme.add_watchlist(); Event.stop(event); return false;});
            } else if (box_add) {
                box_add.addClassName('disabled');
                Event.observe(box_add, 'click', function(event){alert('Watchlist disabled!'); Event.stop(event); return false;});
            }
            if (!box) return;
            var lul = box.down('ul');

            var param = new Hash(); param.set('fID', _fID); param.set('pID', _pID);
            
            new Ajax.Request(url, {
                    method: 'get',
                    parameters: param,
                    asynchronous: false, 
                    onSuccess: function(transport) {
                        //alert(transport.responseText);
                        var docRoot = null;
                        try {docRoot = transport.responseXML.documentElement;} catch(e){alert(e); return;};
                        if (!docRoot) {box.hide(); return;};
                        oTheme.list_cleanup(lul);
                        oTheme.list_add(lul, docRoot.getElementsByTagName('li'), {name:'-watchlist', del:1});
                        try {
                            if (Utility.getInt(lul.getElementsByTagName('li')[0].id) == _pID) {
                                box_add.addClassName('disabled');
                            } 
                        }catch(e){};
                    }
            });
        },
        add_watchlist: function() {
            var url = _baseuri_ + 'xml/box_watchlist_add.xml';
            var param = new Hash(); param.set('fID', _fID); param.set('pID', _pID);
            
            if ($('add_watchlist').hasClassName('disabled'))
                return;
            $('add_watchlist').addClassName('imgloader');
            new Ajax.Request(url, {
                    method: 'get',
                    parameters: param,
                    onSuccess: function(transport) {
                        $('add_watchlist').removeClassName('imgloader');
                        $('add_watchlist').addClassName('disabled');
                        var docRoot = null;
                        try {docRoot = transport.responseXML.documentElement;} catch(e){debug.innerHTML = e; return;};
                        if (!docRoot) {return;};
                        var box = $('box_watchlist');
                        if (box) {
                            var lul = box.down('ul');
                            oTheme.list_add(lul, docRoot.getElementsByTagName('li'), {name:'-watchlist', del:1});
							try {var ai = lul.getElementsByTagName('li')[0].getElementsByTagName('a')[1]; ai.focus(); ai.blur();}catch(e){};
                        }
                    },
                    onFailure: function() {
                        $('add_watchlist').removeClassName('imgloader');
                        $('add_watchlist').addClassName('disabled');
                    }
            });
        },
        del_watchlist: function(obj) {
            var url = _baseuri_ + 'xml/box_watchlist_del.xml';
            var lil = $(obj).up('li');
            var param = new Hash(); param.set('fID', _fID); param.set('pID', _pID); param.set('delID', lil.id);
            var lul = $(obj).up('ul');
            $(obj).addClassName('imgloader');
            new Ajax.Request(url, {
                    method: 'get',
                    parameters: param,
                    onSuccess: function(transport) {
                        var docRoot = null;
                        try {docRoot = transport.responseXML.documentElement;} catch(e){debug.innerHTML = e; return;};
                        if (!docRoot) {return;};
						//alert(Utility.getInt(lil.id) +'=='+ _pID);
						if (Utility.getInt(lil.id) == _pID) {
							$('add_watchlist').removeClassName('disabled');
                        }
                        lil.remove();
                        oTheme.list_add(lul, docRoot.getElementsByTagName('li'), {name:'-watchlist',del:1});
                            try {
                                if (Utility.getInt(lul.getElementsByTagName('li')[0].id) == _pID) {
                                    $('add_watchlist').addClassName('disabled');
                                } 
                            }catch(e){};
                    }
            });            
        },
        list_cleanup: function(oUL) {
            if (oUL) $(oUL).update('');
        },
        list_add: function(oUL, list, params) {
            oUL = $(oUL);
			var named = params.name ? params.name : null;
			var del = params.del ? true : false;
            var lil = null, nid = 0;
			var this_id = '';
			var this_class = params.clas ? ' class="'+params.clas+'"' : '';
            if (list && list.length > 0) {
                if ($('0' + named)) {$('0' + named).remove()} 
                for (var i=0; i<list.length; i++) {
					this_id = '';
                    nid = list[i].getAttribute('id');
                    try {$(nid+named).remove()} catch(e){};
					if (nid && named) {
						this_id = ' id="'+nid+named+'"';
					}
                    if (nid != 0 && nid != '0' && del) {
                        oUL.insert(new Template('<li#{listid}#{clas}><div class="list_del"><a href="#del" onclick="oTheme.del_watchlist(this); return false;"><img alt="del" src="'+_basetheme_+'images/list-del.gif'+'" /></a></div>#{content}</li>').evaluate({listid:this_id,clas:this_class,content:getNodeValue(list[i])}));
                    } else {
                        oUL.insert(new Template('<li#{listid}#{clas}>#{content}</li>').evaluate({listid:this_id,clas:this_class,content:getNodeValue(list[i])}));
                    }
                }
            }
        },
        saveOpacity: function() {}
});

function getNodeValue(oNode) {
    if (oNode.nodeValue)
        return oNode.nodeValue;
    else if (oNode.firstChild)
        return getNodeValue(oNode.firstChild);
    else
        return '';
}

function parseChildNodes(node,parentTag,childTag){
    var results=[];
    try{
        var children=$A(node.getElementsByTagName(parentTag)[0].getElementsByTagName(childTag));
        results=children.collect(function(value,index){return value.firstChild.data;});
    } catch(e) {};
    return results;
}

function _XMLSerialize(oNode){
	var sXml="";
	if (!oNode.nodeType) {
		throw new Error("Not valid DocumentElement or Node element");
		return false;
	}
	switch(oNode.nodeType){
		case 1:
			sXml="<"+oNode.tagName;
			for(var i=0;i<oNode.attributes.length;i++){
				sXml+=" "+oNode.attributes[i].name+"=\""+oNode.attributes[i].value+"\"";
			}
			sXml+=">";
			for(var i=0;i<oNode.childNodes.length;i++){
				sXml+=_XMLSerialize(oNode.childNodes[i]);
			}
			sXml+="</"+oNode.tagName+">";
			break;
		case 3:
			sXml=oNode.nodeValue;
			break;
		case 4:
			sXml="<"+"![CDATA["+oNode.nodeValue+"]]"+">";
			break;
		case 7:
			sXml="<?"+oNode.nodevalue+"?>";
			break;
		case 8:
			sXml="<"+"!--"+oNode.nodevalue+"--"+">";
			break;
		case 9:
			for(var i=0;i<oNode.childNodes.length;i++){
				sXml+=_XMLSerialize(oNode.childNodes[i]);
			}
			break;
	}
	return sXml;
}

var CSSAddon = new Array();
var insertCSSIE = function(style, key, val) {
    style.addRule(key, val);
}
var insertCSSIGK = function(style, key, val) {
    var str = key + ' {' + val + '}';
    style.sheet.insertRule(str, style.sheet.cssRules.length);
}

