var faders = new Array();
var faderTimers = new Array();
var faderTotals = new Array();
var faderPointers = new Array();
var baseUrl;

$(document).ready( function() {
	baseUrl = $('base').attr('href');
	swfobject.embedSWF(baseUrl+"header.swf", "logo", "950", "205", "9.0.0", baseUrl+"header.swf", false, { bgcolor: "#000000" }, false );
	setFader( '.new' );
	setFader( '#new_products > .bproduct' );
	setFader( '#special_products > .bproduct' );
	$('input, textarea, select').focus( function() {
		if( $(this).css('background-color') == 'rgb(106, 19, 19)' || $(this).css('background-color') == '#6a1313' )
			$(this).css('background-color', '#141414');
	});
	
	$(window).resize(function(){
		setMetalLines();
	});
	setMetalLines();
	$('.cart_songs > a#shs').html( ECOMLang.showSongs );
});

function isValidEmail( email ) {
	var	checkStr = email;
	var	emailValid = false;
	var	emailAt	= false;
	var	emailPeriod	= false;
	for	(i = 0;	i < checkStr.length; i++)
	{
		ch = checkStr.charAt(i);
		if ((ch == "/") || (ch == "^") || (ch == "$") || (ch == "#") || (ch == "§") || (ch == "*") || (ch == "?") || (ch == "%") || (ch == "&")){
			error = true;
		} else {
			if (ch == "@"){
				emailAt	= true;
			}
			if (ch == "."){
				emailPeriod	= true;
			}
		}
	}
	if (emailAt && emailPeriod)
		return true;
	return false;
}


function setFader( name ) {
	var fader_id = faders.length;
	faders[fader_id] = name;
	faderTotals[fader_id] = $( name ).size();
	if( faderTotals[fader_id] > 0 ) {
		handleFader( fader_id, true );
	}
}

function relocate( url ) {
	location.href = url;
}

function goToSlice( id ) {
	if( id != 0 )
	return relocate( baseUrl + 'browseslice/' + id );
	return;
}

function setMetalLines( ) {
	var t = $('#footer').offset();
	var borderHeight = $('#footer').height() + t.top;
	/*alert( t.top + ' ' +  $('body').height() );
	var borderHeight = $('body').height(); */
	$('.border').css( 'height', borderHeight + 'px' );
}

function handleFader( fid, init ) {
	faderTimers[fid] = setTimeout( 'handleFader( '+fid+' )', 6000 );
	if( init == true ) {
		faderPointers[fid] = 0;
		$(faders[fid]+':eq('+(faderPointers[fid])+')').show();
		return;
	}

	$(faders[fid]+':eq('+faderPointers[fid]+')').fadeOut('slow', function() {
		if( faderPointers[fid] == faderTotals[fid]-1 )
		faderPointers[fid] = 0;
		else faderPointers[fid] = faderPointers[fid] + 1;
		$(faders[fid]+':eq('+(faderPointers[fid])+')').fadeIn('slow');
	});

}

var page_url = '';

function setUrl( set_url ) {
	page_url = set_url;
}

var installments_opened = false;
function showInstallments( price ) {
	if( installments_opened == true ) {
		$('#dialog').dialog('open');
		return;
	}
	var dialog = document.createElement('div');
	$( dialog ).attr('id', 'dialog');
	$( dialog ).html( ECOMLang.Loading );
	$('body').prepend( dialog );
	installments_opened = true;
	$("#dialog").dialog( { title: ECOMLang.Loading, width: 520, height: 505, resizable: false } );
	$('#dialog').show();
	$("#dialog").load( baseUrl + 'payments #installments_title, #installments_table', { price: price }, function() {
		var title = $("div#installments_title").html();
		$("div#installments_title").remove();
		$('#dialog').data('title.dialog', title);
	} );
}


			function WriteError( input_name, error ) {
				var div_id = 'error_' + input_name;
				var count = 0;
				if( obj = document.getElementById( div_id ) ) {
					if( error.length != 0 ) {
						obj.innerHTML = error;
					}
					if( obj2 = document.getElementsByName( input_name )[0] ) {
						if( obj2.type != 'checkbox' && obj2.type != 'radio' && obj2.type != 'submit' ) {
							obj2.style.background = '#6a1313';
							count++;
						}
					}
				} else {
					alert( error + " @ " + input_name );
				}
			}
function WriteErrorAff( input_name, error, formId ) {
	var div_id = 'error_' + input_name;
	var fe_selector = '';

	if( formId != undefined )
	fe_selector = 'form#'+formId+' > ';

	if( $('#'+div_id).size() > 0 ) {
		$('#'+div_id).html( error );
		/*var temp = fe_selector + '* [name="'+input_name+'"]';
		if( $( temp ).size() > 0 ) {
			var type = $( temp ).attr('type');
			if( type != 'checkbox' && type != 'radio' && type != 'submit' ) {
				$( temp ).css( 'background-color', '#c29996' );
			}
		}*/
	}
}

function CatShowHide(DivName) {
	var obj = document.getElementById('cat_'+DivName);
	var divs = obj.getElementsByTagName('DIV');
	var spans = obj.getElementsByTagName('SPAN');
	for( var i = 0; i < divs.length; i++){
		divs[i].style.display = divs[i].style.display == "none" ? "" : "none"
	}
	for( var i = 3; i < spans.length; i++){
		spans[i].style.display = spans[i].style.display == "none" ? "" : "none"
	}
}

var start=new Date();
start=Date.parse(start)/1000;
var myRef="";
var counts = 0;
function setUrl(ref) {
	myRef=ref;
}
function setCounts(mycount) {
	counts = mycount;
}

function CountDown( ){
	var now=new Date();
	now=Date.parse(now)/1000;
	var x=parseInt(counts-(now-start),10);
	document.getElementById("myclock").innerHTML = x;
	if(x>0){
		timerID=setTimeout("CountDown()", 100)
	}else{
		location.href=myRef;
	}
}

function SliceSH( name, stext, htext ) {
	var sh_link = document.getElementById( name + '_sh_link' );
	if( obj = document.getElementById( name + '_sh' ) ) {
		var new_id = name + '_hs';
		while( obj = document.getElementById( name + '_sh' ) ) {
			obj.style.display = 'block';
			obj.id = new_id;
		}
		sh_link.innerHTML = htext;
	} else if( obj = document.getElementById( name + '_hs' ) ) {
		var new_id = name + '_sh';
		while( obj = document.getElementById( name + '_hs' ) ) {
			obj.style.display = 'none';
			obj.id = new_id;
		}
		sh_link.innerHTML = stext;
	} else {
		return;
	}
}

function SHSongs( ident ) {
	var OLselector = $( '#' + ident + ' > ol');
	var Aselector = $( '#' + ident + ' > a#shs');
	if( OLselector.css('display') == 'none' ) {
		OLselector.slideDown();
		Aselector.html( ECOMLang.hideSongs );
	} else {
		OLselector.slideUp();
		Aselector.html( ECOMLang.showSongs );
	}
}

function enlargeImage( product_id ) {
	var url = baseUrl + "product/"+product_id+"/enlarge";
	window.open( url, "", "resizable=1,HEIGHT=200,WIDTH=200");
}
