$(document).ready(function(){
	// initialize the library with the API key
	FB.init({ apiKey: 'dbe82caa22c6ad9b6f5090da5bd4cc42' });
	//round corners
	$('.rounded').corner('3px');		
	//activate superbox
	$.superbox();
	$('#superbox').corner();
	$('#notificationContainer').css({'opacity':'.95'});
	
	//facebook log in
	$('.loginfacebook').bind('click', function() {
		showNotificationAlert('<img src="/img/general/ajaxloading.gif" > <span class="greenText">connecting with facebook</span>','default');
		
		FB.login(handleFacebookSessionResponse,'offline_access');

//		FB.login(handleFacebookSessionResponse,'email, read_stream, publish_stream, offline_access, status_update, share_item');
	});
		
	//facebook log out
	$('.logoutfacebook').bind('click', function() {
		FB.logout(function(response) {
		  // user is now logged out
		});
		FB.logout(handleFacebookSessionResponse);
	});
	
	//facebook log in
	$('#headerButtonSignout').bind('click', function() {
		FB.logout(function(response) {
		  // user is now logged out
		});
		FB.logout(handleFacebookSessionResponse);
	});
	
	

	$('#mainContentContainer').click(function(event){
		if($(event.target).hasClass('yesVote')){
			ajaxVote($(event.target).attr('rel'),1);
		}
		else if($(event.target).hasClass('noVote')){
			ajaxVote($(event.target).attr('rel'),0);
		}
	});
	
	$('#testFacebookClick').click(function(event){
		FB.getLoginStatus(function(response) {
			alert(response.toSource());
			if(response.session){
				var request = {
				  method: 'users.getInfo',
				  uids: response.session.uid,
				  fields: 'name'
				};
				
				FB.api(request, showFBData);
			
			}
			else{
				showTimedNotificationAlert(5000,'you have to be logged in with facebook','default')
			}
		});

	});


});
	
// handle a session response from any of the auth related calls
function handleFacebookSessionResponse(response) {
	FB.login();

	FB.getLoginStatus(function(response) {
		//alert(response.toSource());
	  	if(response.session){
			facebookLogin(response.session.uid);
		}
		else{			
			hideNotificationAlert();
		}
	});
}



function showFBData(response){
	alert(response.toSource());
}


function facebookLogin(userID){
	$.post("/lib/app/controller/http_post_controller/user_controller.php", 
	{ 
	 doAction : "registerFacebook",
	 facebookUserID : userID
	} ,
	function(returnqueuedata){
	 $('#ajaxReturn').html(returnqueuedata);
	 //$('#postResultArea').html('<div class="width100p errorMessage">Success!</div>');
		},
		"html");//return type and end of post command 

}

function deleteComment(commentID,userID){
	$.post("/lib/app/controller/http_post_controller/comment_controller.php", 
		{ 
		 doAction : "deleteComment",
		 commentID : commentID,
 		 extraUserID : userID
		} ,
		function(returnqueuedata){
		 $('#ajaxReturn').html(returnqueuedata);
		 //$('#postResultArea').html('<div class="width100p errorMessage">Success!</div>');
		},
	"html");//return type and end of post command 

}

//$('body').css('backgroundColor')
function RGBStringToHex(funcInput){
	rgbArray=funcInput.split(',');
	rval=rgbArray[0].replace('rgb(','');
	gval=rgbArray[1];
	bval=rgbArray[2].replace(')','');
	return RGBtoHex(rval,gval,bval);
}

//Functions
function isLoggedIn(){
	return jUserSession.asdfsa123fd14312asf3e23r=='5a4f918dacv3sd5f19';
}

function closeLightBoxWindow(){
		$('#superbox-overlay').triggerHandler('click');  
	}

function showTimedNotificationAlert(time,html,style){
	$('#notificationContainer').html('<div class="siteContainer">'+html+'<span class="cursorPointer redText floatRight boldText" style=" width:20px; padding-left:20px;"  onclick="hideNotificationAlert();">x</span></div>');
	$('#notificationContainer').slideDown('fast');
	
	t = setTimeout('hideNotificationAlert();',time);
}

function showNotificationAlert(html,style){
	$('#notificationContainer').html('<div class="siteContainer">'+html+'<span class="cursorPointer redText floatRight boldText" style=" width:20px; padding-left:20px;"  onclick="hideNotificationAlert();">x</span></div>');
	$('#notificationContainer').slideDown('fast');
}

function hideNotificationAlert(){
	t = setTimeout("$('#notificationContainer').slideUp('fast')",700);

	//$('#notificationContainer').slideUp('fast');
}



function showTimedNotificationFunction(time,html,style,callback){
	$('#notificationContainer').html('<div class="siteContainer">'+html+'<span class="cursorPointer redText floatRight boldText" style=" width:20px; padding-left:20px;"  onclick="hideNotificationAlert();">x</span></div>');
	$('#notificationContainer').slideDown('fast');
	
	t = setTimeout('hideNotificationFunction('+callback+');',time);
}

function showNotificationFunction(html,style,callback){
	$('#notificationContainer').html('<div class="siteContainer">'+html+'<span class="cursorPointer redText floatRight boldText" style=" width:20px; padding-left:20px;"  onclick="hideNotificationAlert();">x</span></div>');
	$('#notificationContainer').slideDown('fast', callback );
}

function hideNotificationFunction(callback){
		t = setTimeout("$('#notificationContainer').slideUp('fast', "+callback+" )",700);

	//$('#notificationContainer').slideUp('fast', callback );
}

function reloadPage(){
	window.location.reload(true);
}

//display error message
function displayErrorStyle(selector,message){
	$(selector).html('<div class="width100p errorMessage">'+message+'</div>');
}

//display success message
function successErrorStyle(selector,message){
	$(selector).html('<div class="width100p successMessage">'+message+'</div>');
}

//reset the text area
function clearAlertArea(selector){
	$(selector).html(' ');
}

function loadingIconArea(selector){
	$(selector).html('<img src="/img/general/ajaxloading.gif">');
}

//email addresses
function checkemail(str){
	//var str=document.validation.emailcheck.value
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str)){
		return true;
	}
	else{
		return false;
	}
}

function setWindowTitle(windowTitle){
  titleString='vokoder fm - '+ windowTitle;
  
  //doc title
  document.title = titleString;
  
  //html dom title
  if(BrowserDetect.browser!="Explorer"){
	$('title').html(titleString);  
  }

}


/*================================================//
//NAME: windowLocation
//This function executes once all of the DOM elements have loaded
//variables,dependencies & why: none
//functions used & why: none
//================================================*/
function setWindowLocation(windowLocation){
  windowLocation=windowLocation.toLowerCase().replace(/ /g,'+');
  window.location=siteVersionURL+'#'+windowLocation; 
}


/*================================================//
//NAME: getUrlHashArray
//This function executes once all of the DOM elements have loaded
//variables,dependencies & why: none
//functions used & why: none
//================================================*/
function getUrlHashArray(){
  //url hash
	var hash = window.location.hash.substr(1);
	var hashArray =hash.split("/");
	
	return hashArray;   
}      