/**
 * 	Developed by Web2dev.net on October 2007
 * 
 * 	Sergey Koksharov (admin@web2dev.net)
 * 	http://web2dev.net
 */ 
var SEngine = Class.create();
SEngine.prototype = {
	options: {},
	cats: [],
	activeCat: 0,
	activeEngine: 0,
	opened: false,
	_cronid: 0,
	openTime: 200,
	firsttime: 1,
	isIndex: 1,
	
	initialize: function(categories, isIndex) {
		var options = {
			selector: 	'selector',
			searchbar: 	'searchbar',
			search:		'search',
			sbtn:		'sbtn',
			frm:		'frm',
			cats:		'categories',
			menu:		'menu',
			main:		'main',
           // sbtn:       'sbtn',
			iframe:		'iframe'
		};
		
		this.options = options;
		this.cats = categories;
		this.isIndex = isIndex;
		
		if (!this.isIndex && location.search) {
			// set search term
			var query = location.search.toQueryParams();
			if (query.s) {
				$(this.options.search).value = decodeURIComponent(query.s).replace(/\+/g, ' ');
			}
		}
		
		this.setActiveCat(getCookie('cat'));
		this.setActiveEngine(getCookie('engine'));
		this.attachEvents();
		this.onSearchbarClick();
	},
	
	openMenu: function() {
		this.opened = true;
		if (this._cronid) window.clearTimeout(this._cronid);
		this._cronid = window.setTimeout((function(){
			$(this.options.menu).style.display = 'block';
		}).bind(this), this.openTime);
	},
	
	hideMenu: function() {
		this.opened = false;
		Element.hide(this.options.menu);
		if (this._cronid) window.clearTimeout(this._cronid);
	},
	
	setActiveCat: function(id) {
		this.activeCat = id || this.activeCat;
		setCookie('cat', this.activeCat, 30, '/');
		this.fillCategories();
		this.fillEngines();
	},
	
	setActiveEngine: function(id) {
		this.activeEngine = id != undefined ? id : this.activeEngine;
		setCookie('engine', this.activeEngine, 30, '/');
		$(this.options.selector).innerHTML = this.cats[this.activeCat].engines[this.activeEngine].name;
		this.hideMenu();
		if ($(this.options.search).value) this.postQuery();
	},
	
	fillEngines: function(id) {
		var catid = id || this.activeCat;
		var engines = this.cats[catid].engines;
		var lines = [];
		for(var i=0; i<engines.length; i++) {
			lines.push('<a href="javascript:void(0)" onclick="_se.setActiveEngine(\''+ i +'\')">'+ engines[i].name +'</a>');
		}
		
		$(this.options.menu).innerHTML = lines.join('\n');
		if (!this.firsttime)
			this.setActiveEngine(0);
		this.firsttime = 0;
	},
	
	fillCategories: function() {
		var lines = [];
		for(var i=0; i<this.cats.length; i++) {
			var liClass = i == this.activeCat ? ' class="active"' : '';
			lines.push('<li'+ liClass +'><a href="javascript:void(0)" onclick="_se.setActiveCat(\''+ i +'\')">' + this.cats[i].name + '</a></li>');
		}
		
		$(this.options.cats).innerHTML = lines.join('\n');
	},
	
	postQuery: function() {
		if (this.isIndex) {
			return true;
		} else {		
			var tpl = this.cats[this.activeCat].engines[this.activeEngine].tpl;
			var query = tpl.replace('%s', encodeURIComponent($(this.options.search).value));
			
			$(this.options.iframe).src = query;
            
            
            setTimeout("updatesize()",1000);
            
			this.firsttime = 0;
			return false;
		}		
	},
	
	attachEvents: function() {
		var opt = this.options;
		Event.observe($(opt.selector), 'mouseover',	this.onSelectorHover.bindAsEventListener(this));
		Event.observe($(opt.selector), 'mouseout',	this.onSelectorOut.bindAsEventListener(this));

		Event.observe($(opt.searchbar), 'mouseover',this.onSearchbarHover.bindAsEventListener(this));
		Event.observe($(opt.searchbar), 'mouseout', this.onSearchbarOut.bindAsEventListener(this));
		Event.observe($(opt.searchbar), 'click', 	this.onSearchbarClick.bindAsEventListener(this));

		Event.observe($(opt.main), 'mouseover', 	this.onMainHover.bindAsEventListener(this));
        
    //    Event.observe($(opt.sbtn), 'mouseover',this.onSearchbarHover.bindAsEventListener(this));
      //  Event.observe($(opt.sbtn), 'mouseout', this.onSearchbarOut.bindAsEventListener(this));
	},
	
	onMainHover: function(event) {
		if (!this.opened) return;
		var element = Event.findElement(event, 'div');
		if (element.id == this.options.menu || element.id == this.options.selector) return;
		this.hideMenu();
	},
	
	onSelectorHover: function(event) {
		$(this.options.selector).className = "active";
		this.openMenu();
	},
	
	onSelectorOut: function(event) {
		$(this.options.selector).className = "";
	},
	
	onSearchbarHover: function(event) {
		$(this.options.searchbar).className = "active";
	},
	
	onSearchbarOut: function(event) {
		$(this.options.searchbar).className = "";
	},
	
	onSearchbarClick: function(event) {
		$(this.options.search).focus();
	}
};

var isIndex = 0;
function onBodyLoad() {
	window._se = new SEngine(categories, isIndex);
}

function setCookie(name, value, expires, path, domain, secure) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + expires*24*60*60*1000);		
	document.cookie= name + "=" + escape(value) +
		((expdate) ? "; expires=" + expdate.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}



Event.observe(window, 'load', onBodyLoad);


function updatesize(){
    var myWidth = 0, myHeight = 0, widt=0, hite=0;
            
            if( typeof( window.innerWidth ) == 'number' ) {
                 //Non-IE
                 myWidth = window.innerWidth;
                 myHeight = window.innerHeight;
                 widt=13;
                 hite=100;
            } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
                widt=12;
                hite=86;
            } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
                widt=12;
                hite=130;
            }
            
           // var hite=myHeight-120;
           
           
            var browser=navigator.appName
            var b_version=navigator.appVersion
            var version=parseFloat(b_version)
           
            var ie=false /*@cc_on || true @*/; 
            var ie6=(document.implementation != null) && (document.implementation.hasFeature != null);
            var ie7=(document.implementation != null && document.compatMode != null && window.XMLHttpRequest != null);
            
            if(ie&&ie6&&!ie7){
                widt=0;           
            }   
                                                                            
            document.getElementById('iframe').width=myWidth-widt;
            document.getElementById('iframe').height=myHeight-hite;
}
