﻿$(function() {
  $('#demo-video').dialog({
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    title: 'Demo Video',
    width:540,
    close: function() {
      //-- stop video --//
      //document.getElementById('demo-video-player').sendEvent('STOP');
    }
  });
  $('div.ui-widget-overlay').live('click',function(){
    $('#demo-video').dialog('close');
  });
  
  $('#appname').ellipsis();

  setTimeout(function(){ init() },1);

  $('#embed-video').keypress(function(e) {
    if (e.which != 3)
      return false;
  }).bind('mousedown', function() {
    var obj = this;
    setTimeout(function() { checkEmbedSelection(obj) }, 1);
    return false;
  });

  function checkEmbedSelection(obj) {
    if ($(obj).is(':not(:selected)')) {
      obj.select();
    }
  }

  $('#screenshot-left').click(function() {
    if ($('#screenshots:not(:animated)').length == 1) {
      var width = $('#screenshots li:first').width();
      var ml = parseInt($('#screenshots').css('margin-left'), 10);
      var curindex = ml != 0 ? Math.abs(ml) / width : 0;
      scrollScreens(curindex - 1);
    }
  });
  $('#screenshot-right,#screenshots img').click(function() {
    if ($('#screenshots:not(:animated)').length == 1) {
      var width = $('#screenshots li:first').width();
      var ml = parseInt($('#screenshots').css('margin-left'), 10);
      var curindex = ml != 0 ? Math.abs(ml) / width : 0;
      scrollScreens(curindex + 1);
    }
  });
  $('#screenshot-control a').click(function(e) {
    var li = $('#screenshot-' + $(this).attr('rel'));
    var index = $('#screenshots li').index(li);

    scrollScreens(index);

    e.preventDefault();
    return false;
  });

  $('#watch-video').click(function(e) {
    launchVideo();
    e.preventDefault();
    window.location.hash = '';
    return false;
  });

  $('#details-read-more').live('click', function() {
    if ($('#app-details-remainder').is(':visible')){
      $('#app-details-remainder').slideUp();
      $(this).html('Show More &gt;');
    } else {
      $('#app-details-remainder').slideDown();
      $(this).html('Show Less &gt;');
    }
  });
  
  $('#related-apps-scroll-left').click(function(){
    if ($('#related-apps:not(:animated)').length == 1) {
      scrollRelatedApps(-1);
    }
  });
  $('#related-apps-scroll-right').click(function(){
    if ($('#related-apps:not(:animated)').length == 1) {
      scrollRelatedApps(1);
    }
  });

  function init() {
    var hash;
    try { hash = window.location.hash.substr(1); } catch (err) { }

    if (hash != null) {
      if (hash == "video") {
        launchVideo();
      }
      else if ($('#screenshot-' + hash).length == 1) {
        var index = $('#screenshots li').index($('#screenshot-' + hash));
        scrollScreens(index);
      }
    }

    $('#app-details').append('<a id="details-read-more">Read More &gt;</a>');
    
    if (videoFileUrl.length != 0){
      var so = new SWFObject(modalityObjects.rootpath + 'videos/player.swf','demo-video-player','500','350','9','#ffffff');
      so.addParam('allowfullscreen','true');
      so.addParam('allowscriptaccess','always');
      so.addParam('wmode','opaque');
      so.addVariable('file', videoFileUrl);
      so.write('video-replace');
    }
  }

  function scrollScreens(index) {
    var width = $('#screenshots li:first').width();
    var ml = $('#screenshots').css('margin-left');
    var lis = $('#screenshots li').length;
    var sindex = index;

    if (index < 0)
      sindex = lis - 1;
    else if (index >= lis)
      sindex = 0;

    $('#screenshots').stop(false, false).animate({
      marginLeft: width * sindex * -1
    }, 500);

    $('#screenshot-control a').removeClass('on');
    $('#screenshot-control a:eq(' + sindex + ')').addClass('on');
  }
  
  function scrollRelatedApps(index) {
    var width = parseInt($('#related-apps li:first').width(),10) + parseInt($('#related-apps li:first').css('padding-right'),10);
    var ml = parseInt($('#related-apps').css('margin-left'),10);
    var lis = $('#related-apps li').length;
    var curindex = ml != 0 ? Math.abs(ml) / width : 0;
    var sindex = index + curindex;
    
    if (sindex < 0)
      sindex = 0;
    else if (sindex >= lis)
      sindex = 0;
    else if (sindex + 6 > lis)
      sindex = curindex;

    $('#related-apps').stop(false, false).animate({
      marginLeft: width * sindex * -1
    }, 500);
  }

  function launchVideo() {
    $('#demo-video').dialog('open');
    //document.getElementById('demo-video-player').sendEvent('PLAY');
  }
});
