function showProfile() {
	$("#profile_tab").addClass("active_tab");
	$("#points_activity_tab").removeClass("active_tab");
	$("#selected_offers_tab").removeClass("active_tab");
	$("#rated_services_tab").removeClass("active_tab");
	$('#account_tab_section').load('/client/async/account_profile.jsp');
}

function showPointsFromToolbar() {
	$("#profile_tab").removeClass("active_tab");
	$("#points_activity_tab").addClass("active_tab");
	$("#selected_offers_tab").removeClass("active_tab");
	$("#rated_services_tab").removeClass("active_tab");
	$('#account_tab_section').load('/client/async/account_point_activity.jsp');
}

function showPoints() {
	$.ajax({
		   type: "POST",
		   url: "/client/myActivityPoints.htm",
		   dataType:"html",
		   data: "called_from=my_account",
		   success: showPointsCallBack,
		   error:function (xhr, ajaxOptions, thrownError){
			 //alert('debug: ' + xhr.status);
			 //alert('debug: ' + thrownError);
			} 
		   });
	
}

function showPointsCallBack(data) {
	$("#profile_tab").removeClass("active_tab");
	$("#points_activity_tab").addClass("active_tab");
	$("#selected_offers_tab").removeClass("active_tab");
	$("#rated_services_tab").removeClass("active_tab");
	$('#account_tab_section').empty();
	$('#account_tab_section').html(data);
	var date = new Date();
	var month = date.getMonth();
	var months=new Array(12);
	months[0]="January";
	months[1]="February";
	months[2]="March";
	months[3]="April";
	months[4]="May";
	months[5]="June";
	months[6]="July";
	months[7]="August";
	months[8]="September";
	months[9]="October";
	months[10]="November";
	months[11]="December";
	$("#runningMonth").text(months[month]);
}

function showOffers() {
	$.ajax({
		   type: "POST",
		   url: "/client/mySelectedOffers.htm",
		   dataType:"html",
		   data: "",
		   success: showOffersCallBack,
		   error:function (xhr, ajaxOptions, thrownError){
			 //alert('debug: ' + xhr.status);
			 //alert('debug: ' + thrownError);
			} 
		   });
}

function showOffersCallBack(data) {
	$("#profile_tab").removeClass("active_tab");
	$("#points_activity_tab").removeClass("active_tab");
	$("#selected_offers_tab").addClass("active_tab");
	$("#rated_services_tab").removeClass("active_tab");
	$('#account_tab_section').empty();
	$('#account_tab_section').html(data);	
}

function showRatings() {
	$.ajax({
		   type: "POST",
		   url: "/client/myServiceRatings.htm",
		   dataType:"html",
		   data: "",
		   success: showRatingsCallBack,
		   error:function (xhr, ajaxOptions, thrownError){
			 //alert('debug: ' + xhr.status);
			 //alert('debug: ' + thrownError);
			} 
		   });
}

function showRatingsCallBack(data) {
	$("#profile_tab").removeClass("active_tab");
	$("#points_activity_tab").removeClass("active_tab");
	$("#selected_offers_tab").removeClass("active_tab");
	$("#rated_services_tab").addClass("active_tab");
	$('#account_tab_section').empty();
	$('#account_tab_section').html(data);
}

var actionClicked=false;
function onHoverAction(el, actionNum) {
	if (!actionClicked) {
		onClickAction(el,actionNum);
		$(el).bind("mouseout", function (event) {
			$(el).removeClass("point_action_list_hover");
			$(".action_explained").hide();
			$("#explain_action_default").show();
		});
	}
	$(el).bind("click", function (event) {
		onClickAction(el,actionNum);
		$(el).unbind("mouseout");
		actionClicked=true;
		if (actionNum==3) {
			if (clippy != null) {
				clippy.destroy();
			}
			setupClippyCopy();
		}
	});
}

var clippy = null;
function setupClippyCopy() {
	var clip_bitly_link = jQuery("#bitly_share_link_small").html();
	
	if (!FlashDetect.installed) {
		return false;
	}
	
	ZeroClipboard.setMoviePath( 'http://ecosquidtest.virtual.vps-host.net/client/js/ZeroClipboard.swf' );
	clippy = new ZeroClipboard.Client();
	clippy.setText( clip_bitly_link );
	
	clippy.setHandCursor( true );
	clippy.addEventListener('mouseOver', function (client) {
		// copy the text on mouse over
		clippy.setText( clip_bitly_link );
	});
	clippy.addEventListener('mouseOut', function(client) {
		jQuery('#small_tooltip').hide();
	})
	
	clippy.addEventListener('complete', function (client, text) {
		// clipboard will have copied once this event fires
		// alert("Copied text to clipboard: " + text );
	});
	clippy.glue( "click_copy_small" );
}

function onClickAction(el, actionNum) {
	$(".point_action_list").unbind("mouseout");
	$(".point_action_list").removeClass("point_action_list_hover");
	$(el).addClass("point_action_list_hover");
	$(".action_explained").hide();
	$("#explain_action_"+actionNum).show();
}

function hideOffer(id,type){
	var r=confirm("Are you sure you want to delete this item?");
	if (r==true){
		hideOfferAjaxCall(id,type);
	}
}

function hideOfferAjaxCall(id,type){
	var dataStr = "id=" + id + "&type=" + type;
	

	$.ajax({
		   type: "POST",
		   url: "hideOffer.htm",
		   dataType:"json",
		   data: dataStr,
		   success: hideOfferCallBack,
		   error:function (xhr, ajaxOptions, thrownError){
		 	//alert('debug: ' + xhr.status);
			//alert('debug: ' + thrownError);
		} 
	   });
}

function hideOfferCallBack(data){
	var id = null;
	if(data.status == 'success')
	{
		if(data.type=='offer'){
			id = "historic_item_offer_" + data.id;
			$("#" + id).hide();
		}
		else{
			id = "historic_item_rating_" + data.id;
			$("#" + id).hide();
		}
	}
	else
	{
		if(data.type=='offer'){
			id = "deletion_error_for_offer_" + data.id;
			$("#" + id).html("Oops.. EcoSquid is experiencing some technical problems, please try again later");
		}
		else{
			id = "deletion_error_for_rating_" + data.id;
			$("#" + id).html("Oops.. EcoSquid is experiencing some technical problems, please try again later");
		}
	}
}

function cropImage() {
	$.ajax({
		   type: "POST",
		   url: "/client/cropped.htm",
		   dataType:"json",
		   data: "x1=" + $("#x1").val() + "&y1=" + $("#y1").val() + "&h=" + $("#h").val() + "&w=" + $("#w").val(),
		   success: cropImageCallBack,
		   error:function (xhr, ajaxOptions, thrownError){
				//alert('debug: ' + xhr.status);
				//alert('debug: ' + thrownError);
		}
	});
}

function cropImageErrorCallBack() {
	$("#upload_file_error_message").show();
}

function cropImageCallBack(data) {
	if (data.status != 'failure') {
		$(".preview").empty();
		var userId = data.status;
		$("#save_cropped_image_btn").show();
		$("#cancel_btn").show();
		var str ='<div>' +
					'<img src="images/user_profiles/'+ userId + '_temp_cropped.jpg" width="90px" height="90px;" alt=""/>' +
				 '</div>';
		$(".preview").html(str);
	}
	else{
	}
}

function saveImage() {
	$.ajax({
		   type: "POST",
		   url: "/client/saveImage.htm",
		   dataType:"html",
		   data: "choose_file" + $("#choose_file").val(),
		   success: saveImageCallBack,
		   error:function (xhr, ajaxOptions, thrownError){
				//alert('debug: ' + xhr.status);
				//alert('debug: ' + thrownError);
		}
	});
}

function saveImageErrorCallBack() {
	$("#upload_file_error_message").show();
}

function saveImageCallBack(data) {
	if (data.status == 'success') {
		$("#upload_file_error_message").hide();
		window.location.href = "/client/crop.jsp";
	}
	else{
		$("#upload_file_error_message").show();
	}
}

function updateSubscriptions() {
	
	var dataStr = "&newsletter=" + $("input[name='newsletter']").attr("checked");
	dataStr += "&points_emails=" + $("input[name='points_emails']").attr("checked");
	dataStr += "&rating_reminder_emails=" + $("input[name='rating_reminder_emails']").attr("checked");
	
	$.ajax({
		   type: "POST",
		   url: "/client/changeSubscriptions.htm",
		   dataType:"json",
		   data: dataStr,
		   success: updateSubscriptionsCallback,
		   error:function (xhr, ajaxOptions, thrownError){
				//alert('debug: ' + xhr.status);
				//alert('debug: ' + thrownError);
		}
	});
}

function updateSubscriptionsCallback(data) {
	if (data.status == 'success') {
		$("#subscriptions_form_block").empty();
		$("#change_subscriptions_success_message").show();
		$("#change_subscriptions_error_message").hide();
	}
	else{
		$("#change_subscriptions_error_message").show();
	}
}
