var playerPlaying = false; // know weather the player/image are being "played"
var currentDiv = "playerImages"; // know weather the player/image are being "played"
var currentType = "camp"; // know weather the player/image are being "played"
var player; //the swfobject player id
var so;	//swf object
var timerInfo;	//just a timer var
var photoArray = new Array();	//array holding current photos for album
var autoscrollEnabled = false;	//is autoscrool enabled
var autoscrollTimer;	//timer for the autoscroll
var autoscrollArray = new Array();	//array holding campaigns for autoscroll
var autoscrollPosition = 0;	//current position of the autoscroll
var autoscrollSpeed = 5;	//amoung of time between autoscroll (in seconds)
var autoscrollRestart = 5;	//amoung of time between autoscroll (in seconds)
var pauseSpeed = 200;
var defaultPlaying = true;

//change language
function changeLang(){
	url = window.location.href;
	url = url.replace(BASE_URL, '');
	goTo(BASE_URL + 'includes/changeLang.inc.php?page=' + url);
}

//open popup window
function openPopup(url){
	window.open(url,'popup','toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=1000,height=800');
}

//goto whatever page
function goTo(url){
	window.location = url;
}

function hideElement(elem, hide){
	if (hide == true)
		document.getElementById(elem).style.display = "none";
	else
		document.getElementById(elem).style.display = "block";
}

//textbox show/hide manipulation
function textAction(textbox, text, onFocus){
	if (onFocus){
		if (document.getElementById(textbox).value == text){
			document.getElementById(textbox).value = "";
			document.getElementById(textbox).className = "";
		}
	}else{
		if (document.getElementById(textbox).value == ""){
			document.getElementById(textbox).value = text;
			document.getElementById(textbox).className = "dull";
		}
	}
}

//validate email address
function validateNewsletter(textBoxValue, errorMsg) {
	var str		=	document.getElementById('newsletter_email').value;
	var at		=	"@"
	var dot		=	"."
	var lat		=	str.indexOf(at)
	var lstr	=	str.length
	var ldot	=	str.indexOf(dot)
	var error = false;
	
	if (str == "" || str == null || str == textBoxValue)		error = true;
	if (str.indexOf(at)==-1)	error = true;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)		error = true;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)		error = true;
	if (str.indexOf(at,(lat+1))!=-1)		error = true;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)		error = true;
	if (str.indexOf(dot,(lat+2))==-1)		error = true;
	if (str.indexOf(" ")!=-1)		error = true;

	if(error){
		alert(errorMsg);
		return false;
	}
	
	return true;
}

//dim the current element
function dimElem(elem, dim){
	if (dim){
		elem.setAttribute("class", "item_layer dimElement");
		elem.className = 'item_layer dimElement';
	}else{
		elem.setAttribute("class", "item_layer brightElement");
		elem.className = 'item_layer brightElement';
	}
}

//dim the current element
function superDimElem(elem, dim){
	if (dim){
		elem.setAttribute("class", "superDimElement");
		elem.className = 'superDimElement';
	}else{
		elem.setAttribute("class", "brightElement");
		elem.className = 'brightElement';
	}
}

//hide the current element
function hideElem(elem, hide){
	if (hide){
		elem.style.display = "none";
	}else{
		elem.style.display = "block";
	}
}

function anchorScroll(elem){
	 jQuery.scrollTo("#"+elem, 800); return false;
	 //$(elem).scrollTo();
}

function changeCursor(pointer){
	if(pointer)	 document.body.style.cursor='pointer';
	else	document.body.style.cursor='auto';
}

function changeSubMenu(elem, over){
	if (over){
		document.getElementById(elem).className = "choix_titre_element_selected";
	}else{
		document.getElementById(elem).className = "choix_titre_element";
	}
}

/*--------------------
		CAMPAIGNS VISUALS
--------------------*/
//load the photo info
function loadPhotoAlbum(id, position){
	jQuery("#photoHolder").load( BASE_URL + 'ajax/campaign_photo.ajax.php', {'id' : id, 'position' : position});
//	clearTimeout(timerInfo);
//	timerInfo = setTimeout("startAutoscroll(true)", (autoscrollRestart * 1000));
}
//switch photos
function nextPhoto(isNext){
	stopAutoscroll();
	playerPlaying = true;
	var position 		= Number(document.getElementById('currentPhoto').value);
	var totalPhotos	= document.getElementById('totalPhotos').value;
	var newPosition = 0;
	
	if (isNext){
		if (position+1 < totalPhotos)	newPosition = position+1;
	}else{
		if (position-1 < 0)	newPosition = totalPhotos-1;
		else	newPosition = position-1;
	}
	
	if (newPosition < 0 || newPosition > totalPhotos) newPosition = 0;
			
	document.getElementById('currentPhoto').value = newPosition;
	document.getElementById('currentPhotoCounter').innerHTML = newPosition+1;
	document.getElementById('Photo').src = photoArray[newPosition];
//	clearTimeout(timerInfo);
//	timerInfo = setTimeout("startAutoscroll(true)", (autoscrollRestart * 1000));

}
//change campaign
function changeVisuals(campaign, type, source){
	jQuery("#AJAXempty").load( BASE_URL + 'ajax/campaign_counter.ajax.php', {'campaign' : campaign});
	
	hideAllDisplayDiv("");
	
	if (type == "VID" || type == "AUD"){
		playerPlaying = true;
		currentDiv = "videoHolder";
		hideElement('videoHolder', false);
		defaultPlaying = false;
		startPlayer(campaign, source, true);
	}else if (type == "PHO"){
		currentDiv = "photoHolder";
		hideElement('photoHolder', false);
		loadPhotoAlbum(campaign, 0)
	}
}


/*--------------------
		CAMPAIGNS (portfolio, service, interactive solutions)
--------------------*/

//show campaign info from text boxes
function showInfoText(type, section){
	jQuery("#AJAXinfo").load( BASE_URL + 'ajax/texte_info.ajax.php', {'type' : type, 'section' : section});
}

//show the player image
function showPlayerImage(campaign){
	hideAllDisplayDiv("playerImages");
	jQuery("#playerImages").load( BASE_URL + 'ajax/campaign_player_image.ajax.php', {'campaign' : campaign, 'section' : document.getElementById('section').value, 'subsection' : document.getElementById('subsection').value});
}

function showInfoCampaign(campaign, section, subsection){
	jQuery("#AJAXinfo").load( BASE_URL + 'ajax/campaign_info.ajax.php', {'campaign' : campaign, 'section' : section, 'subsection' : subsection});
}

function changeCampaign(campaign, type, section, subsection){
	stopAutoscroll();
	playerPlaying = true;
	document.getElementById('currentCampaign').value = campaign;
	document.getElementById('currentInfo').value = "CAMP";
	showInfoCampaign(campaign, section, subsection);
	changeVisuals(campaign, type, 'camp');
}

function showCampaign(restartScroll, campaign, type, section, subsection, infoType){
	clearTimeout(timerInfo);
	timerInfo = setTimeout("showCampaignAfterPause("+restartScroll+", '"+campaign+"','"+type+"','"+section+"','"+subsection+"','"+infoType+"')", pauseSpeed);
}

function showCampaignAfterPause(restartScroll, campaign, type, section, subsection, infoType){
//	if (restartScroll) restartAutoscroll();
//	else	stopAutoscroll();
	showPlayerImage(campaign);
	if (infoType == "PORT" || infoType == "SOLU" || infoType == "SERV"){
		showInfoText(infoType, subsection);
	}else{ //infoType == CAMP
		showInfoCampaign(campaign, section, subsection);
	}
}

function showService(restartScroll, servId, campaign, type){
	clearTimeout(timerInfo);
	timerInfo = setTimeout("showServiceAfterPause("+restartScroll+", '"+servId+"','"+campaign+"','"+type+"')", pauseSpeed);
}

function showServiceAfterPause(restartScroll, servId, campaign, type){
//	if (restartScroll) restartAutoscroll();
//	else	stopAutoscroll();
	if (servId == ""){
		showInfoText("SERV", document.getElementById('subsection').value);
	}else{
		showInfoText("SERV-INFO", servId);
	}
	showPlayerImage(campaign);
}

function changeService(campaign, servId, type){
	stopAutoscroll();
	playerPlaying = true;
	document.getElementById('currentID').value = servId;
	document.getElementById('currentCampaign').value = campaign;
	document.getElementById('currentInfo').value = "SERV-INFO";
	showInfoText("SERV-INFO", servId);
	changeVisuals(campaign, type, 'camp');
}

//hides all the posible windows to show stuff
function hideAllDisplayDiv(exception){
	if (exception == "playerImages")	hideElement('playerImages', false);
	else	hideElement('playerImages', true);
	if (exception == "photoHolder")		hideElement('photoHolder', false);
	else	hideElement('photoHolder', true);
	if (exception == "videoHolder")					hideElement('videoHolder', false);
	else	hideElement('videoHolder', true);
}

/*--------------------
		TEAM
--------------------*/
//pause before team info IMPORTANT for mouseover movements
function pauseTeamInfo(team){
	showTeamInfo(team)
}
//loading the team member informaiton
function showTeamInfo(team){
	//clearTimeout(timerInfo);
	jQuery("#AJAXinfo").load( BASE_URL + 'ajax/team_info.ajax.php', {'team' : team});
}
//side bar clicked on team page
function changeTeam(team, campaign, type){
//	stopAutoscroll();
	showTeamInfo(team);
	changeVisuals(campaign, type, 'camp');
	///showCampaign(false, campaign, type, "team", team,"TEAM");
	document.getElementById('currentCampaign').value = campaign;
	document.getElementById('currentTeam').value = team;
}


/*--------------------
		AUTOSCROLL
--------------------*/
//start interval timer if enabled
function startAutoscroll(enabled){
	if (enabled && autoscrollArray.length > 1){
		autoscrollPosition = 0;
		autoscrollEnabled = true;
		autoscrollTimer = setInterval('autoscroll()', (autoscrollSpeed*1000));
		playerPlaying = false;
	}
}
//interval function
function autoscroll(){
	showInfoText(document.getElementById('section').value, document.getElementById('subsection').value)
	playerPlaying = false;
	elem = document.getElementById('itemLayer['+autoscrollPosition+"]");
	if (autoscrollPosition != 0)
		prevElem = document.getElementById('itemLayer['+(autoscrollPosition-1)+"]");
	else prevElem = document.getElementById('itemLayer['+(autoscrollArray.length-1)+"]");
	
	if(prevElem)
		dimElem(prevElem, false); 
	
	if (autoscrollPosition >= autoscrollArray.length)
		autoscrollPosition = 0;
	
	elem = document.getElementById('itemLayer['+autoscrollPosition+"]");
	dimElem(elem, true);
	showPlayerImage(autoscrollArray[autoscrollPosition]);
	autoscrollPosition++;
}
//stop interval timer
function stopAutoscroll(){
	//clearTimeout(timerInfo);
	if (autoscrollEnabled){
		autoscrollEnabled = false;
		clearInterval(autoscrollTimer);
		if (autoscrollPosition == 0)	elem = document.getElementById('itemLayer['+(autoscrollArray.length-1)+"]");
		else 	elem = document.getElementById('itemLayer['+(autoscrollPosition-1)+"]");
		dimElem(elem, false); 

	}
}
/*
//restart autoscrolling
function restartAutoscroll(){
	clearTimeout(timerInfo);
	playerPlaying = false;
	startAutoscroll(true);
}
*/

/*--------------------
		FOOTER
--------------------*/
//share to facebook link popup maker
function shareFb(url, title){
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(title),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}


/*--------------------
	PLAYER FUNCTIONS
--------------------*/
//play the movie
function startPlayer(id, type, autostart){
	playerPLaying = true;
	if(navigator.userAgent.match(/iPad/i) != null) {	//iPad
		document.getElementById('swfPlayer').innerHTML =  "Unavailable";
	}else{
		so = new SWFObject(BASE_URL + 'includes/player-licensed.swf','mpl','640','380','8');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addVariable('width','640');
		so.addVariable('height','380');
		so.addVariable('file', BASE_URL + 'includes/playlist_video.inc.php?info='+id+'-_-'+type);
		so.addVariable('backcolor','0x000000');
		so.addVariable('frontcolor','0xe99e21');
		so.addVariable('lightcolor','0xe97820');
		so.addVariable('screencolor','0x000000');
		so.addVariable('linkfromdisplay','true');
		so.addVariable('searchbar','false');
		so.addVariable('autostart',autostart);
		so.addParam('wmode', 'opaque');
		
		so.addVariable('enablejs', 'true');
		so.addVariable('javascriptid', 'mediaplayer');
	
		so.write('swfPlayer');
	}
}
//add listener to the player object
function playerReady(obj) {
	player = document.getElementById(obj['id']);
	player.addModelListener('STATE','stateHandler'); //listener
}
//listen to the events of the player object
function stateHandler(obj) {
	if (obj.newstate == "PLAYING"){
		playerPlaying = true;
		stopAutoscroll();
	}else if (obj.newstate == "COMPLETED" || obj.newstate == "IDLE"){
		playerPlaying = false;
		currentDiv = "playerImages";
		if (document.getElementById('typeCampaign').value != "zone-client"){
			showPlayerImage(document.getElementById('currentCampaign').value);
			if (document.getElementById('typeCampaign').value != "zone-team"){
				if (defaultPlaying)	startAutoscroll(true);
			}
		}
	}
}

/*--------------------
	ZONE CLIENT
--------------------*/
//confirm delete of audio
function confirmDeleteAudio(texte, id){
	if(confirm(texte)){
		goTo(BASE_URL + "zone-client/audio/delete/"+ id);
	}
}
//confirm switching page
function confirmChangePage(texte, url){
	if(confirm(texte)){
		goTo(url);
	}
}
//pause before team info IMPORTANT for mouseover movements
function pauseClientInfo(team){
	clearTimeout(timerInfo);
	timerInfo = setTimeout("showTeamInfo("+team+")", 100);
}
//loading the team member informaiton
function showClientInfo(exl_id){
	//clearTimeout(timerInfo);
	jQuery("#AJAXinfo").load( BASE_URL + 'ajax/client_info.ajax.php', {'exl' : exl_id});
}
//loading the team member informaiton
function showClientListing(page){
	//clearTimeout(timerInfo);
	jQuery("#AJAXlisting").load( BASE_URL + 'ajax/client_listing.ajax.php', {'page' : page});
}
//side bar clicked on team page
function changeClient(exl_id){
//	stopAutoscroll();
	showClientInfo(exl_id);
	changeVisuals(exl_id, "VID", "exl");
}
//chaneg artist listing
function loadRadioArtists(letter, library){
	loadingText('AJAXartist');
	
	jQuery("#AJAXartist").load( BASE_URL + 'ajax/radio_artists.ajax.php', {'letter' : letter, 'library' : library});
}
//chaneg artist listing
function loadRadioStyles(letter, library){
	loadingText('AJAXstyle');
	
	jQuery("#AJAXstyle").load( BASE_URL + 'ajax/radio_styles.ajax.php', {'letter' : letter, 'library' : library});
}
//radio
function startRadio(library, genre, id){
	var so = new SWFObject(BASE_URL + 'includes/MP3player.swf','mpl','640','400','9');
	so.addParam('allowscriptaccess','always');
	so.addParam('wmode','transparent');
	so.addVariable('MP3playlist',BASE_URL + 'radio/' + library + '/' + genre + '/' + id);
	so.addVariable('searchbar','false');
	so.addVariable('autostart','yes');
	so.write('videoHolder');
	
	anchorScroll('main');
}

function loadingText(elem){
	document.getElementById(elem).innerHTML = "<br><br>&nbsp;&nbsp;"+CONST_loading_txt;
	
}
