/* 	
 * This software is (c) 2009 Emtee Software & Consult, DK-8000 Aarhus, and all rights are reserved.
 * For further information see: http://www.emtee.dk, or contact info@emtee.dk
 */
 
 // ONLOAD HANDLER
 function addLoadEvent( func ){
	var oldonload = window.onload;
	if ( typeof window.onload != 'function' ) {
		window.onload = func;
	} else {
		window.onload = function() {   
			if ( oldonload ) {
				 oldonload();
			}
			func();
		}
	}
 }
 
 // OCTO SET DYNAMIC BACKGROUND
 function octo_setDynbg( file, url, id, site, type ){
	var oBody = document.getElementById('octo_body');
	
	var docHeight = Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
	
	/*
	var docWidth = 0;
	if( document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ){
		docWidth = document.documentElement.offsetWidth;
	}
	if( window.innerWidth ){
		docWidth = window.innerWidth;
	}
	if( document.body && document.body.offsetWidth ){
		docWidth = document.body.offsetWidth;
	}
	if( document.body && document.body.clientWidth ){
		docWidth = document.body.clientWidth;
	}
	*/
	
	oBody.style.height = docHeight + 'px';
//	oBody.style.width = docWidth + 'px';
	oBody.style.backgroundImage = 'url("' + file + '")';
	
	if( type==1 ){
		oBody.style.backgroundPosition = 'center top';
		oBody.style.backgroundRepeat = 'no-repeat';
		oBody.style.backgroundAttachment = 'fixed';
	}
	
	if( type==2 ){
		oBody.style.backgroundPosition = 'center top';
		oBody.style.backgroundRepeat = 'no-repeat';
	}
	
	if( type==4 ){
		oBody.style.backgroundPosition = 'center top';
		oBody.style.backgroundRepeat = 'no-repeat';
	}
	
	if( url!='' ){
		oBody.style.cursor = 'pointer';
		oBody.onclick = function() { octo_addClick( id, site ); window.open( url ); }
	}
 }
 
 // SET VALUE
 function octo_setValue( id, value ){
	var object = document.getElementById( id );
	if( object != null ){
		object.value = value;
	}
 }
 
 // SET CHECKED
 function octo_setCheck( id, value ){
	var object = document.getElementById( id );
	if( object != null ){
		if( value==0 ){
			object.checked = false;
		} else {
			object.checked = true;
		}
	}
 }
 
 // SET HTML
 function octo_setHTML( id, value ){
	var object = document.getElementById( id );
	if( object != null ){
		object.innerHTML = value;
	}
 }

 // ADD ELEMENT CLICKS
 function octo_addClick( id, site ){
	xajax_octo_addClick( id, site );
 }
 
 // ADD VOTES
 function octo_addVote( form ){
	xajax_octo_addVote( xajax.getFormValues(form) );
 }
 
 // REDIRECT
 function octo_redirect( url ){
	window.location = url;
 }
 
 // DISPLAY
 function octo_displayElement() {
	for(i = 0; i < arguments.length;i++) {
		var element = document.getElementById(arguments[i]);
		if (element != null) {
			element.style.display = 'block';
		}
	} 
 }

 // HIDE	
 function octo_hideElement() {
	for(i = 0; i < arguments.length;i++) {
		var element = document.getElementById(arguments[i]);
		if (element != null) {
			element.style.display = 'none';
			element.innerHTML = '';
		}
	} 
 }
 
 // DISPLAY IF HIDDEN, HIDE IF DISPLAYED
 function octo_toggleDisplayElement() {
	for(i = 0; i < arguments.length;i++) {
		var element = document.getElementById(arguments[i]);
		if (element != null) {
			if( element.style.display == 'none' ){
				element.style.display = '';
			} else {
				element.style.display = 'none';
			}
		}
	} 
 }
 
 
 // LOGIN FORM
 function submitForm_login() {
	var form = document.getElementById('frm_login'); 
		if (form != null) {
			document.getElementById('response_login').innerHTML = 'Vent et øjeblik...';			
			document.getElementById('response_login').style.display = 'block';
			xajax_frm_login_frontend( xajax.getFormValues('frm_login') );
		}
 }
 
 function switchto(q){ 
    if (q){ 
        document.getElementById('passwordtextI').style.display="none"; 
        document.getElementById('passwordI').style.display="inline"; 
        document.getElementById('passwordI').focus(); 
    } else { 
        document.getElementById('passwordI').style.display="none"; 
        document.getElementById('passwordtextI').style.display="inline"; 
    } 
 } 
 
 function btnRememberMe(){
	var state = document.getElementById( 'state_rememberMe' );
	var button = document.getElementById( 'input_rememberMe' ); 
	if( state != null && button!= null ){
		if( state.value == 0 ){
			state.value = 1;
			button.checked = true;
		} else {
			state.value = 0;
			button.checked = false;
		}
	}
 }
 
 // OCTO FORMS
 function submitForm_octo( formName ) {
	var form = document.getElementById( formName ); 
		if (form != null) {
			form.submit();
		}
 }
 
 function submitForm2Ajax_octo( formName ) {
	var form = document.getElementById( formName );
		if (form != null) {
			xajax_octo_frmHandler( xajax.getFormValues( form ) );
		}
 }
 
 // 
 function octo_waitMsg( id ){
	$object = document.getElementById( id );
	if( $object!=null ){
		$object.innerHTML = '<p>Vent et øjeblik...</p>';
		$object.style.display = 'block';
	}
 }
 
 // OCTO GALLERY ROTATOR
 function octo_galleryRotator(){
	var dataJson = eval( "(" + xajax.call('octo_getGalleryTop',{parameters: [], mode:'synchronous'}) + ")" );
 
	if( dataJson != null ){
		this.data = dataJson;
		this.max_imgGallery = dataJson.length;
		this.cur_imgGallery = -1;
	
		this.rotate();
	}
 }
 
 octo_galleryRotator.prototype.set = function( id ) {
	octo_galleryRotator.cur_imgGallery = ( id-1 );
	
	clearTimeout( octo_galleryRotator.timer_rotator );
	octo_galleryRotator.rotate();
 }
 
 octo_galleryRotator.prototype.over = function( id ) {
	if( this.cur_imgGallery != id ) {
		document.getElementById('imgGallery_'+id).style.backgroundColor='#FBA90D';
	}
 }

 octo_galleryRotator.prototype.out = function( id ) {
	if( this.cur_imgGallery != id ) {
		document.getElementById('imgGallery_'+id).style.backgroundColor='#A4A4A4';
	}
 }
 
 octo_galleryRotator.prototype.rotate = function() {
	this.cur_imgGallery++;
	
	if( this.cur_imgGallery==this.max_imgGallery ) {
		this.cur_imgGallery = 0;
	}
	
	xajax_octo_galleryRotator_rotate( this.data, this.cur_imgGallery );
	
	this.timer_rotator = window.setTimeout(function(){octo_galleryRotator.rotate();},20000);
 }
 
 // FACEBOOK SHARE SCRIPT
 function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
 }
 
 // CHECKBOX AS RADIO
 function octo_chkAsRad( form, chk ){
	var form = document.getElementById( form );
	for ( var x=0; x<form.length; x++ ) {
		if( form.elements[x].type == 'checkbox' ){
			if( form.elements[x].id == chk ){
				form.elements[x].checked = true;
			} else {
				form.elements[x].checked = false;
			}
		}		
	}
 }
 
 // PRINT HTML
 function octo_print( element ){
	var object = document.getElementById( element );
	
	if( object != null ) {
		var printing = window.open('','','left=0,top=0,width=550,height=400,toolbar=0,scrollbars=0,sta­tus=0');
		printing.document.write( object.innerHTML );
		printing.document.close();
		printing.focus();
		printing.print();
		printing.close();
	}
 }
 
 // SCROLL
 function octo_setScroll( id, direction ){
	var oDiv = document.getElementById( id );
		
	switch( direction ){
		case "right":
			oDiv.scrollLeft = oDiv.scrollWidth;
			break;
		case "bottom":
			oDiv.scrollTop = oDiv.scrollHeight;
			break;	
	}
 }
 
 // FLASH PLAYER
 var currentState = "NONE"; 
 var previousState = "NONE";

 var player;
 function playerReady(obj) {
	player = document.getElementById(obj['id']);
	curFile = player.getPlaylist()[0].file;
	addListeners();
 }

 function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
		player.addControllerListener("PLAYLIST", "setCurFile");
/*		player.addViewListener("PLAY", "countMediaView"); */
		player.addViewListener("PLAY", "addSettings");
	} else {
		listenerTO=window.setTimeout("addListeners()",100);
	}
 }


 function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate;

             if(currentState=="PLAYING"){window.clearTimeout(hboldtvUpdatePlaylist);} 
 }

 function setCurFile() {
	curFile = player.getPlaylist()[0].file;
 }

 function setPlaylistContentAuto(i){
	oldI=i-1;
	if(oldI==0){oldI=8;}
 
	document.getElementById('anchor'+oldI).style.color="black";

	document.getElementById('playerID').sendEvent('LOAD', {file:playlistFile[i],image:playlistImage[i]});

	document.getElementById('hboldtv_titel').innerHTML="<b>"+playlistTitles[i]+"</b>";
	document.getElementById('anchor'+i).style.color="white";

	newI=i+1;
	if(newI==9){newI=1;}  
 
	hboldtvUpdatePlaylist=window.setTimeout("setPlaylistContentAuto(newI);", 10000);
 }

 function resetLinks(newI){
	for (var i = 1; i <= 5; i++){
		if( document.getElementById('anchor'+i) ){
			document.getElementById('anchor'+i).style.color="#666666";
		}
	}

	if(document.getElementById('anchor'+newI)){
		document.getElementById('anchor'+newI).style.color="#006699";}
 }

 function countMediaView(obj){
	if(obj){
		var xplCurFile	= curFile.split("/");
		var xplCurFile	= xplCurFile[2].split("_");
		var file_id = xplCurFile[0];

		window.setTimeout('xajax_countMediaView(\''+file_id+'\')',1000);
	}
 }
 
 function addSettings() {
	var today = new Date();
	var expirationDate = new Date();
	expirationDate.setFullYear(2011,0,1);
	
	// var add = "/media/310_bitter.flv";
	// var add = "/includes/hummel/CarlBloopersWEB_Widescreen_960x540.flv";
	
	if (today < expirationDate && add != "" && curFile != add) {
		playAddListenerPost(curFile, add);
	}
 }

 function playAddListenerPost(url, add) {
	var state = document.getElementById('playerID').getConfig().state;
	
	if (state != 'COMPLETED') {
		window.setTimeout(function(){playAddListenerPost(url, add);},100);
	}

	if (state == 'COMPLETED') {
		document.getElementById('playerID').sendEvent('LOAD', {file:'' + add });
		document.getElementById('playerID').sendEvent('PLAY');
		
		stopAdd(url);
	}
 }

 function stopAdd(url) {
	var state = document.getElementById('playerID').getConfig().state;
	
	if (state != 'COMPLETED') {
		window.setTimeout(function(){stopAdd(url);},100);
	}

	if (state == 'COMPLETED') {
		document.getElementById('playerID').sendEvent('LOAD', {file:'' + url });
	}
 }
 
 // MANAGER
 function manager_selectTeamDisplay( team ){
	var object_1 = document.getElementById( 'manager_selectTeamDisplay' );
	var object_2 = document.getElementById( 'manager_selectRiderDisplay' );
	
	if( object_1!=null && object_2!=null ){
		var retVal = object_1.value + '_' + object_2.value;
	} else {
		var retVal = '0_0';
	}
	
	xajax_manager_setTeamContent( team, 'riderList', 0, retVal );
 }
 
 function manager_selectStatDisplay( game ){
	var object_1 = document.getElementById( 'manager_selectStatDisplay' );
	
	if( object_1!=null ){
		var retVal = object_1.value;
	} else {
		var retVal = '0';
	}
	
	xajax_manager_setStatsContent( game, retVal );
 }
 
 function manager_updateTotal( form ){
	var oForm = document.getElementById( form );
	var total = 0;
	var value;
	var tmp;
	var tmp_arr;
	
	for( i=0; i < oForm.elements.length; i++ ){
		tmp = oForm.elements[i].name;
		if( tmp=="access_userReg" ){
			value = oForm.elements[i].value;
		} else if( tmp=="smsNotification" ){
			valueSms = oForm.elements[i].value;
			if( valueSms=="none" ){
				valueSms = 0;
			}
		} else {
			tmp_arr = tmp.split('_');
			if( tmp_arr[1]=="team" && oForm.elements[i].checked ){
				total++;
				smsElement = document.getElementById( 'managerTeamlist_sms_' + tmp_arr[2] );
				if( smsElement!=null ){
					smsElement.disabled = false;
				}
			} else if( tmp_arr[1]=="team" && !oForm.elements[i].checked ){
				smsElement = document.getElementById( 'managerTeamlist_sms_' + tmp_arr[2] );
				if( smsElement!=null ){
					smsElement.checked = false;
					smsElement.disabled = true;
				}
			} else if( tmp_arr[1]=="sms" && oForm.elements[i].checked ){
				totalSms++;
			}
		}
	}

	document.getElementById( form + '_total' ).innerHTML = ( total * value ) + ( totalSms * valueSms ) +" DKK";
 }
 
 /* GALLERY IN NEWS - feltetGiN */
 var feltetGiN_step = 1;
 var feltetGiN_leftTimer = '';
 var feltetGiN_rightTimer = '';
 
 function feltetGiN_leftGo( id ){
	clearTimeout( feltetGiN_leftTimer );
	document.getElementById( id ).scrollLeft += feltetGiN_step;
	feltetGiN_leftTimer = setTimeout( "feltetGiN_leftGo('" + id + "' )", 10 )
 }
 
 function feltetGiN_rightGo( id ){
	clearTimeout( feltetGiN_rightTimer );
	document.getElementById( id ).scrollLeft -= feltetGiN_step;
	feltetGiN_rightTimer = setTimeout( "feltetGiN_rightGo('" + id + "' )", 10 )
 }
 
 function feltetGiN_stop(){
	clearTimeout( feltetGiN_leftTimer );
	clearTimeout( feltetGiN_rightTimer );
 }
