// Copyright 2007, Google Inc.
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met:
//
//  1. Redistributions of source code must retain the above copyright notice, 
//     this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//  3. Neither the name of Google Inc. nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Change this to set the name of the managed resource store to create.
// You use the name with the createManagedStore, and removeManagedStore,
// and openManagedStore APIs. It isn't visible to the user.
var STORE_NAME = "my_offline_docset";

// Change this to set the URL of tha manifest file, which describe which
// URLs to capture. It can be relative to the current page, or an
// absolute URL.
var MANIFEST_FILENAME = "manifest.json";
var localServer;
var store;

var gears_logo = '<a href="http://gears.google.com"><img src="gears_logo.gif" border="0" alt="أنقر لمعرفة المزيد عن Google Gears" style="float:left;" /></a>';
var gears_msg = new Array(4);
gears_msg[0] = gears_logo + 'بإمكانك تسريع تصفح الموقع من خلال استخدام <a href="http://gears.google.com"><em>Google Gears</em></a> وهي إضافة مجانية من جوجل لتسريع المواقع. يمكنك تحميلها من <a href="http://gears.google.com">موقع جوجل جيرز</a>';
gears_msg[1] = gears_logo + 'أنت بالفعل تملك <a href="http://gears.google.com"><em>Google Gears</em></a>. أضغط الزر <input type="button" value=" استخدام Google Gears " onclick="enableGears()" /> لتمكين الموقع من استخدامها. تقوم Google Gears بتسريع تصفح المواقع وهي إضافة مجانية من جوجل, لمعرفة المزيد عنها أدخل إلى <a href="http://gears.google.com">موقع جوجل جيرز</a>';
gears_msg[2] = gears_logo + 'يتم الآن تركيب <a href="http://gears.google.com"><em>Google Gears</em></a> يرجى الإنتظار ...<span id="gears_prograss"></span>';
gears_msg[3] = gears_logo + 'تم تركيب <a href="http://gears.google.com"><em>Google Gears</em></a> بنجاح. الآن تمتع بتصفح فائق السرعة للموقع';

// Called onload to initialize local server and store variables
function initGears()
{
	if (!window.google || !google.gears)
	{
		hintOut(gears_msg[0]);
	}
	else
	{
		if(window.google.gears.factory.hasPermission)
		{
			doInitGears();
		}
		else
		{
			hintOut(gears_msg[1]);
		}
	}
}

var first_time_gears = false;
function enableGears()
{
	first_time_gears = true;
	alert("ستظهر لك رسالة تأكيد بأنك تريد السماح للموقع باستخدام جوجل جيرز. \nضع إشارة صح وأضغط الزر \nAllow");
	doInitGears();
}

function doInitGears()
{
	localServer = google.gears.factory.create("beta.localserver");
	store = localServer.createManagedStore(STORE_NAME);
	createStore();
	if(first_time_gears)
	{
		hintOut(gears_msg[2]);
	}
}

// Create the managed resource store
function createStore() {
  if (!window.google || !google.gears) {
	alert("يجب أولاً أن تقوم بتركيب Google Gears");
    return;
  }
  
  store.onprogress = function(details)
  {
	var gears_prograss = document.getElementById('gears_prograss');
	if(gears_prograss)
		gears_prograss.innerHTML = Math.round(details.filesComplete * 100 / details.filesTotal) + "%";
  }
  
  store.manifestUrl = MANIFEST_FILENAME;
  store.checkForUpdate();

  var timerId = window.setInterval(function() {
    // When the currentVersion property has a value, all of the resources
    // listed in the manifest file for that version are captured. There is
    // an open bug to surface this state change as an event.
    if (store.currentVersion)
	{
		window.clearInterval(timerId);
		if(first_time_gears)
		{
			hintOut(gears_msg[3]);
		}
    }
	else if (store.updateStatus == 3)
	{
		hintOut("حدث خطأ في Google Gears: " + store.lastErrorMessage +"\nيرجى تحديث الصفحة وإعادة المحاولة من جديد");
    }
  }, 500);  
}

// Remove the managed resource store.
function removeStore() {
  if (!window.google || !google.gears) {
    alert(install_gears_msg);
    return;
  }

  localServer.removeManagedStore(STORE_NAME);
  hintOut("تم إزالة جميع الملفات المستخدمة في Google Gears");
}

// Utility function to output some status text.
function hintOut(s) {
	var gears_hint_msg = document.getElementById('hint_msg');
	if(gears_hint_msg) gears_hint_msg.innerHTML = s;
	
	var gears_hint_msg_box = document.getElementById('hint_msg_box');
	if(gears_hint_msg_box) gears_hint_msg_box.style.display = 'block';
}