
// On document ready
document.observe("dom:loaded", function() {
	
	// Are we on the Home? Is the Home Scrollable there?
	
	if ($('homeSrollable')) {
		
		var controls = $$('#homeSrollable ul.controls');
		var slides = $$('#homeSrollable ul.slides li');
		
		Effect.Appear( controls[0] );
		
		var pos_top = parseInt($('homeSrollable').getHeight()) - 38;
		var pos_left = parseInt($('homeSrollable').getWidth()) - parseInt(controls[0].getWidth()) - 10;
		
		db_total_slides = slides.size();
		
		controls[0].setStyle({
			top: pos_top + 'px'
			,left: pos_left + 'px'
		});
		
		var buttons = $$('#homeSrollable ul.controls li');
		
		buttons.each(function(s, index){
			$(s).observe('click', db_change_slide );
		});
		
		slides.each(function(s, index){
			$(s).observe('mouseover', db_reset_timer );
		});
		
		db_change_slide(0);
		
		db_slider_count = 0;
		home_overlay_activated = false;
		setInterval ( "db_slide_timer()", 1000 );
		
	}

});

function db_change_slide( id ) {
	
	if (typeof id != 'number') {
		id = ( $(this).id.substring(7) );
	}
	
	//console.log("hit id: " + id );
	
	$$('#homeSrollable ul.controls li').each(function(s, index){
		$(s).removeClassName('selected');
	});

	$('button_'+id).addClassName('selected');
	
	// Slide slides
	
	var slides = $$('#homeSrollable ul.slides')[0];
	
	if (myEffect) myEffect.cancel();
	
	var myEffect = new Effect.Move(slides, { 
		x: ( parseInt($('homeSrollable').getWidth()) * (parseInt(id)*-1) )
		, y: null
		, duration: 0.6
		, fps: 30
		, transition: Effect.Transitions.easeInOutQuint 
		, mode: 'absolute'
	});
	
	db_slider_cur = parseInt(id);
}

function db_slide_timer() {
	
	db_slider_count++;
	
	if ( db_slider_count > 4 && !home_overlay_activated ) {
		
		//console.log("CURRENT: " + db_slider_cur + '   -   TOTAL: ' + db_total_slides);
		
		var next_slide = (db_slider_cur + 1);
		
		if ( parseInt(next_slide) > (parseInt(db_total_slides)-1) ) {
			next_slide = 0;
		}
		
		db_slider_cur = parseInt(next_slide);
		db_change_slide( next_slide );
		db_slider_count = 0;
	}
	
	//console.log(db_slider_count);
}

function db_reset_timer() {
	//console.log('reset timer');
	db_slider_count = 0;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function paypal_buy_from_select( form_id ) {
	
	var product_obj = ($(form_id).options[ $(form_id).options.selectedIndex ].value).evalJSON();
	
	paypal_submit_form( product_obj );
	
}

function paypal_submit_form( product_obj ) {
	
	$('paypal_form').item_name.value = product_obj.title;
	$('paypal_form').amount.value = product_obj.price;
	$('paypal_form').shipping.value = product_obj.shipping_price;
	
	$('paypal_form').submit();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function check_mailing_list_form(ref) {
	
	// No special checks at this time. Later we may use this space to include special checks
	// outside the scope of checkform()
	
	return checkform( ref );	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// Pop up the Jukebox window

function launch_jukebox( url ) {
	
	var jukebox = window.open(
		url
		,"jukebox"
		,"location=0,status=0,scrollbars=0,width=450,height=330"
	);
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function toggle_blind( id ) {
	
	if ( $( id ).visible() ) {
		Effect.BlindDown( id );
		
	} else {
		Effect.BlindDown( id );
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function erase_input_default( form, defaultValue, elemID ) {
	
	if (elemID) {
		// The element's ID has been supplied. This is being used for the search
		// input in the global nav. We're going to turn this copy from grey to black...
		var elem = $(elemID);
		
		elem.setStyle({color:"#000000"});
	}
	
	if (form.value == defaultValue) {
		form.value = '';
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function checkform(ref)
{
	var errorActivated = false;
	
	// Test if DOM is available and there is an element called required
	if(!document.getElementById || !document.createTextNode){return;}
	if(!document.getElementById('required')){return;}

	// Define error messages and split the required fields
	var errorID='errormsg';
	var errorClass='_error';
	var errorMsg='';
	var reqfields=document.getElementById('required').value.split(',');
	
	// Clean up old mess
	
	$('form_error_msg').hide();
	
	for(var i=0;i<reqfields.length;i++)
	{
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		
		var curClassName = f.className;
		var index = curClassName.indexOf(errorClass);
		
		if (index != -1) {
			f.className = curClassName.substr(0,index);
		}
	}
		
	// loop over required fields
	for(var i=0;i<reqfields.length;i++)
	{
		
		// check if required field is there
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		
		// test if the required field has an error, 
		// according to its type
		switch(f.type.toLowerCase())
		{
			case 'text':
			
				// email is a special field and needs checking
				if(f.id.indexOf('email') != -1 && !cf_isEmailAddr(f.value)){cf_add_error(f, 'Email address is invalid.')}
				
				if(f.value=='' && f.id!='email'){cf_add_error(f, 'Please fill out the required fields.')}							
				
										
			break;
			case 'textarea':
				if(f.value==''){cf_add_error(f)}							
			break;
			
			case 'password':
				if(f.value==''){cf_add_error(f)}
				
				// password must be four characters
				if(f.value.length < 4){cf_add_error(f, 'Password must be at least 4 characters')}
				
			break;
			
			case 'checkbox':
				if(!f.checked){cf_add_error(f)}							
			break;
			case 'select-one':
				if(!f.selectedIndex && f.selectedIndex==0){cf_add_error(f)}							
			break;
		}// end switch
	}// end for
	
	
	return !errorActivated;
	
	
	function cf_isEmailAddr(str) 
	{
	    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
	
	function cf_add_error(o,msg)
	{
		errorMsg += msg;
		
		if (o.className.indexOf(errorClass) == -1) o.className= o.className + errorClass;
		
		// Check if there is no error message
		if(!errorActivated)
		{
			// create errormessage and insert before submit button
			errorActivated = true;
			Element.update('form_error_msg', "<p>"+errorMsg+"</p>" );
			
			$('form_error_msg').show();
			new Effect.Highlight( $('form_error_msg'), {
				'startcolor':'#ffffff'
			} );
			
			if (msg != '' && msg != undefined) {
				//alert(msg);
			}
				
		} 
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.relatedElement.className = 'toomuch';
	} else {
		this.relatedElement.className = '';
	}
	this.relatedElement.firstChild.nodeValue = currentLength;
	
	// not innerHTML
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function getPageSize() {
        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

