jQuery(document).ready( function ($) {

	var labels=new Array('Poor','Poor','Ok','Ok','Good','Good','Great','Great','Awesome','Awesome!');
	//var oriMsg = 'Rate to see the next photo!';
	var oriMsg = 'Click on the stars to rate!';
	
	$('li.hotornot-button').hover(
		function () {
			$(this).addClass('button-hover');
			$('li.hotornot-button:lt(' + this.id + ')').addClass('button-hover');
			//var i = $('.stars .hotornot-button').index($(this));
			var i = this.id;
			$('#cur-rating .rating').html(labels[i-1]);
		},
		function () {
			$(this).removeClass('button-hover');
			$('li.hotornot-button:lt(' + this.id + ')').removeClass('button-hover');
			$('#cur-rating .rating').html(oriMsg);
		}
	);
	
	$('li.hotornot-button').click( function () {
		
		var vote = this.id;
		$('input#hotornot-vote').val(vote);
		
		//Original way: submit form
		//$('form#hotornot-form').submit();
		
		//New way: ajax post
		var voteID = $('form#hotornot-form input#hotornot-id').val();
		//var voteTask = $('form#hotornot-form input#hotornot-task').val();
		var voteTask = 'voteajax';
		var voteItemid = $('form#hotornot-form input#hotornot-Itemid').val();
		var voteData = {vote:vote, id:voteID, task:voteTask, Itemid:voteItemid};
		//console.log(voteData);
		$('#rating-group').fadeOut(function(){
			$('#rating-submitting').fadeIn();

			$.post('index.php?option=com_hotornot', voteData, function(data) {
				//might want to do something with data
				//$('.result').html(data);
				//alert(data);
				$('#rating-submitting').fadeOut(function(){
					$('#rating-submitted').fadeIn();
					//alert('Share!');
					if (vote > 7){
						//Share if rating more than 7
						try{
							shareThisTurbanWithRating(vote, gotoNext);
						}catch(err){
							showFacebook();
						}
					}else{
						gotoNext();
					}
				});
			});
		});
		
		
		
	});
	
	function gotoNext(){
		//$('#btn-next').click();
		//$('#btn-next').click(function(){return true;}).click();
		window.location.href = $('#btn-next').attr('href');
	}
	
	$('#share-link').click( function () {
		$(this).select();
	});

	$('.hotornot-report').click( function () {
		$(this).parent().load($(this).attr('href'));
		$(this).parent().html($('#hotornot-loading').html());
		return false;
	});

	$('.hotornot-delete, #del-button').click( function () {
		if (confirm('Are you sure you want to delete this image?'))
		{
			$(this).parent().load($(this).attr('href'));
			$(this).parent().html($('#hotornot-loading').html());
		}
		return false;
	});

	/*
	$('#hotornot-comments .update').load('index.php?option=com_hotornot&task=comments&format=raw&id=' + $('#hotornot-comments').attr('class'));

	$('#show-comment-form').click( function () {
		$('#comment-form').show("normal");
		return false;
	});
	*/

	
	// ############################	 Fav functions	 ############################
	// Added by Ryan

	$('#fav-button2').click( function () {
		var disp = $('#favid').css('display');
		if (disp == 'none'){
			$('#favid .update').html("You must log in first!");
			$('#favid').slideDown('slow');
		}else{
			$('#favid .update').fadeOut('normal',function(){
				$('#favid .update').html("You must log in first!");
				$('#favid .update').fadeIn('normal');
			});
		}
			return false;
	});
	
	$('#fav-button').click( function () {

	var disp = $('#favid').css('display');
	if (disp == 'none'){
		$('#favid .update').hide();
		$('#favid .loading').show();
		$('#favid').slideDown('slow',toggleFav);
	}else{
		$('#favid .update').fadeOut('normal',function(){
			$('#favid .loading').fadeIn('normal',toggleFav);
		});
	}
		return false;
	});
	
	function toggleFav(){
		if ($('#fav-label').html() == 'Un-Favorite'){
			$.post('index.php?option=com_hotornot&format=raw&task=remfav', { id: $('#favid').attr('class') },
				function(data) {
					//update UI to show this image has been unfavorited
				$('#favid .loading').fadeOut('fast',
				
				function (){
					$('#fav-button').attr('class','fav');
				$('#fav-label').html('Favorite');
				$('#favid .update').fadeOut('normal',function(){
					$('#favid .update').html(data);
					$('#favid .update').fadeIn('slow');
				});
				});
			});
		}else{
			$.post('index.php?option=com_hotornot&format=raw&task=addfav', { id: $('#favid').attr('class') },
				function(data) {
					//update UI to show this image has been favorited
					$('#favid .loading').fadeOut('fast',
						function (){
							$('#fav-button').attr('class','unfav');
							$('#fav-label').html('Un-Favorite');
							$('#favid .update').fadeOut('normal',function(){
								$('#favid .update').html(data);
								$('#favid .update').fadeIn('slow');
							});
						}
					);
					shareThisTurban('Check this turban out!', function(){});
					//shareTurban2();
				}
			);
		}
	}

	/*
	// ############################	 Tab Box functions	 ############################
	// Added by Ryan

	$(".tabs li:eq(0) a").css("border-left", "none");
	
	$(".tabbed-box .tabs li").filter(":lt(4)").click(function() {
		var tab_name = $(this).attr('class');
		//alert('tab_name='+tab_name);

		$(".tabbed-box .tabs li a").removeClass("active");
		$(".tabbed-box .tabs li." + tab_name + " a").addClass("active");
		$(".tabbed-box .tabbed-content").hide();
		$(".tabbed-box #" + tab_name).show();

		return false;
	});
	
	$(".tabbed-box .tabs li").filter(":first").click()
	
	// #########	Hack fixes	############
	
	$('#specialcommentbox').parent().css('height','auto');
	 */
	
	
});

function shareThisTurbanWithRating(rating,callback){
	var msg = 'I give this Turban '+rating+'/10, what do you think?';
	shareThisTurban(msg,callback);
}

function shareThisTurban(msg, callback){
	var img_src = jQuery('#photo IMG').attr('src');
	var photoOwner = jQuery('input#owner').val();
	var caption = photoOwner + '\'s turban';
	var link = window.location.href;
	shareTurban(msg, caption, img_src, link, callback);
}

// TODO: call this function if high rating is given!
function shareTurban(msg, caption, img_src, link, callback){
	//alert('img_src='+img_src+', img_link='+img_link);	

	var streamObject = {
			method: 'stream.publish',
			message: msg,
			attachment: {
				name: caption,
				caption: 'Rate My Turban - Bringing you turbans of the world.',
				description: (
					'Explore the world of turban wearers with Rate my turban.'
				),
				href: link,
				media: [
					{
						type: 'image', 
						src: img_src, 
						href: link
					}
				]
			},
			action_links: [
				{ text: 'Rate Turbans Now', href: JS_URL_ROOT }
			],
			user_message_prompt: 'Tell your friends about this turban'
		};
	
	try{
		FB.ui(streamObject, callback);
	}catch(err){
		callback(null);
	}
	
	/*
	function callback(response) {
		if (response && response.post_id) {
			//alert('Post was published.');
		} else {
			//alert('Post was not published.');
		}
		
	}*/
	

}

/* Just for testing
function shareTurban2(){
	var share = {
	   method: 'stream.share',
	   u: window.location.href
	 };

	FB.login(function(response){
		if (response){
			FB.ui(share, function(response) { console.log(response); });
		}else{
			alert('Oops!');
		}
	});
	 
}*/

