/* Global Javascript for F5 */
var loadIcon = '<img alt="loading" src="/css/images/loading.gif" id="load_icon" />',
	favIcon = '/css/images/icon_document.gif',
	toolBarMode = 0, 
	toolBar,
	toggleToolBar,
	siteFrame;

window.addEvent('domready',function(){
	toolBar = $('toolbar');
	
	var	toolBarFx = new Fx.Morph(toolBar,{link:'chain'}),
		toolBarWidth = toolBar.getCoordinates().width,
		toolBarMarginLeft = toolBar.getStyle('margin-left'),
		myModal,myRequest,
		searchKey = $$('input.search_key'),
		favIcons = $$('img.favicon'),
		bodyBlock = $('body_block'),
		container = $('container'),
		systemNotice = $('system_notice');
//		screenshots = $$('img.screenshot');

	if(systemNotice)
		systemNotice.set('morph',{duration:10000}).morph({'opacity':0});
	container.setStyle('min-height',(window.getCoordinates().height-1) + 'px');
	
/*	toggleToolBar = function() {
		if(toolBarMode) {
			Cookie.dispose('toolbar',{path:'/'});
			if($('item_url')) {
				location.href = $('item_url').get('value');
				Cookie.write('redirect',1);
			} else {
				$$('html').setStyle('overflow','auto');
				toolBar.setStyle('position','relative');
				toolBarFx.start({
					'left':'0px',
					'top':'0px',
					'width':toolBarWidth
				});
				bodyBlock.setStyle('padding-top',0);
				toolBarMode = 0;
				//linkEvents.remove();
			}
		} else {
			$$('html').setStyle('overflow-x','hidden');
			toolBar.setStyles({
				'position':'fixed'
			});
			bodyBlock.setStyle('padding-top','50px');
			toolBarFx.start({
				'left':'-25px',
				'top':'-13px',
				'width':window.getCoordinates().width+80+'px'
			});
			toolBarMode = 1;
			Cookie.write('toolbar','attached',{duration:365,path:'/' });
			//linkEvents.add();
		}
	}  */
	
	linkEvents.add();
	
/*	if(Cookie.read('toolbar') || location.href.indexOf('/url?') != -1) {
		toolBar.setStyles({
			'left':'-25px',
			'top':'-13px',
			'width':window.getCoordinates().width+80+'px'
		});
		toolBar.setStyle('position','fixed');
		bodyBlock.setStyle('padding-top','50px');
		toolBarMode = 1;
		linkEvents.add();
	} */
	
	//toolBar.addEvent('dblclick',toggleToolBar);	
	if(searchKey) {
		searchKey.addEvent('keyup',function(e){
			if(e.key == 'enter' && this.get('value').trim() != '') {
				window.location = '/search/'+urlencode(this.get('value').trim());
			}
		});
		$('searchBtn').addEvent('click',function(){
			window.location = '/search/'+urlencode(searchKey[0].get('value').trim());			
		});
	}
});

window.addEvent('resize',function(){
	if(toolBarMode == 1) {
		if(siteFrame)
			siteFrame.setStyle('height',(window.getCoordinates().height-31)+'px');		
		toolBar.setStyles({
			'left':'-25px',
			'top':'-13px',
			'width':window.getCoordinates().width+80+'px'
		});
	}
});

function urlencode( str ) {
    var histogram = {}, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

var linkEvents = {
	add : function() {
		var itemLinks = $$('a.item_link');
		itemLinks.removeEvents().addEvent('click',function(e){
			//if(Cookie.read('toolbar')) {
				e.stop();
				var info = this.get('class').split(' ')[1].split('-'),
					url = '/url?trk='+info[0]+'&u='+info[2]+'&l='+urlencode(this.get('href'));
				if(info[3] == 'blank' && !Cookie.read('toolbar'))
					window.open(url);
				else
					location.href = url;
			//}
		}); 
	},
	remove: function() {
		var itemLinks = $$('a.item_link');
		itemLinks.removeEvents();	
		$$('html').setStyle('overflow','auto');
		$('toolbar_search').setStyle('display','none');
	}
};	

var loadJS = function(file) { 
	var script = new Element('script',{
		'src':file,
		'type':'text/javascript'
	}).inject($$('head')[0]); 
};