$(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'});
	$('#pageFooterConatiner').css({'opacity':.9});
	//$('#ajaxPageText').css({'opacity':.9});

	//ajaxPageText

	
	//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')
			}
		});

	});

	$('#pageFooterConatiner').css({'height':'40px'});

	$('#musicPlayerButton').click(function(event){
		if($('#musicPlayerButton').hasClass('playerIsOpen')){
			$('#pageFooterConatiner').css({'height':'40px'});
			$('#musicPlayerButton').animate({'opacity':.95});
			$('#musicPlayerButton').removeClass('playerIsOpen');
		}
		else{
			$('#pageFooterConatiner').css({'height':'90px'});
			$('#musicPlayerButton').animate({'opacity':.3});
			$('#musicPlayerButton').addClass('playerIsOpen');
		}
	});

	$('#homeButton').click(function(event){
		openPage("home");
	});

	$('#stayhotButton').click(function(event){
		openPage("stayhot");
	});
	
	$('#playcoolButton').click(function(event){
		openPage("playcool");
	});	
	
	$('#buyscarfButton').click(function(event){
		openPage("buyscarf");
	});
	
	$('#gogreenButton').click(function(event){
		openPage("gogreen");
	});
	
	$('#aboutButton').click(function(event){
		openPage("about");
	});
	
	//jump to right page
	currentPageHash = getUrlHashArray();
	
	urlString = window.location.href; 

	
	if(currentPageHash[0]=="stayhot"){
		openPage("stayhot");
	}
	else if(currentPageHash[0]=="playcool"){
		openPage("playcool");
	}
	else if(currentPageHash[0]=="buyscarf"){
		openPage("buyscarf");
	}
	else if(currentPageHash[0]=="gogreen"){
		openPage("gogreen");
	}
	else if(currentPageHash[0]=="about"){
		openPage("about");
	}
	/*
	else if(currentPageHash[0]=="viewpic"){
		setHomeText();
		$('#actualAjaxText').fadeIn('fast');	
		turnOnMusic();	
		//openPage("gogreen");
	}
	*/
	else if((currentPageHash[0]=='' || currentPageHash[0]=='home') && urlString.search('/post/')>0){
		setHomeText();
		$('#actualAjaxText').fadeIn('fast');	
		turnOnMusic();
		
		//split URL by directory to get post id
		urlStringArray = urlString.split('/');
		
		//split postID incase there's a hash at the end
		postIDArray = urlStringArray[4].split('#');
		//alert(urlStringArray[4]);
		//openPage("home");
		
		postID = postIDArray[0];
		getPostDisplay(postID);
	}
	else{
		openPage("home");
	}

});
	
// 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" style="margin-top:150px;">');
}

//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(windowHashLocation){
	orignalUrl = window.location.href
	
	orignalUrlArray = orignalUrl.split('#');

	hashLocation=windowHashLocation.toLowerCase().replace(/ /g,'+');
  	window.location=orignalUrlArray[0]+'#'+hashLocation; 
}


/*================================================//
//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;   
}      




function openPage(type){
	//	$('#ajaxPageText').animate({'width':'0'});
	$('#actualAjaxText').fadeOut("fast",function(){$('#ajaxPageText').animate({'width':'30'});});
	setWindowLocation(type);
    page="";
     
    if(type=="playcool"){
    	page="/lib/app/view/ajaxpage/pages/ajax_playcool_view.php";
    	
    	$('body').css({'background-image':'url("/img/backgroundgraphics/bg2-cold2.jpg")','backgroundColor':'#ececec'});

    	//$('#ajaxContainer').css({'color':'#fff'});
    	$('#mainboxContainer').animate({'backgroundColor':'#cedfFF'},500);
    	
    	//turn off music
		turnOffMusic();
		
		
		//set text
		pageText='<h2>play cool.</h2>'+
  		"<p>Cool is neither the opposite of hot, nor is it a mind set. Cool is chill. It's sexy. It's confident, but not cocky. Cool is open to interpretation. Use with caution, play cool."
  		+'<br> <span style="font-size:10px;">share cool on <a href="http://facebook.com/share.php?u=http://playcoolstayhot.com/#playcool" target="_blank">facebook</a>, <a href="http://twitter.com/home/?status=RT+%40cmarchuska+http://playcoolstayhot.com/#playcool" target="_blank">twitter</a>, and subscribe to updates on <a href="http://www.youtube.com/cmarchuska">youtube</a></span></p>';

  		
  		$('#actualAjaxText').html(pageText);


    }
    else if(type=="stayhot"){
    	page="/lib/app/view/ajaxpage/pages/ajax_stayhot_view.php";
    	
    	$('body').css({'background-image':'url("/img/backgroundgraphics/bg3-hot.jpg")','backgroundColor':'#ef0000'});

    	//$('#ajaxContainer').css({'color':'#fff'});
    	$('#mainboxContainer').animate({'backgroundColor':'#FFcd96'},500);
    	
    	//turn off music
		turnOffMusic();
		
		//set text
		pageText='<h2>stay hot.</h2>'+
  		'<p>Sometimes you’re so hot you need to strip down to heat things up, don\'t be shy, less is more...  <br> <span style="font-size:10px;">share hot on <a href="http://facebook.com/share.php?u=http://playcoolstayhot.com/#stayhot" target="_blank">facebook</a>, <a href="http://twitter.com/home/?status=RT+%40cmarchuska+http://playcoolstayhot.com/#stayhot" target="_blank">twitter</a>, and subscribe to updates on <a href="http://www.youtube.com/cmarchuska">youtube</a></span></p>';

  		
  		$('#actualAjaxText').html(pageText);

  		

    }
    else if(type=="gogreen"){
    	page="/lib/app/view/ajaxpage/pages/ajax_gogreen_view.php";
    	
    	$('body').css({'background-image':'url("/img/backgroundgraphics/bg4-about.jpg")','backgroundColor':'#060'});

    	//$('#ajaxContainer').css({'color':'#fff'});
    	$('#mainboxContainer').animate({'backgroundColor':'#fff'},500);
		
		pageText='<h2>go green & buy local.</h2>'+
  		'<p>The perfect accessory for the stylish & socially conscious individual. Deliciously soft & luxuriously long, this unisex scarf is made from organic cotton or bamboo viscose. Hand dyed & made with care in NYC\'s Garment District. '+
  		'<br>Read more about eco-friendly fashion on our <a href="http://cmarchuska.blogspot.com/" style="font-weight:bold;">blog</a></p>';

  		
  		$('#actualAjaxText').html(pageText);
  		

		
		//turn off music
		turnOffMusic();
    }
    else if(type=="about"){
    	page="/lib/app/view/ajaxpage/pages/ajax_about_view.php";
    	
    	$('body').css({'background-image':'url("/img/backgroundgraphics/bg4-about.jpg")','backgroundColor':'#060'});

    	//$('#ajaxContainer').css({'color':'#fff'});
    	$('#mainboxContainer').animate({'backgroundColor':'#fff'},500);
		
		pageText='<h2>about cmarchuska.</h2>'+
  		'<p>coming soon</p>';

  		
  		$('#actualAjaxText').html(pageText);
  		

		
		//turn on music
		turnOnMusic();
    }
    else{
    	page="/lib/app/view/ajaxpage/pages/ajax_home_view.php";

    	$('body').css({'background-image':'url("/img/backgroundgraphics/bg1-letters.jpg")','backgroundColor':'#D0BE91'});

    	//$('#ajaxContainer').css({'color':'#000'});
    	$('#mainboxContainer').animate({'backgroundColor':'#1C1D1A'},500);
    	
    	pageText='<h2>play cool. stay hot.</h2>'+
    	"<p>It's not just a scarf, it's THE HUNTER SCARF. Crafted from organic cotton or bamboo viscose and made in NYC's Garment District, the hunter scarf can be worn on your head as a wrap or on your waist as belt. It can be worn however you want. What's cool? What's hot? Upload your own pic and let the world decide...</p>";
  	
  		$('#actualAjaxText').html(pageText);	
  		
    	
    	//turn on music
		turnOnMusic();
    }
   

   loadingIconArea('#ajaxPageContainer');   
     
    getAjax(page);

}	

function getAjax(page){
	$.post(page,
  	{
  		postType :"regular"
  	},  	
 	function(data){
 		setAjaxPageData(data);
		
  	},'html');
}

function setAjaxPageData(data){
	$('#ajaxPageContainer').html(data); 		
	$('.rounded').corner('3px');
	$('.bigRounded').corner('5px');		
	$('.topRounded').corner('5px tl').corner('5px tr');
	$('.bottomRounded').corner('5px bl').corner('5px br');
	//		$('#ajaxPageText').animate({'width':'780'},function(){$('#actualAjaxText').fadeIn("fast");});
	setTimeout("$('#ajaxPageText').animate({'width':'780'},function(){$('#actualAjaxText').fadeIn('fast');});",500);

}

function getPostDisplay(postID){
   	page="/lib/app/view/ajaxpage/pages/ajax_home_view.php";

	$.post(page,
  	{
  		postType :"regular",
  		postID : postID
  	},  	
 	function(data){
 		$('#ajaxPageContainer').html(data);
 		
   		
   		$('.rounded').corner('3px');
		$('.bigRounded').corner('5px');		
		$('.topRounded').corner('5px tl').corner('5px tr');
		$('.bottomRounded').corner('5px bl').corner('5px br');
//		$('#ajaxPageText').animate({'width':'780'},function(){$('#actualAjaxText').fadeIn("fast");});
		
  	},'html');
}

function setHomeText(){
	pageText='<h2>play cool. stay hot.</h2>'+
    	"<p>It's not just a scarf, it's THE HUNTER SCARF. Crafted from organic cotton or bamboo viscose and made in NYC's Garment District, the hunter scarf can be worn on your head as a wrap or on your waist as belt. It can be worn however you want. What's cool? What's hot? Upload your own pic and let the world decide...</p>";
  	
  	$('#actualAjaxText').html(pageText);	
}

function turnOffMusic(){
	//turn off music
	$('#musicPlayerFooterContainer').html('<div style="text-align:center; line-height:60px; font-size: 30px">music disabled</div>');
}

function turnOnMusic(){
	if($('#musicPlayerFooterContainer').text()=='music disabled'){
		$('#musicPlayerFooterContainer').html('<iframe id="vkfmPlayer" name="vkfmPlayer" src ="http://vokoder.fm/api/embed/player/?APIVersion=1.3&playerVersion=1&playerStyle=bar&height=80&width=100%&css=default&type=playlist&id=19&showplaylist=false&autostart=true" width="100%" height="80" scrolling="no" frameborder="0">');
	}
}
