var errors_in_transactions = '';
var confirmleave = false;
var must_pay_content = [];
var current_object_global = [];
var gts_post_id = ''; //before gts we save the post this is the post id result

function toggle_post(post_id,hashed) {
	var current_post_handled = post_id;
	if ($('#post_'+post_id).hasClass('close') ) {
		$('#post_' + current_post_handled).show();
		$('#post_' + current_post_handled+ ' .details').show('slide',{direction : 'up'},'slow',function(){
			if (hashed) {
				location.hash = '#post_' + post_id +  '_position';
			}
			$('#post_'+post_id).removeClass('close');
		});
	} else {
		$('#post_'+post_id+ ' .details').hide('slide',{direction : 'up'},'slow',function(){
			$('#post_'+post_id).addClass('close');
			$('#post_' + current_post_handled).hide();
		});
	}
}
function widget(object, callback, sms) {
	var widget_background = '<div id="widget_background"></div>';
	var widget_box   = 	'<div id="widget">';
	widget_box		+=		'<center>';
	widget_box		+= 			'<div class="widget_outer_transparent_container"></div>';
	widget_box		+=			'<div class="widget_outer_container">';
	widget_box  	+=		 		'<div class="widget_close">&#10006;</div>';
	//widget_box  	+= 				'<div class="widget_container_top"><div class="drag_handle"></div></div>';
	widget_box  	+= 				'<div class="widget_container">';
	widget_box  	+= 					'<div class="widget_inner_container">';
	if (object.title) {
		widget_box	+= 						'<div class="title">'+object.title+'</div>';
	}
	widget_box		+=						'<div class="widget_content">'+object.content+'</div>';
	if (sms && callback) {
		widget_box		+=					'<center><div class="field_container" style="padding-top:15px;_height:45px"><div class="field" style="float:none"><input type="text" id="widget_mobile" value=""></div></div></center>';
	//	widget_box		+=					'<center><div class="widget_continue" onmouseover="$(this).addClass(\'hover\')" onmouseout="$(this).removeClass(\'hover\')">'+strings.send_sms+' &rsaquo;</div></center>';
	} else if (callback) {
	//	widget_box		+=					'<center><div class="widget_continue" onmouseover="$(this).addClass(\'hover\')" onmouseout="$(this).removeClass(\'hover\')">'+strings.go_on+' &rsaquo;</div></center>';
	}
	widget_box  	+= 					'</div>';
	widget_box  	+= 				'</div>';
	//widget_box  	+= 				'<div class="widget_container_bottom"></div>';
	widget_box		+=			'</div>';
	widget_box		+=		'</center>';
	widget_box  	+= 	'</div>';
	
	$('body').append(widget_background);
	$('body').append(widget_box);
	
	$('#widget_background').height($(document).height());
	$('#widget').height($(document).height());
	$('#widget .widget_outer_transparent_container').height($('#widget .widget_outer_container').height());
	var widget_position_top = (($(window).height()/2) - $('#widget .widget_outer_container').height()/2) + $(window).scrollTop();
	$('#widget .widget_outer_transparent_container').css({top:widget_position_top});
	$('#widget .widget_outer_container').css({top:widget_position_top});
	//$('#widget .widget_outer_container').draggable({handle:'.drag_handle'});
	$('#widget').disableSelection();
	
	$('#widget_background').css('opacity',0.5); /* If this opacity is not set, then in IE the fade-in starts from 100% and the goes transparent */
	$('#widget').hide();
	
	// The following 2 lines are required in order to complete the CSS3PIE IE hack (bug fix) in lines 57-58 below
	$('#widget .field_container').hide();
	$('#widget .widget_continue').hide();
	
	//$('#widget_background').show('slide',{'direction':'left'},500,function() {
	$('#widget_background').fadeIn('fast',function() {
		//$('#widget').show('slide',{'direction':'right'},500);
		$('#widget').fadeIn('fast', function() {
			// IE CSS3PIE bug fix
			$('#widget .field_container').slideDown();
			$('#widget .widget_continue').slideDown();
		});
	});
	
	//var callback_function_string = '';
	if (sms) {
		$('#widget .widget_continue').bind('click', callback);
		$('#widget .widget_close').bind('click', function() {
			widget_close();
		});
	} else if (callback) {
		//callback_function_string = callback.toString().replace(/"/g,'\'');
		$('#widget .widget_continue').bind('click', callback);
		$('#widget .widget_close').bind('click', callback);
	} else {
		$('#widget .widget_close').bind('click', function() {
			widget_close();
		});
	}
	
}
function widget_close() {
	//$('#widget').hide('slide',{'direction':'left'},500,function() {
	$('#widget').fadeOut('fast',function() {
		//$('#widget_background').hide('slide',{'direction':'right'},500,function() {
		$('#widget_background').fadeOut('fast',function() {
			$('#widget_background').remove();
			$('#widget').remove();
			submission_in_progress = 0;
		});
	});
}

// submit form.post-edit
function form_submit() {
	if (validate(document.getElementById('form__post_edit'))) {
    	$('#form__post_edit').submit();
	}
}	
var post_id_to_delete = '';
function add_marked_field_and_submit() {
	
	 //*	we save the post on special_submit() and here we only referring to the my-account page with operation succesful 
	 
	$('#form__post_edit').append('<input type="hidden" name="param__marked" value="1">');
	$('#form__post_edit').append('<input type="hidden" name="param__notify_user" value="0">');
	var date = new Date();
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
//	date.setMonth(date.getMonth()+1);
	var m = date.getMonth() + 2;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	var max_date = new Date(yy, m, 0);
	if (d >  max_date.getDate()) {
		day =  max_date.getDate();
	}
	$('#form__post_edit').append('<input type="hidden" name="param__end_date" value="'+year+month+day+'">');
	$('#form__post_edit').submit();
	
}

// post_type  = false || must_pay
function mark_and_save_post_before_submit(payment_type){
	//first we  set the date ,after the payment recived , we add the marked field
	/* end testing*/
//	$('#form__post_edit').append('<input type="hidden" name="param__marked" value="1">');
	$('#form__post_edit').append('<input type="hidden" name="param__notify_user" value="0">');
	
	var date = new Date();
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
//	date.setMonth(date.getMonth()+1);
	var m = date.getMonth() + 2;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	var max_date = new Date(yy, m, 0);
	if (d >  max_date.getDate()) {
		day =  max_date.getDate();
	}
	$('#form__post_edit').append('<input type="hidden" name="param__end_date" value="'+year+month+day+'">');
	
	$('#form__post_edit').append('<input type="hidden" name="param__ajax_submit" value="1">');
	if (payment_type == 'long-term') {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_marking_must_pay });
	} else if(payment_type == 'long-term-existing') {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_marking_must_pay_on_existing_post });
	} else {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_marking_normal_object });
	}
}



//payment - type = normal or long-term like the param in the object
// handle new post only 
function save_post_before_submit(payment_type) {
	$('#form__post_edit').append('<input type="hidden" name="param__ajax_submit" value="1">');
	if (payment_type == 'long-term') {
	//	$('#form__post_edit').append('<input type="hidden" name="param__marked" value="1">');
		$('#form__post_edit').append('<input type="hidden" name="param__notify_user" value="0">');
		var date = new Date();
		var d  = date.getDate();
		var day = (d < 10) ? '0' + d : d;
	//	date.setMonth(date.getMonth()+1);
		var m = date.getMonth() + 2;
		var month = (m < 10) ? '0' + m : m;
		var yy = date.getYear();
		var year = (yy < 1000) ? yy + 1900 : yy;
		var max_date = new Date(yy, m, 0);
		if (d >  max_date.getDate()) {
			day =  max_date.getDate();
		}
		if (m > 12 ) {
			m = 1;
			month = (m < 10) ? '0' + m : m;
			yy++;
			year = (yy < 1000) ? yy + 1900 : yy;
		}
		$('#form__post_edit').append('<input type="hidden" name="param__end_date" value="'+year+month+day+'">');
		$('#form__post_edit').append('<input type="hidden" name="param__ajax_submit" value="1">');
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_marking_must_pay});
	} else {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_normal_must_pay});	
	}
}

// normal payment on groups that posting cost money
function set_gts_widget_with_normal_must_pay(data) {
	gts_post_id = data;
	gts('he' ,must_pay_content.params.payment,must_pay_content.invoice_recipient,function() { confirmleave = false; window.location=https_url+'my-account?saved='+gts_post_id});
}

//payment for marking post in a must-pay group
function set_gts_widget_with_marking_must_pay(data) {
	gts_post_id = data;
	gts('he' ,must_pay_content.params.long_term_payment,must_pay_content.invoice_recipient,function() { confirmleave = false; window.location=https_url+'my-account?saved='+gts_post_id});
}

//payment for marking post in a must-pay group
function set_gts_widget_with_marking_must_pay_on_existing_post(data) {
	gts_post_id = data;
	gts('he' ,must_pay_content.params.amount ,current_object_global.invoice_recipient,function() { confirmleave = false; window.location=https_url+'my-account?saved='+gts_post_id});
}

//payment for marking a post in a normal group (group that posing is free )
function set_gts_widget_with_marking_normal_object(data) {
	gts_post_id = data;
	gts('he' ,current_object_global.price,current_object_global.invoice_recipient,function() { confirmleave = false; window.location=https_url+'my-account?saved='+gts_post_id});
}
/*
 open a widget with available post option - must pay || mark;
 define by $('#row_2 #payment_only').val().
 * */
function mark_post_ads(current_object,debug) {
	current_object_global = current_object;
	var widget_object = [];
	var html_content = '';
	//alert(validate(document.getElementById('form__post_edit')));
	if (validate(document.getElementById('form__post_edit'))) {
		//special_submit();
		if ($('#row_2 #payment_only').val() && $('#submit_button').text() == strings.post ) {
			html_content +=	must_pay_content.content;
			html_content += '<div class="top_button_container">';
			html_content += ' 	<div class="button__regular" onclick="widget_close();save_post_before_submit(\'normal\');"></div>';
			html_content += '</div>';
			html_content += '<div class="bottom_button_container">';
			html_content += ' 	<div class="button__marked" onclick="widget_close();save_post_before_submit(\'long-term\');"></div>';
			html_content += '</div>';
			must_pay_content.content = html_content;
			widget(must_pay_content);
		} else if ($('#row_2 #payment_only').val() && $('#submit_button').text() != strings.post){
			html_content += '<div>';
			html_content +=	 must_pay_content.params.pg_marking_text;
			html_content += '<center><div class="buttons" align="center">';
            html_content += '   <div class="button__marked" onclick="widget_close(); save_post_before_submit(\'long-term\')"></div>';
            html_content += '   <a class="button__unmark" onclick="$(\'#form__post_edit\').submit()">'+strings.only_post_post+'</a>';
            html_content += '</div></center>';
			must_pay_content.content = html_content;
			widget(must_pay_content);	
		} else {
            html_content += '<div>';
            html_content += current_object.content;
            html_content += '<center><div class="buttons" align="center">';
            html_content += '   <div class="button__marked" onclick="widget_close(); mark_and_save_post_before_submit()"></div>';
            html_content += '   <a class="button__unmark" onclick="$(\'#form__post_edit\').submit()">'+strings.only_post_post+'</a>';
            html_content += '</div></center>';
            current_object.content = html_content;
            widget(current_object); 
        }
	}
}
function mark_post_button(current_object,post,current_date){
	current_object_global = current_object;
	var widget_object = [];
	var html_content = '';
	     if ($('#row_2 #payment_only').val() ) {
	        html_content += '<div>';
            html_content +=  must_pay_content.params.pg_marking_text;
            html_content += '<center><div class="buttons" align="center">';
            html_content += '   <div class="button__marked" onclick="widget_close(); mark_and_save_post_before_submit(\'long-term-existing\');"></div>';
            html_content += '   <a class="button__unmark" onclick="$(\'#form__post_edit\').submit()">'+strings.only_post_post+'</a>';
            html_content += '</div></center>';
            must_pay_content.content = html_content;
            widget(must_pay_content);
	     } else {
	        html_content += '<div>';
            html_content += current_object.content;
            html_content += '<center><div class="buttons" align="center">';
            html_content += '   <div class="button__marked" onclick="widget_close(); mark_and_save_post_before_submit(\'normal\');"></div>';
            html_content += '   <a class="button__unmark" onclick="widget_close();jump(\''+post+'\',\''+current_date+'\')">'+strings.just_jump+'</a>';
            html_content += '</div></center>';
            current_object.content = html_content;
            widget(current_object);
        }
		return false;
	
}
function on_delete(post_id,description,user_id) {
	post_id_to_delete = post_id;
	var current_object = [];
	var html_content = '';
	html_content += '<div class="content_container '+direction+'" dir="'+direction+'">';
	html_content += '	<div class="description '+direction+'">'+description.content+'</div>';
	html_content += '	<form id="delete_reason_form">';
	html_content += '		<input type="hidden" name="action" value="send_delete_reason">';
	html_content += '		<input type="hidden" name="user_id" class="user_id" value="'+user_id+'">';
	html_content += '		<div class="input_title">'+strings.my_reason+'</div>';
	html_content += '		<div class="input_field">';
	html_content += '			<select name="reason">';
	html_content += '				<option value="'+strings.found+'">'+strings.found+'</div>';
	html_content += '				<option value="'+strings.lost_hope+'">'+strings.lost_hope+'</div>';
	html_content += '				<option value="'+strings.other+'">'+strings.other+'</div>';
	html_content += '			</select>';
	html_content += '		</div>';
	html_content += '		<div class="input_title">'+strings.comment+'</div>';
	html_content += '		<div class="input_field">';
	html_content += '			<textarea name="comment"></textarea>';
	html_content += '		</div>';
	html_content += '	</form>';
	html_content += '	<div class="buttons '+direction+'">'
	/*html_content += '		<div class="button__blue" onclick="delete_post(\''+post_id+'\');widget_close()">'+strings.delete_post_without_send+'</div>';*/
	html_content += '		<div class="button__blue" onclick="send_delete_reason(\'delete_reason_form\',$(this))">'+strings.send_and_delete+'</div>';
	html_content += '	</div>';
	html_content += '</div>';
	current_object.content = html_content;
	widget(current_object,function(){
		delete_post(post_id_to_delete);
	},false) ;
}
function send_delete_reason(form_id,button_object){
	button_object.css({backgroundImage : 'url(/images/global/loader.gif);'});
	var params = [];
	params = $('#'+form_id).serializeArray();
	$.get(href_url,params,function(data){
		if ($('.user_id').val() != '505') {
			widget_close();
			delete_post(post_id_to_delete);
		}
		
	},'json');
}
function delete_user_images(href_url,user_id){
	$.get(href,{ 'action' : 'delete_user_images','user_id' : user_id},function(data){
		if (data == 'success'){
			$('#delete_succsess').show();
		}
	},'json');
	
}
function makeHomePage(element) {
			if ( (document.all) && (element) )	{
				element.style.behavior='url(#default#homepage)';
				element.setHomePage('http://www.shutaf.com');
			} else {
				alert('הגלשן בו הינכם משתמשים אינו מאפשר לעדכן את עמוד הבית שלכם באופן אוטומטי מתוך האתר. השתמשו בתפריטי הגלשן בכדי לעדכן את האתר כעמוד הבית');
			}
		}

// sidebar
var href = '';
var https_url = '';
var clicked_posts = [];
function sub_handler(parent_id) {
//	var current_group_shown = $('#current_group_shown').val();
	$('#sub_container__'+current_group_shown).hide();
	$('#sub_container__'+parent_id).show();
	$('#param__parent_id').val(parent_id);
	$('#current_group_shown').val(parent_id);
	$('#search').attr('action',$('#search').attr('action')+parent_id);
}

function display_image(showcase_id) {

	$(showcase_id + ' .youtube').hide();
	$(showcase_id + ' .image').show();
}
function display_youtube(showcase_id,youtube_id) {
	var youtube_code = youtube_id;
	display_youtube.code = youtube_code;
	$('#showcase_container__'+showcase_id + ' .image').hide();
	$('#showcase_container__'+showcase_id + ' .youtube').show();
	
}
function sidebar_param_handler(group_id){
	if (group_id == 'search') {
		$('#level_2').html('<div class="text">&nbsp;</div>');
	} else {
		$('#level_2').html("&nbsp;");
		$('#loader').show();
	 	$.get(href,{'action' : 'get_post_param', 'group_id' : group_id , 'template' : 'sidebar' } ,function(data){
	     	$('#level_2').html(data);
	     	$('#loader').hide();
		},'html');
	}
	
 }
function submit_form_sidebar(form_id) {
	$(form_id).attr('action',href + $('#level_1').val());
	return true;
}
//seach
// not_first_display 
function send_email(post_id,user_email,confirmation) {
	var current_form = '#form_container_'+post_id;
	
	if (user_email == 'spam') {
		if (!$(current_form).hasClass('not_first_display')) { 
			$(current_form).html($('#form_handler_spam .form_container').html());
			$(current_form).addClass('not_first_display');
		}	
		$(current_form + ' textarea').val("מודעה "+post_id+"היא \n");
		$('#to_email').val(" ");
	} else {
		if (confirmation == 1) { 
			if (!$(current_form).hasClass('not_first_display')) { 
				$(current_form).html($('#form_handler .form_container_succsess').html());
				$(current_form).addClass('not_first_display');
			}
		} else { 
			if (!$(current_form).hasClass('not_first_display')) { 
				$(current_form).html($('#form_handler .form_container').html());
				$(current_form).addClass('not_first_display');
			}
		}
		$(current_form + ' textarea').val();
		$('#to_email').val(user_email);
	}
	$(current_form).toggle();
	$('#post_id').val(post_id);	
}
function send_email_new(post_id,user_email,confirmation,user_id,referrer) {
	var current_form = '#form_container_'+post_id;
	
	if (user_email == 'spam') {
		if (!$(current_form).hasClass('not_first_display')) { 
			$(current_form).html($('#form_handler_spam .form_container').html());
			$(current_form).addClass('not_first_display');
		}	
		$(current_form + ' textarea').val("מודעה "+post_id+"היא \n");
		$('#to_email').val(" ");
	} else {
		if (confirmation == 1) { 
			if (!$(current_form).hasClass('not_first_display')) { 
				$(current_form).html($('#form_handler .form_container_succsess').html());
				$(current_form).addClass('not_first_display');
			}
		} else { 
			if (!$(current_form).hasClass('not_first_display')) {
				if (!user_id) {
					var object = [];
					var content = '';
					content += '<div class="widget_container">';
					content += '	<div class="description">' + not_register_object.content + '</div>';
					content += '	<center>';
					content += '		<table cellpadding="0" cellspacing="0">';
					content += '			<tr>';
					content += '				<td>';
					content += '					<div class="button__green_120" style="margin-bottom:10px;" onclick="window.location=\'' + href_url + 'login\?referrer=' + referrer + '\'">להרשמה</div>';
					content += '				</td>';
					content += '				<td><div class="separator">&nbsp;</div></td>';
					content += '				<td>';
					content += '					<div class="button__gray" style="margin-bottom:10px;" onclick="widget_close();" >' + strings.close + '</div>';
					content += '                </td>';
					content += '            </tr>';
					content += '		</table>';
					content += '	</center>';
					content += '</div>';
					object.content = content;
					widget(object, false, false);
				} else {
					$(current_form).html($('#form_handler .form_container').html());
					$(current_form).addClass('not_first_display');
				}
			}
		}
		$(current_form + ' textarea').val(" ");
		$('#to_email').val(user_email);
	}
	$(current_form).toggle();
	$('#post_id').val(post_id);	
}

function post_form_submit(post_id,to_email) {
	$('#form_container_'+post_id+ ' .to_email').val(to_email);
	$('#form_container_'+post_id+ ' .post_id').val(post_id);
	$('#form_container_'+post_id+ ' form').submit();
	
}
//posts
function delete_post(post_id) {
	$.post(https_url+'/my-account/post-edit', {'action':'delete','post':post_id,'json' : 1},'json');
	window.location= href+'/my-account?delete_success=1';
}
function post_sort_by_group(group_id,doc_type,user_id){
	$('#post_container').hide();
	$('#post_container').html("&nbsp;");
	$('#search_loader').show();
	
 	$.get(href,{'action' : 'get_post_param', 'group_id' : group_id , 'template' : 'user' ,'doc_type': doc_type,'user_id':user_id} ,
 		function(data){
	     	$('#post_container').html(data);
	     	$('#search_loader').hide();
 		},'html');
 }
function show_group_post(group_id,doc_type,user_id){
	$('#post_container').hide();
	$('.s_title').removeClass('selected');
	$('#'+group_id+ ' .s_title').addClass('selected');
		$.get(href,{'action' : 'get_post_param', 'group_id' : group_id , 'template' : 'user' ,'doc_type': doc_type,'user_id':user_id} ,
 		function(data){
	     	$('#post_container').html(data);
			$('#post_container').show();
 		}
	,'html');
	
}
function jump_old(post,timestamp) {
	$.get(href,{'action' : 'update-timestamp','post' : post,'timestamp' : timestamp},
		function (data) {
			if (data == 'success') {
				$('.notes').toggle('slow');
			} else {
				$('#client_side_errors').toggle('slow');
				$('#client_side_errors .jump').show();
			}
		},'html');
		
}
function jump(post,timestamp) {
	$.get(href,{'action' : 'update-timestamp-new','post' : post,'timestamp' : timestamp},
		function (data) {
			if (data == 'success') {
				$('.notes').show('slide',{direction : 'up'},'fast');
			} else {
				$('#client_side_errors .jump_to_soon').show();
				$('#client_side_errors').show('slide',{direction : 'up'},'fast');
			}
		},'json');
		
}
function mark_success(post){
	$.get(href,{'action' : 'mark-post','post' : post},
		function (data) {
			if (data == 'success') {
				$('.notes .mark_success').show();
		//		$('.notes').show('slide',{direction : 'up'},'fast');
			} else {
				$('#client_side_errors .jump_to_soon').show();
				$('#client_side_errors').show('slide',{direction : 'up'},'fast');
			}
		},'json');
}
function add_click(post) {
	if (jQuery.inArray(post,clicked_posts) < 0) {
		$.get(href,{'action' : 'update-clicks','post' : post },	function (data) {},'html');
	}
}
var id ;
var email;
function fb_login(referrer, skip_flash, lottery) {
	$('.loader').show('slide', {direction:'left'});
	FB.login(function(response) {
		fb_uid = response.session.uid;
		fb_access_token = response.session.access_token;
		if (response.session.access_token) {
			// Once the user signed-into FB, we sign him into Gyro
			$.get(href_url+'login', {authority:'facebook', fb_access_token:fb_access_token, json:1}, function(data) {
				if (!data.errors && data.email) {
					window.location = referrer ? referrer : href_url;	
				} else {
					//if user is not registered we register it automaticly and reffering 
					FB.api('/me', function(response) {
						id = response.id;

						email = response.email;
						$.get(href_url,{
							 'action' 		: 'register_user_from_facebook' ,
							 'first_name' 	: response.first_name ,
							 'last_name' 	: response.last_name ,
							 'date_of_birth': response.birthday ,
							 'gender' 		: response.gender ,
							 'email' 		: response.email ,
							 'id' 			: response.id
							  },function(data){
							  	if (data.status == 'success') {
							        FB.api('/me/feed', 'post', { message: data.body ,link : http_url,picture : http_url+'images/global/empty.jpg' }, function(response) {
							            if (!response || response.error) {
							                window.location = referrer ? referrer : href_url;
							            } else {
											window.location = referrer ? referrer : href_url;
										}
							       });
								} else {
									window.location = referrer ? referrer : href_url;
								}
						},'json');
					});
				}
			},'json');
	  	} else {
	    	$('.superdoc.login .loader').hide('slide', {direction:align});
	  	}
	}, {perms:'email,user_birthday,user_location,user_interests,publish_stream'});
} 
//var fb_object = [];
function fb_register(referrer) {
	$('.loader').show('slide', {direction: 'left'});
	FB.login(function(response) {
		//console.log(response.session);
		fb_uid = response.session.uid;
		fb_access_token = response.session.access_token;
		if (response.session.access_token) {
			// Once the user signed-into FB, we sign him into Gyro
			$.get(href_url + 'login', {authority: 'facebook', fb_access_token: fb_access_token, json:1}, function(data) {
				if (!data.errors && data.email) {
					$('.loader').hide('slide', {direction:'left'}, 'fast', function(data) {
						$('#registered_note').show('slide',{direction: 'up'},'fast');	
					});
				} else {
					FB.api('/me', function(response) {
						//console.log(response);
						email = response.email;
						id = response.id;
						$.get(href_url, {
							'action': 'register_user_from_facebook',
							'first_name': response.first_name,
							'last_name': response.last_name,
							'date_of_birth': response.birthday,
							'gender': response.gender,
							'email': response.email,
							'id': response.id
						}, function(data) {
							if (data.status == 'success') {
						        FB.api('/me/feed', 'post', { message: data.body ,link : 'http://www.shutaf.com',picture : 'http://www.shutaf.com/images/global/empty.jpg' }, function(response) {
						            if (!response || response.error) {
						                 $('.errors').show('slide', {direction: 'up'	}, 'fast');
										$('.loader').hide('slide', {direction: 'left'}, 'fast');
						            } else {
										$('.errors').show('slide', {direction: 'up'	}, 'fast');
										$('.loader').hide('slide', {direction: 'left'}, 'fast');
									}
						       });
							} else {
								$('.errors').show('slide', {direction: 'up'	}, 'fast');
								$('.loader').hide('slide', {direction: 'left'}, 'fast');
							}
						}, 'json');
					});	
				}
			}, 'json');
		} else {
			$('.superdoc.login .loader').hide('slide', {direction: align});
		}
	}, {perms: 'email,user_birthday,user_location,user_interests,publish_stream'});
}
 


/*
function add_marking_to_exsiting_post(payment_type) {
	$('#form__post_edit').append('<input type="hidden" name="param__marked" value="1">');
	$('#form__post_edit').append('<input type="hidden" name="param__notify_user" value="0">');
	var date = new Date();
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
//	date.setMonth(date.getMonth()+1);
	var m = date.getMonth() + 2;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	var max_date = new Date(yy, m, 0);
	if (d >  max_date.getDate()) {
		day =  max_date.getDate();
	}
	if (m > 12 ) {
		m = 1;
		month = (m < 10) ? '0' + m : m;
		yy++;
		year = (yy < 1000) ? yy + 1900 : yy;
	}
	$('#form__post_edit').append('<input type="hidden" name="param__end_date" value="'+year+month+day+'">');
	$('#form__post_edit').append('<input type="hidden" name="param__ajax_submit" value="1">');
	if (payment_type == 'long-term') {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_marking_must_pay});
	} else {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_normal_must_pay});	
	}
} 

function add_marking_to_exsiting_post(payment_type) {
	$('#form__post_edit').append('<input type="hidden" name="param__marked" value="1">');
	$('#form__post_edit').append('<input type="hidden" name="param__notify_user" value="0">');
	var date = new Date();
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
//	date.setMonth(date.getMonth()+1);
	var m = date.getMonth() + 2;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	var max_date = new Date(yy, m, 0);
	if (d >  max_date.getDate()) {
		day =  max_date.getDate();
	}
	if (m > 12 ) {
		m = 1;
		month = (m < 10) ? '0' + m : m;
		yy++;
		year = (yy < 1000) ? yy + 1900 : yy;
	}
	$('#form__post_edit').append('<input type="hidden" name="param__end_date" value="'+year+month+day+'">');
	$('#form__post_edit').append('<input type="hidden" name="param__ajax_submit" value="1">');
	if (payment_type == 'long-term') {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_marking_must_pay});
	} else {
		$('#form__post_edit').ajaxSubmit({dataType: 'json',success: set_gts_widget_with_normal_must_pay});	
	}
} 
*/

