// RR.com common JS routines  (jQuery is very cool, but I really don't need it in a site like this.)

var init = {}, util = {};

// Global init
init.global = function() {
	util.myTargetBlank();
	util.IE6backgroundFix();
};


// Specific inits
init.index = function() {
	util.showHideNews('show-index-oldnews', 'index-oldnews', 'ajax/index-oldnews.php', init.index_oldnews );
	util.myAntiPopup();
};

init.discogsbar = function() {
	util.installExtension('installDiscogsbar', 'Discogs Toolbar 0.3','/img/extDSB.png');
};

init.orkutbar = function() {
	util.showHideNews('show-otb-oldnews', 'otb-oldnews','ajax/orkutbar-oldnews.php');
	
	util.installExtension('installOrkutbar', 'Orkut Toolbar 1.5.2','/img/extOtb.png');
	util.installExtension('installPinkSkin', 'Orkut Toolbar Pink Skin (Default)','/img/extOtb.png');
	util.installExtension('installBlueSkin', 'Orkut Toolbar Blue Skin','/img/extOtb.png');
	util.installExtension('installGraySkin', 'Orkut Toolbar Gray Skin','/img/extOtb.png');
	util.installExtension('installBlackSkin', 'Orkut Toolbar Black Skin','/img/extOtb.png');
	
	util.myAntiPopup();
};

init.contact = function() {
	rr.addEvent('mail_subject', 'change', function() {
		var elem = rr.getElement('other');
		var value = rr.getElement('mail_subject').value;
		elem.style.display = value == '0' ? "block" : "none";
		if (value == '0') { rr.getElement('mail_other').focus(); }
	});
	
	// anti spambots
	rr.addEvent('vaiPlaneta', 'click', function() {
		var frm = rr.getElement('contact_form');
		frm.action = rr.getElement('meLevaQueEuVou').value;
		frm.submit();
	});
};

init.crypter = function() {
	rr.addEvent('encrypt', 'click', function(){
		rr.getElement('action').value='encrypt';
		rr.getElement('form_crypter').submit();
	});
	
	rr.addEvent('decrypt', 'click', function(){
		rr.getElement('action').value='decrypt';
		rr.getElement('form_crypter').submit();
	});
	
	var textBox = rr.getElement('text');
	if(textBox.value.length) {
		textBox.selectionStart = 0;
	}
};

init.index_oldnews = function() {
	util.showHideNews('show-index-whatsnew', 'index-whatsnew', 'ajax/index-whatsnew.php' );
};

// Helper functions

// Open a[@rel='external'] in new windows (or tabs)
util.myTargetBlank = function(){
	var links = rr.getElementsByAttribute('rel', 'external', { tagName:'a' } );
	
	for (var i = 0, len = links.length; i < len; i++) {
		links[i].onclick = function(e){
			e = e || event;
			if(e.ctrlKey || e.shiftKey || e.altKey || e.metaKey ) {
				return true;
			} else {
				window.allowPopup = true;
				window.open(this.href);
				return false;
			}
		};
	}
};

// Blocks unwanted popups from the counter
util.myAntiPopup = function(){
	window.nativeOpen = window.open;
	
	window.open = function(url, name, specs, replace) {
		if(window.allowPopup) {
			url = url || 'about:blank';
			name = name || '_blank';
			specs = specs || 'channelmode=no,directories=yes,fullscreen=no,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes';
			replace = replace || false;
			window.nativeOpen(url, name, specs, replace);
			window.allowPopup = false;
			return true;
		}
		return true;
	};
};

// Fix IE6 blinking background images bug
util.IE6backgroundFix = function() {
	if (window.ActiveXObject && typeof document.body.style.maxHeight == "undefined") {
		try { document.execCommand('BackgroundImageCache',false,true); } catch(e){}
	}
};

// Show/hide oldnews
util.showHideNews = function(elem, container, url, callback) {
	rr.addEvent(elem, 'click', function(event){
		if(event.preventDefault) { event.preventDefault(); }
		rr.showHideElement(container);
		if (rr.getStyle(container, 'display') != 'none') { rr.ajax.replaceContent(container, url, { callback: callback } ); }
		return false;
	});
};

// Install extensions in mozilla-like browsers
util.installExtension = function(elem, title, image) {
	rr.addEvent(elem, 'click', function(event) {
		if(event.preventDefault) { event.preventDefault(); }
		rr.extras.installExtension(event, title, image);
	});
};

// Call the auto init automaticaly on every page
(init.autoInit = function() {
	if(typeof init.global === 'function') { init.global(); }
	
	var page = document.body.id.match(/(.+)-body$/);
	if(page && page[1] && typeof init[page[1]] === 'function') {
		init[page[1]]();
	}
}());