			$(document).ready(function(){
				$("#contentLoader").hide();
				loginText('.swap-value');
				checkLogin();
				intializeFaceBook('124550327614861');
					
					standAloneChangePage = false;
				    $.history.init(function(hash){
						if(hash == "") {
							// initialize your app
							checkLink();
						} else {
							// restore the state from hash
							checkLink();
						}
					});
				
			});
			function checkLink(){
				var urlHalves = String(document.location).split('#');
				var urlHalves1 = String(urlHalves[1]).split('/');
				page_link = urlHalves[0];
				get_page = urlHalves1[0];
				get_do = urlHalves1[1];
				get_id = urlHalves1[2];
				get_var1 = urlHalves1[3];
				get_var2 = urlHalves1[4];
				if(get_page  == 'undefined' || get_page  == ''){
					getPage('main');
				}else{
					if(!standAloneChangePage)getPage(get_page,get_do,get_id);
				}
			};
			
			function loadPage(link,tdo,tid){
				if(tdo!=undefined){
					theDo = '/'+tdo;
					theDoLink = '&do='+tdo;
				}else{
					theDo = '';
					theDoLink = '';
				}
				if(tid!=undefined){
					theId = '/'+tid;
					theIdLink = '&id='+tid;
				}else{
					theId = '';
					theIdLink = '';
				}
				document.location = "#"+link+theDo+theId;
				
				get_page = link;
			};
			function getPage(link,tdo,tid){
				var loader = $("#loaderInHead");
				if(tdo!=undefined){
					theDo = '/'+tdo;
					theDoLink = '&do='+tdo;
				}else{
					theDo = '';
					theDoLink = '';
				}
				if(tid!=undefined){
					theId = '/'+tid;
					theIdLink = '&id='+tid;
				}else{
					theId = '';
					theIdLink = '';
				}
				//$("#contentLoader").html("جاري تحميل الصفحة...");
				loader.html("<img valign=middle src='webCore/resources/ajax-loader.gif'>  جاري التحميل...").slideDown(function(){
					document.location = "#"+link+theDo+theId;
					$.get('json.php?page='+link+theDoLink+theIdLink, function(data) {
						$("#contentLoader").fadeOut(function(){
							$("#contentLoader").html(data);
							$("#contentLoader").fadeIn();
							loader.slideUp(1000);
						});
					});
					
					get_page = link;
				});
			};
			
			
			function doRegister(){
				$("#doRegister").html("انتظر ..");
				$.get('json.php?page=register', function(data) {
					jQuery.bsBox(data);
					$("#doRegister").html("انضم الينا");
				});
			}
			
			function forgetPass(){
				var loader = $("#loaderInHead");
				loader.show();
				$.get('json.php?page=forgetPass', function(data) {
					jQuery.bsBox(data);
					loader.hide();
				});
			}
			
			//----------------------------------
			function loginText(theID){
				swapValue = [];
				$(theID).each(function(i){
				//alert($(this).val())

				   swapValue[i] = $(this).val();
				   $(this).focus(function(){
					  if ($(this).val() == swapValue[i]) {
						 $(this).val("");
					  }
					  $(this).addClass("focus");
				   }).blur(function(){
					  if ($.trim($(this).val()) == "") {
						 $(this).val(swapValue[i]);
					 $(this).removeClass("focus");
					  }
				   });
				});
			}
			
		//---------------------------------
		function checkLogin(){
			
				$("#loginError").html("جاري تسجيل الدخول...").show();
				$("#loginFormMain").hide();

				$.get('json.php?page=checkLogin' , function(data) {
					var data = eval("({" + data + "})");
					if(data.status == true){
						$("#loginError").html("مرحبا بك يا "+data.name).show();
						$("#logoutBtn").show();
						$("#loginJoin").hide();
						$(".topHeadForgetPass").hide();
					}else{
						$("#loginError").hide();
						$("#loginFormMain").show();
					}
				  
				});
		}
		//---------------------------------
		function postMainLogin(){
			codeError = false;
			var name = $("#LoginName");
			var pass = $("#LoginPass");
			
			if(name.val() == '' || name.val() == 'ادخل اسم المستخدم او البريد الالكتروني'){
				alert("يجب ان لا تترك خانة اسم المستخدم فارغة");
				codeError = true;
				name.focus();
			}else if(pass.val() == '' || pass.val() == 'كلمة المرور'){
				alert("يجب ان لا تترك خانة كلمة المرور فارغة");
				codeError = true;
				pass.focus();
			}
			
			
			if(codeError != true){
				$("#loginError").html("جاري تسجيل الدخول...").show();
				$("#loginFormMain").hide();

				$.post('json.php?page=userLogin' , { name: name.val(), pass: pass.val() }, function(data) {
					var data = eval("({" + data + "})");
					if(data.status == true){
						$("#loginError").html("مرحبا بك يا "+data.name).show();
						$("#logoutBtn").show();
						$("#loginJoin").hide();
						$(".topHeadForgetPass").hide();
						name.val("");
						pass.val("");
						checkLink();
					}else{
						codeError = true;
						$("#loginError").hide();
						$("#loginFormMain").slideDown(function(){
							alert("لم يتم الدخول يرجى التأكد من البيانات.");
							name.focus();
						});
					}
				  
				});
			}
		}
		//---------------------------------
		function logout(){		
			var name = $("#LoginName");
				$("#loginFormMain").hide();
				$("#logoutBtn").hide();
				$("#loginError").html("جاري تسجيل الخروج...").show();

				$.get('json.php?page=userLogOut' , function(data) {
					var data = eval("({" + data + "})");
					if(data.status == true){
						$("#loginError").hide();
						$("#loginJoin").show();
						$(".topHeadForgetPass").show();
						$("#loginFormMain").slideDown(function(){
							name.focus();
						});
						checkLink();
					}else{
						codeError = true;
						$("#loginError").hide();
							$("#logoutBtn").show();
							alert("لم يتم الخروج. هناك خطأ .");
					}
				  
				});
		}

		
	//---------------------------------
		function countComment(commentPost,comLoad){
			var commentPost = $("#"+commentPost);
			var comLoad = $("#"+comLoad);
			
			comLoad.html("عدد الحروف المستخدمة "+commentPost.val().length+" حرف.").show();
		}		
	//---------------------------------
		function postComment(id){
			codeError = false;
			var commentPost = $("#commentPost");
			var comLoad = $("#comLoad");
			var commentPostDiv = $("#commentPostDiv");
			var commentPostDivDone = $("#commentPostDivDone");
			
			if(commentPost.val().length < 10){
				comLoad.html("يجب ان تكون الحقول اكثر من 10 حرف.").show();
				codeError = true;
				commentPost.focus();
			}

			if(codeError != true){
				comLoad.html("جاري ارسال النص..").show();

				$.post('json.php?page=postComment' , { commentPost: commentPost.val() ,itemID:id}, function(data) {
					var data = eval("({" + data + "})");
					if(data.status == true){
						comLoad.html("تم الارسال").show();
						commentPost.val("");
						commentPostDiv.hide();
						commentPostDivDone.fadeIn();
					}else{
						codeError = true;
						comLoad.hide();
						alert("لم يتم الدخول يرجى التأكد من البيانات.");
					}
				  
				});
			}
		}
		
		//---------------------------------
		function postOnlineReg(id){
			codeError = false;
			var inputRegName = $("#inputRegName");
			var inputRegTel = $("#inputRegTel");
			var inputRegNums = $("#inputRegNums");
			var inputRegStart = $("#inputRegStart option:selected");
			var inputRegEnd = $("#inputRegEnd option:selected");
			var save = $("#onlineRegSave");
			var regForm = $("#onlineRegForm");
			
			if(inputRegName.val().length < 5){
				save.html("يجب ان لا تترك حقل الاسم فارغا او اقل من خمسة احرف.").show();
				codeError = true;
				inputRegName.focus();
			}else if(inputRegTel.val().length < 7){
				save.html("يجب ان لا تترك حقل الهاتف فارغا او اقل من سبعة ارقام.").show();
				codeError = true;
				inputRegTel.focus();
			}else if(inputRegNums.val().length < 1){
				save.html("يجب ان لا تترك حقل الاشخاص فارغا.").show();
				codeError = true;
				inputRegNums.focus();
			}

			if(codeError != true){
				save.html("جاري ارسال الطلب ..").show();

				$.post('json.php?page=onlineRegSave' , { inputRegName: inputRegName.val() ,inputRegTel: inputRegTel.val() ,inputRegNums: inputRegNums.val() ,inputRegStart: inputRegStart.val() ,inputRegEnd: inputRegEnd.val() ,itemID:id}, function(data) {
					var data = eval("({" + data + "})");
					if(data.status == true){
						save.html("تم الارسال").show();
						regForm.hide();
					}else{
						codeError = true;
						save.hide();
						regForm.show();
						alert("لم يتم ارسال الطلب . يتوجب عليك تسجيل الدخول .");
					}
				  
				});
			}
		}
		
		//-----------------------------
		function doSearch(cityID){
			var loader = $("#loaderInHead");
			loader.html("<img valign=middle src='webCore/resources/ajax-loader.gif'>  جاري البحث...").show();
		  var searchName = $('#searchName').val();
		  var hasDelivery 	= $('#hasDelivery').is(':checked');
		  if(cityID == undefined) var cityData = $('#cityData option:selected').val();
		  else cityData = cityID;
		if(cityData == undefined)cityData = 0 ;
			standAloneChangePage = true;
			document.location = "#search";
			  $.post("json.php?page=search",{name:searchName,hasDelivery:hasDelivery,cityData:cityData},function(data){
				 $("#contentLoader").html(data);
				 standAloneChangePage = false;
				 loader.hide();
			  });
		}
		//-----------------------------
		function showHead(){
			var topHeadBtn1 = $(".topHeadBtn1");
			topHeadBtn1.show();
		}
		function hideHead(){
			var topHeadBtn1 = $(".topHeadBtn1");
			topHeadBtn1.hide();
		}
		//-----------------------------
		function closeBox(){
			jQuery(document).trigger('close.bsBox')
		}
		//-----------------------------
		function addtoScroll(){
			$.get('json.php?page=addtoScroll', function(data) {
				jQuery.bsBox(data)
			});
		};
		//-----------------------------
		
		
//----------------------------------------------------------------------

function intializeFaceBook(FACEBOOK_APP_ID){
	FB.init({appId: FACEBOOK_APP_ID, status: true,cookie: true, xfbml: true});
		FB.Event.subscribe('auth.login', function(response){
			//alert(response.status);
			facebookConnect();
		   // window.location.reload();
		});

}

//-----------------------------------------------------------------------
function facebookConnect(){
	
		$("#loginError").html("جاري تسجيل الدخول...").show();
		$("#loginFormMain").hide();

		$.get('json.php?page=fLogin' , function(data) {
			//alert(data);
			var data = eval("({" + data + "})");
			if(data.message == true){
				$("#loginError").html("مرحبا بك يا "+data.name).show();
				$("#logoutBtn").show();
				$("#loginJoin").hide();
				$(".topHeadForgetPass").hide();
			}else{
				$("#loginError").hide();
				$("#loginFormMain").show();
			}
		  
		});

}

function FBlogout(){
	FB.logout(function(response) {
	  //alert(" user is now logged out");
	  loadResource('logout');
	});
}
function fbSignIn(){
	FB.login(function(response) {
	  if (response.session) {
		if (response.perms) {
			//$("#loginError").html("تم تسجيل الدخول").show();
			facebookConnect();
		 // alert(" user is logged in and granted some permissions.");
		  // perms is a comma separated list of granted permissions
		} else {
		 // alert(" user is logged in, but did not grant any permissions");
		 //	$("#loginError").html("تم تسجيل الدخول بدون اية صلاحيات").show();
			facebookConnect();
		}
	  } else {
		alert("Error : user is not logged in.");
		//$("#loginError").html("لم يتم تسجيل الدخول").show();
	  }
	}, {perms:'read_stream,publish_stream,offline_access,email,user_birthday,read_friendlists'});
}
		
		
		
