/*
 * @(#) rkk.js	(c)2007 Ali - N
 *
 * http://www.Ali-N.com/
 * 
 */


var section;
var pageWidth;
var pageHeight;
var pageTop;
var contentHeight;

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'scroll', set_dimensions, false);
Event.observe(window, 'resize', set_dimensions, false);
Event.observe(window, 'unload', Event.unloadCache, false);


function initialize() {
	section = "home"; //home  = for flash intro
	getBrowserInfo();
	set_dimensions();
	apply_nav_links();
	load_section(section);
//	new mailForm('mailForm');
}


// PAGE AND ELEMENT RESIZING
function set_dimensions() {
	var frameWidth;
	var frameHeight;
	var frameTop;
	var el;

	if (self.innerWidth) {
		frameWidth  = self.innerWidth;
		frameHeight = self.innerHeight;
		frameTop    = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		frameWidth  = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
		frameTop    = document.documentElement.scrollTop;
	}
	else if (document.body) {
		frameWidth  = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
		frameTop    = document.body.scrollTop;
	}
	else {
		return;
	}
	
	// update the globals
	pageWidth  = frameWidth;
	pageHeight = frameHeight - 100;
	pageMidHeight = frameHeight - 200;
	pageTop    = frameTop;
	
 
	//set the ctn_bottom div
	if ( el = $('ctn_bottom') ) {
		if (browser == "Internet Explorer" || browser == "Opera") {
			el.style.position = "absolute";
			el.style.top = frameTop + pageHeight;
		} else {
			el.style.position = "fixed";
			el.style.bottom = 0;
		}
	}
	
	 
	//set the ctn_toplogo div
	if ( el = $('ctn_toplogo') ) {
		if (browser == "Internet Explorer" || browser == "Opera") {
			el.style.position = "absolute";
			el.style.top = 0;
		} else {
			el.style.position = "fixed";
			el.style.top = 0;
		}
	}
	
	
	//set the ctn_top div
	if ( el = $('ctn_top') ) el.style.height = pageHeight; //ali = pageHeight
	if ( el = $('content') ) {
			el.style.height = pageMidHeight;
			el.style.top = 100;
			
	}
	
}


// CONTENT LOADING AND DISPLAY ANIMATIONS

var ctnAnim;
var isOpen = false;

function load_section(s) {
	if (s == null) return;
	
	if ( !isOpen ) { section = s;
		
		if (section == "home") {
			if ( el = $('content') ) el.style.display = "none";
			if ( el = $('bkg') )     el.style.display = "none";
			if ( el = $('ctn_top') ) el.style.display = "block";
			
			//render the flash file
			set_dimensions();
			var fo = new FlashObject("base.swf", "base", "100%", "100%", "6");
			fo.addParam("quality", "best");
			fo.addParam("wmode", "transparent");
			fo.write("ctn_top");
		} else {
			if ( el = $('content') ) el.style.display = "block";
			if ( el = $('ctn_top') ) el.style.display = "none";

			new Ajax.Request( section + '/', { method:'get', parameters:'', onComplete:render_data });
			var track=s_gi('wmgwbrrockkillskidcom');
			track.pageName=section;
			void(track.t());
			load_bkg();
		}
	} else {
		ctnAnim.close();
		section = s;
	}
	
	//set the selected button
	set_nav_btns();
}

function check_reload() {
	if ( !isOpen ) {
		load_section(section);
	} else {
		if (section == "write") new writeForm('writeForm');
		if (section == "tombofdoom") setTimeout(lb_initialize.bindAsEventListener(this), 250);
		if (section == "video") initVideoButtons();
		contentHeight = $(section).scrollHeight;
		set_dimensions();
	}
}


function render_data(request) {	
	var response = request.responseText;
	
	if ( el = $('content') ) {
		el.style.display = "block";
		el.innerHTML = response;
	} else {
		var my_body = document.getElementsByTagName("body");
		var my_div = document.createElement("div");
		my_div.id = "content";
				
		//set the inner content
		my_div.innerHTML = response;
		my_body[0].appendChild(my_div);    
	}
	
	//set the section content header background
	$(section).style.backgroundImage = "url('images/header_"+ section +".jpg')";
	
	//set up the content animation
	ctnAnim = new fx.Anim( section, { duration:750, onComplete:check_reload } );
	ctnAnim.open();
}


function load_bkg(url) {
	if ( el = $('bkg') ) {
		el.style.display = "block";
		el.innerHTML = '<img src="images/bkg_' + section + '.jpg" alt="Monster Massive 07" class="bkg_img" />';
	}
}


function apply_nav_links() {
	var a = document.getElementsByTagName("a");

	$A(a).each( function(el) {
		if (el.parentNode.parentNode.id == "nav" || el.parentNode.parentNode.id == "navextra") {
			var ext = el.className;
			
			el.onclick = function() {
				var url = this.getAttribute("href");

				if ( ext == 'external' ) {
					var newWin = window.open(url);
					newWin.focus;
				} else {
					url = url.split('/');        
					url = url[url.length - 2];
					load_section( (url == "") ? "home" : url );
				}

				return false;
			}
		}
	});
}


function set_nav_btns() {
	var a = document.getElementsByTagName("a");
	
	$A(a).each( function(el) {
		if (el.parentNode.parentNode.id == "nav" || el.parentNode.parentNode.id == "navextra") {
			el.className = (el.parentNode.className == section) ? "selected" : null;
		}
	});
}


//animate the content height
fx.Anim = Class.create();
fx.Anim.prototype = {
	initialize: function(el, options) {
		this.effect = new fx.Height(el, options);
	},
		
	open: function() {
		this.effect.custom(140, $(section).scrollHeight);
		isOpen = true;
	},
	
	close: function() {
		this.effect.custom($(section).scrollHeight, 140);
		isOpen = false;
	}
}

/*
//build the mail form
var mailForm = Class.create();
mailForm.prototype = {
	initialize: function(el) {
		this.el = $(el);
		this.emWatch = new inputWatcher('email', 'Your Email', true);
		this.mbWatch = new inputWatcher('mobile', 'Your Mobile#', false);
		Event.observe('submitSignup', 'click', this.submitForm.bindAsEventListener(this), false);
	},
		
	submitForm: function(e) {  	  
		if( this.emWatch.getIsValid() ) {
			var newWin = window.open(this.el.action, 'confirm', 'width=350,height=350,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no');
			newWin.focus;
		} else {
			Event.stop(e);
			return false;
		}
	}
};


//build the write form
var writeForm = Class.create();
writeForm.prototype = {
	initialize: function(el) {
		this.el = $(el);
		this.emWatch = new inputWatcher('email', '', true);
		Event.observe('submitWrite', 'click', this.submitForm.bindAsEventListener(this), false);
	},
		
	submitForm: function(e) {
		if( this.emWatch.getIsValid() ) {
			var par = Form.serialize(this.el);
			new Ajax.Request( this.el.action, { method:'get', parameters:par, onComplete:this.complete });
		} else {
			Event.stop(e);
			return false;
		}
	},
	
	complete: function(request) {
		if (el = $('result')) {
			el.style.display = "inline";
			el.innerHTML = request.responseText;
		}
	}
};


//focus and blur a field element
var inputWatcher = Class.create();
inputWatcher.prototype = {
	initialize: function(el, fieldDefault, validate) {
		this.el = $(el);
		this.fieldDefault = fieldDefault;
		this.validate = validate;
		this.isValid = false;
		
		this.setField(this.fieldDefault);
		Event.observe(this.el, 'focus', this.focusField.bindAsEventListener(this), false);
		Event.observe(this.el, 'blur', this.blurField.bindAsEventListener(this), false);
	},

	setField: function(v) {
		this.el.value = v;
	},

	setIsValid: function() {
		if(this.el.name == 'email') {
			this.isValid = this.email();
		} 
		else if (this.el.name == 'mobile') {
			this.isValid = this.mobile();
		} else {
			this.isValid = false;    
		}
	},

	getIsValid: function() {
		return this.isValid;
	},

	focusField: function() {
		$(this.el).className = "input_field";
		if($F(this.el) == this.fieldDefault) this.setField('');
	},

	blurField: function() {
		if($F(this.el) == '') this.setField(this.fieldDefault);
		if(this.validate) this.setIsValid();
		if(this.validate && !this.isValid) $(this.el).className = "input_error";
	},

	email: function() {    
		var regexp = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
		return regexp.test($F(this.el));
	},
	
	mobile: function(s) {
		var regexp = /^[+]?\d{10}$/;
		return regexp.test($F(this.el));
	}
};


function initVideoButtons() {
	var wmp = document.getElementsByClassName('wmpLink');
	var qt  = document.getElementsByClassName('qtLink');
	wmp.each( function(el) { new extLinks(el,'1','1', true); });
	qt.each( function(el) { new extLinks(el,'500','380', false); });
}

var pop;

var extLinks = Class.create();
extLinks.prototype = {
	initialize: function(el, width, height, closeIt) {
		this.el      = $(el);
		this.href    = el.href;
		this.width   = width;
		this.height  = height;
		this.closeIt = closeIt;
		this.pop;
		this.createLink();
	},

	createLink: function() {
		Event.observe(this.el, 'click', this.openWindow.bindAsEventListener(this), false);
		this.el.onclick = function() { return false; };
	},
	
	openWindow: function() {
		pop = window.open(this.href, 'video', 'width='+this.width+',height='+this.height+',directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no');
    
		if (this.closeIt) {
		  window.focus();
			setTimeout(this.closeWindow, 2000);
		} else {
			pop.focus();
		}
	},
	
	closeWindow: function() {
    pop.close();
  }
};*/