/* 	
 * 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';
	}
	
	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;
 var playerId;
 function playerReady(obj) {
	player = document.getElementById(obj['id']);
	playerId = 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( id, i ){
	var textId = "media_text_" + id;
 
	oldI = parseInt( 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( textId ).innerHTML="<b>"+playlistTitles[i]+"</b>";
	document.getElementById('anchor'+i).style.color="white";

	newI = parseInt( i ) + 1;
	if( newI==9 ){ newI = 1; }  
 
	var strCall = "setPlaylistContentAuto( '" + id + "', '" + newI + "' );";
	hboldtvUpdatePlaylist=window.setTimeout( strCall, 10000 );
 }

 function resetLinks(newI){
	for (var i = 1; i <= 8; i++){
		if( document.getElementById('anchor'+i) ){
			document.getElementById('anchor'+i).style.color="#000000";
		}
	}

	if(document.getElementById('anchor'+newI)){
		document.getElementById('anchor'+newI).style.color="#FFFFFF";}
 }

 function countMediaView(obj){
	if(obj){
		window.setTimeout('xajax_countMediaView(\''+curFile+'\')',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 });
	}
 }
 
 // TOP MENU
 function showMenu(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=100; i++) {
		if (document.getElementById('srom'+i)) {document.getElementById('srom'+i).style.display='none';}
	}
	
	if (d) {d.style.display='block';}
  }
  
  // CHATWALL
  function setChatwallContent( p, o ){
	xajax_getChatwallContent( xajax.getFormValues('chatwall_Form'), p, o );
    if( document.getElementById('chatwall_Message') ){
		document.getElementById('chatwall_Message').value="";
	}
   }

 function setChatwallContentAuto( p ){
	xajax_getChatwallContent("", p, "0");
	// chatUpdateInner=window.setTimeout("setChatwallContentAuto();", 60000);
 }
 
// NEWSLETTER
function box_newsletter_displayTagOptions( id ){
	$object = document.getElementById( 'box_newsletter_tagOptions_'+id );
	if( $object.style.display == 'none' ){
		$object.style.display = 'block';
	} else { 
		$object.style.display = 'none';
		
		$object = document.getElementById( 'box_newsletter_tagSelector_'+id );
		if( $object.style.display == 'block' ){
			$object.style.display = 'none';
		}
	}
}

function box_newsletter_displayTagSelector( id ){
	$object = document.getElementById( 'box_newsletter_tagSelector_'+id );
	if( $object.style.display == 'none' ){
		$object.style.display = 'block';
	} else { 
		$object.style.display = 'none';
	}
}

function deleteOption(object,index) {
    object.options[index] = null;
}

function addOption(object,text,value) {
    var defaultSelected = true;
    var selected = true;
    var optionName = new Option(text, value, defaultSelected, selected)
    object.options[object.length] = optionName;
}

function copySelected( fromObject, toObject, id ) {

	if( !fromObject ){
		document.getElementById( 'selectedTags_'+ id ).setAttribute( 'name', 'tags_'+ id );
		document.getElementById( 'selectedTags_'+ id ).setAttribute( 'id', 'tags_'+ id );
		
		fromObject = document.getElementById( 'tags_'+ id );
	}
		
	if( !toObject ){
		document.getElementById( 'selectedTags_'+ id ).setAttribute( 'name', 'tags_'+ id );
		document.getElementById( 'selectedTags_'+ id ).setAttribute( 'id', 'tags_'+ id );
		
		toObject = document.getElementById( 'tags_'+ id );
	}

    for (var i=0, l=fromObject.options.length;i<l;i++) {
        if (fromObject.options[i].selected)
            addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
    }
    for (var i=fromObject.options.length-1;i>-1;i--) {
        if (fromObject.options[i].selected)
            deleteOption(fromObject,i);
    }
}

function SelectAll( fromObject, id ) {
	$object = document.getElementById( 'tags_'+ id );	
	if( $object == null ){
		document.getElementById( 'selectedTags_'+ id ).setAttribute( 'name', 'tags_'+ id );
		document.getElementById( 'selectedTags_'+ id ).setAttribute( 'id', 'tags_'+ id );
		
		fromObject = document.getElementById( 'tags_'+ id );
	}

    for (var i=0, l=fromObject.options.length;i<l;i++) {
        fromObject.options[i].text,fromObject.options[i].selected = true;
    }    
	
	fromObject.setAttribute( 'name', 'selectedTags_'+ id +'[]' );
	fromObject.setAttribute( 'id', 'selectedTags_'+ id  );
}

// NEWS FEED
function skift_stylinginfo(){
 if(document.getElementById('style_inaktiv').style.display == 'block'){
  document.getElementById('style_inaktiv').style.display = 'none';
	document.getElementById('style_aktiv').style.display = 'block';
 } else {
  document.getElementById('style_inaktiv').style.display = 'block';
	document.getElementById('style_aktiv').style.display = 'none';
 }
}

// MATCH FEED
function matchFeed_TeamSelect(){
	var object = document.getElementById( 'matchFeed_tournament' );
	if( object != null ){
		var tournament = object.value;
		xajax_matchFeed_teamSelect( tournament );
	}
}

// CLUB VIEW
function sPlayerInfo(playerView) {
var view = document.getElementById(playerView);
	if (view != null) {
		view.style.display = 'block';
	}
}

function rPlayerInfo(playerView) {
var view = document.getElementById(playerView);
	if (view != null) {
		view.style.display = 'none';
	}

}

// UPDATE LIVE SCORE
function sportstats_updateLivescore( tmpId ){
	xajax_sportstats_updateLivescore( tmpId );
	
	window.setTimeout(function(){sportstats_updateLivescore( tmpId );},60000);
}

// SELECT TOURNAMENT TABLE VIEW
function select_tournamentTable(){
	var season = document.getElementById( 'tournamentTable_season' );
	var comp = document.getElementById( 'tournamentTable_comp' );
	var phase = document.getElementById( 'tournamentTable_phase' );
	var type = document.getElementById( 'tournamentTable_type' );
	
	if( season != null && comp != null && phase != null && type != null ) {
		xajax_select_tournamentTable( '' + season.value + '', '' + comp.value + '', '' + phase.value + '', '' + type.value + '' );
	}
}

/* GALLERY IN NEWS - hboldGiN */
 var hboldGiN_step = 1;
 var hboldGiN_leftTimer = '';
 var hboldGiN_rightTimer = '';
 
 function hboldGiN_leftGo( id ){
	clearTimeout( hboldGiN_leftTimer );
	document.getElementById( id ).scrollLeft += hboldGiN_step;
	hboldGiN_leftTimer = setTimeout( "hboldGiN_leftGo('" + id + "' )", 10 )
 }
 
 function hboldGiN_rightGo( id ){
	clearTimeout( hboldGiN_rightTimer );
	document.getElementById( id ).scrollLeft -= hboldGiN_step;
	hboldGiN_rightTimer = setTimeout( "hboldGiN_rightGo('" + id + "' )", 10 )
 }
 
 function hboldGiN_stop(){
	clearTimeout( hboldGiN_leftTimer );
	clearTimeout( hboldGiN_rightTimer );
 }
