/* 
 * Web app description
 *
 */
var addWebAppLeft = 0;
var addWebAppTop = 0;

//Add Web App to iOS
function initWebApp() {
  var IsiPhone = navigator.userAgent.indexOf("iPhone") != -1 ;
  var IsiPod = navigator.userAgent.indexOf("iPod") != -1 ;
  var IsiPad = navigator.userAgent.indexOf("iPad") != -1 ;
  var IsiPhoneOS = IsiPhone || IsiPad || IsiPod ;
  if(IsiPhoneOS)
  {
    var nav = navigator;
    var OSVersion = nav.appVersion.match(/OS \d+_\d+/g);
    OSVersion = OSVersion ? OSVersion[0].replace(/[^\d_]/g,'').replace('_','.')*1 : 0;
    if(nav.standalone == false){
      var webAppAlready = getCookie("webapp");
      webAppAlready = parseInt(webAppAlready);
      if(isNaN(webAppAlready))
        webAppAlready = 0;
      if(webAppAlready < 1)
      {
        var stringAdd = '';
        stringAdd +='<div style="width:50%;z-index:1000; position: fixed; left: 25%; bottom: 0;" id="addWebApp">';
        stringAdd += '<img style="height:100%;width:100%;" src="/img/webapp/';
        if (IsiPod) {
          stringAdd += 'ipod';
        } else if (IsiPad) {
          stringAdd += 'ipad';
        } else {
          stringAdd += 'iphone';
        }
        if (OSVersion < 4.2)
          stringAdd += '_plus';
        stringAdd += '.png" id="addWebApp2" /><a style="position: absolute;top:0;left:80%;width:20%;height:50%;" onclick="removeWebApp2()"></a></div>';
        $('body').append(stringAdd);
        setTimeout('getWidth()', 10);
        setTimeout('removeWebApp()', 30000);
        var date = new Date();
        var date2WeekFromNow = new Date(date.getTime() + (61*24*60*60*1000));
        webAppAlready++;
        setCookie("webapp", webAppAlready, date2WeekFromNow);
      }
    }
    else
    {
      var lctn = document.location.href;
      lctn = lctn.toLowerCase();
      lctn = lctn.replace('http://', '');
      lctn = lctn.split('/');
      var webAppAlready = getCookie("webapp");
      webAppAlready = parseInt(webAppAlready);
      if(isNaN(webAppAlready))
      {
        var script   = document.createElement("script");
        script.type  = "text/javascript";
        script.src   = "http://www.g5e.com/facebook/f_shop/ipad/statistics.php";
        script.text  = "";
        document.body.appendChild(script);
        if(lctn.length > 1){
          if(lctn[1].length > 0)
          {
            document.location.href = '/';
          }
        }
      }
      checkLinkInApp();
      var date = new Date();
      var date15Minutes = new Date(date.getTime() + (15*60*1000));
      setCookie("webapp", 1, date15Minutes);
    }
  }
}

function removeWebApp2(){
  var date = new Date();
  var date2WeekFromNow = new Date(date.getTime() + (61*24*60*60*1000));
  setCookie("webapp", 2, date2WeekFromNow);
  removeWebApp();
}

function removeWebApp(){
  $("#addWebApp").fadeOut(3000, function(){
    $(this).remove();
  });
}

function getWidth(){
  var nav = navigator;
  var addWebApp = $('#addWebApp');
  if(addWebApp.length > 0 && !nav.standalone){
    var IsiPad = navigator.userAgent.indexOf("iPad") != -1 ;
    var screenWidth = window.innerWidth;
    var screenHeight = window.innerHeight;
    var screenScrolLeft = $(window).scrollLeft();
    var screenScrolTop = $(window).scrollTop();
    var rationWidth = 1;
    var ratioHeight = 0.4;
    var imgWidth = 0;
    var imgHeight = 0;
    var addWebAppLeftNew = 0;
    var addWebAppTopNew = 0;
    if(IsiPad){
      ratioHeight = 0.372;
      var iPadMargin;
      if (window.orientation == 0 || window.orientation == 180){
        rationWidth = 0.325;
        iPadMargin = 0.111;
      }else{
        rationWidth = 0.244;
        iPadMargin = 0.083;
      }
      imgWidth = screenWidth*rationWidth;
      imgHeight = screenWidth*rationWidth*ratioHeight;
      addWebAppLeftNew = screenScrolLeft+screenWidth*iPadMargin;
      addWebAppTopNew = screenScrolTop;
    }else{
      ratioHeight = 0.35743;
      if (window.orientation == 0 || window.orientation == 180) rationWidth = 0.778;
      else rationWidth = 0.51875;
      imgWidth = screenWidth*rationWidth;
      imgHeight = screenWidth*rationWidth*ratioHeight;
      addWebAppLeftNew = screenScrolLeft+screenWidth*(1-rationWidth)/2;
      addWebAppTopNew = screenScrolTop+screenHeight-screenWidth*rationWidth*ratioHeight;
    }
    addWebApp.width(imgWidth).height(imgHeight);
    if(addWebAppLeftNew!=addWebAppLeft || addWebAppTopNew!=addWebAppTop){
      addWebApp.animate({
        left: addWebAppLeftNew,
        top: addWebAppTopNew
      }, 500);
      addWebAppLeft=addWebAppLeftNew;
      addWebAppTop=addWebAppTopNew;
    }
    setTimeout('getWidth()', 300);
  }
}

function checkLinkInApp() {
  var a = $('a');
  var i;
  var attr1, attr_1, attr2;
  for(i = 0; i < a.length; i++)
  {
    attr1 = a.eq(i).attr('href');
    attr2 = a.eq(i).attr('target');
    if(attr2 != '_blank')
    {
      attr_1 = attr1.toLowerCase();
      if(attr_1.indexOf('javascript') == -1 && attr_1.indexOf('http://') == -1)
      {
        a.eq(i).click(function(){
          var hrf = $(this).attr('href');
          document.location.href = hrf;
          return false;
        });
      }
    }
  }
}
