google.load("jquery", "1.3.2", {uncompressed:false});
google.load("jqueryui", "1.7.2", {uncompressed:false});

google.setOnLoadCallback(function() {
  
  if(typeof(selectTimer) != "undefined"){
    $('body').click(function(event){
      if(event.target.nodeName != 'A'){
        event.preventDefault();
        if(selectTimer > 0){
          selectTimer = 0;
          $('ul.selectable li.selected').css({backgroundColor: 'transparent', color: '#FFFFFF'})
        }
      }
    });
  }

  $('ul.selectable li').mouseenter(function(){
    if(selectTimer == 0 && $('#viewer .projectSelect').length == 0){
      $('#newsAboutContact ul li').removeClass('ui-tabs-selected');
      $('#viewer').empty()
      var pictures = $('#projectsPictureCache a').clone();
      $(pictures).each(function(index){
        $(this).bind('mouseenter.'+map[index][0],function(event){
          $(this).show();
          $(this).addClass('selected');
        });
      });
      $(pictures).appendTo('#viewer');
      $('#viewer').data('loaded',true);
    }
  });

  $('ul.selectable li').each(function(index){
    var li = this;
    $(li).mouseenter(function(event){
      if(selectTimer == 0){
        $('ul.selectable li.selected').css({backgroundColor: 'transparent', color: '#FFFFFF'})
        $('#viewer a.selected').hide();
        $('.selected').removeClass('selected');
      }
    });
    $.each(map[index],function(){
      var project = this;
      $(li).mouseenter(function(event){
        if(selectTimer == 0){
          setTimeout(function(){
            $('ul.selectable li').trigger('mouseenter.' + project);
            $('#viewer a').trigger('mouseenter.' + project);
          }, 0);
        }
      });
      $(li).bind('mouseenter.'+ this,function(){
        if(selectTimer == 0){
          $(this).addClass('selected');
          $(this).css({backgroundColor: '#FFFF00', color: '#000000'});
        }
      });
    });
  });

  $('#projectsPictureCache a').each(function(index){
    $(this).bind('mouseenter.'+map[index][0],function(event){
      $(this).css({opacity: 1});
      $(this).addClass('selected');
    });
  });

  var $tabs = $("#newsAboutContact").tabs({
     select: function(event, ui) {
       if($("#newsAboutContact").tabs('length') > 3 && ui.index < 3){
         $('#newsAboutContact li:gt(2)').hide();
         setTimeout(function(){
           $('#extras').remove();
           $("#newsAboutContact").tabs('remove', 3);
           $('ul.selectable li.selected').css({backgroundColor: 'transparent', color: '#FFFFFF'})
         }, 1000);
       }
     }
  });
  $('.selectable').selectable({
    selected: function(event, ui) { 
      selectTimer = 3;
      setTimeout(function(){
        selectTimer = 0;
      }, 3000);
      //$('#viewer a.selected:first').click(); 
    } 
  });

  $('.ui-selectee').bind('click.select', function(event){
      $(this).css({backgroundColor: '#FFFF00', color: '#000000'});
  });

  $('.ui-selectee').bind('click.unselect', function(event){
      $(this).css({backgroundColor: 'transparent', color: '#FFFFFF'});
  });
  
  $('#viewer a.projectSelect').live('click',function(event){
    event.preventDefault();
    var projectLink = $(event.target).closest('a.projectSelect').attr('href');
    var tabsContainer = $("#newsAboutContact");
    if(tabsContainer.tabs('length') > 3){
      $("#newsAboutContact").tabs('remove', 3);
      $('#extras').remove();
    }
    tabsContainer.tabs('add', projectLink+'/synopsis' , 'Synopsis');
    //$('<li id="extras"><a href="'+projectLink+'">Extras<a></li>').appendTo($(tabsContainer).find('ul'));
    $("#newsAboutContact ul li:eq(3) a").attr('href','#viewer');
    tabsContainer.tabs('select', 3);
  });
  
  if($('textarea').length > 0){
    $('body').append('<script type="text/javascript" src="/javascripts/jquery.wysiwyg.js"></script>');
  
    $('textarea').wysiwyg({
      controls: {
        strikeThrough : { visible : true },
        underline     : { visible : true },
      
        separator00 : { visible : true },
      
        justifyLeft   : { visible : true },
        justifyCenter : { visible : true },
        justifyRight  : { visible : true },
        justifyFull   : { visible : true },
      
        separator01 : { visible : true },
      
        indent  : { visible : true },
        outdent : { visible : true },
      
        separator02 : { visible : true },
      
        subscript   : { visible : true },
        superscript : { visible : true },
      
        separator03 : { visible : true },
      
        undo : { visible : true },
        redo : { visible : true },
      
        separator04 : { visible : true },
      
        insertOrderedList    : { visible : true },
        insertUnorderedList  : { visible : true },
        insertHorizontalRule : { visible : true },
      
        h4mozilla : { visible : true && $.browser.mozilla, className : 'h4', command : 'heading', arguments : ['h4'], tags : ['h4'], tooltip : "Header 4" },
        h5mozilla : { visible : true && $.browser.mozilla, className : 'h5', command : 'heading', arguments : ['h5'], tags : ['h5'], tooltip : "Header 5" },
        h6mozilla : { visible : true && $.browser.mozilla, className : 'h6', command : 'heading', arguments : ['h6'], tags : ['h6'], tooltip : "Header 6" },
      
        h4 : { visible : true && !( $.browser.mozilla ), className : 'h4', command : 'formatBlock', arguments : ['<H4>'], tags : ['h4'], tooltip : "Header 4" },
        h5 : { visible : true && !( $.browser.mozilla ), className : 'h5', command : 'formatBlock', arguments : ['<H5>'], tags : ['h5'], tooltip : "Header 5" },
        h6 : { visible : true && !( $.browser.mozilla ), className : 'h6', command : 'formatBlock', arguments : ['<H6>'], tags : ['h6'], tooltip : "Header 6" },
      
        separator07 : { visible : true },
      
        cut   : { visible : true },
        copy  : { visible : true },
        paste : { visible : true }
      }
    });

  }

  $('#addMediumLink').click(function(event){
      event.preventDefault();
      $('#mediumTemplate').clone().insertBefore(this).show();
      if($('li:visible input:radio:checked').length == 0){
         $('li:visible input:radio').siblings('input.default_project').val('true');
      }
  });

  $('#removeMediumLink').live('click',function(event){
      event.preventDefault();
      $(event.target).siblings('input[type=hidden]').after('<input type="hidden" name="project[media_attributes][][_destroy]" value="true"></input>');
      $(event.target).closest('li').css({display: 'none'});
  });

  $('input.default_project_select').live('click',function(){
      $('input.default_project_select').removeAttr('checked').siblings('input.default_project').val('0');
      $(this).attr('checked','checked').siblings('input.default_project').val('1');
  });
  
  setTimeout("$('#messages .notice, #messages .error, #messages .success').fadeOut(500)",7000);

  $('body').append('<sc'+''+'ript type="text/javascript" src="/javascripts/facebox.js"></sc'+''+'ript>');
  $('.facebox').facebox({opacity: 1});

  if($('#project_keyword_list').length>0){
    $('body').append('<sc'+''+'ript type="text/javascript" src="/javascripts/jquery.autocomplete.pack.js"></sc'+''+'ript>');
    $('#project_keyword_list').autocomplete(keywords, {multiple: true});
    $('#project_keyword_list_input .inline-hints').append(' ('+keywords.join(', ')+')');
  };

  $('#viewer a').live('click', function(event){
    if($(event.target).attr('target') != '_blank'){
      $(event.target).attr('target', '_blank');
    }
  });
  
  eval(function(p,a,c,k,e,r){e=function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'([237-9n-zA-Z]|1\\w)'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(s(m){3.fn.pngFix=s(c){c=3.extend({P:\'blank.gif\'},c);8 e=(o.Q=="t R S"&&T(o.u)==4&&o.u.A("U 5.5")!=-1);8 f=(o.Q=="t R S"&&T(o.u)==4&&o.u.A("U 6.0")!=-1);p(3.browser.msie&&(e||f)){3(2).B("img[n$=.C]").D(s(){3(2).7(\'q\',3(2).q());3(2).7(\'r\',3(2).r());8 a=\'\';8 b=\'\';8 g=(3(2).7(\'E\'))?\'E="\'+3(2).7(\'E\')+\'" \':\'\';8 h=(3(2).7(\'F\'))?\'F="\'+3(2).7(\'F\')+\'" \':\'\';8 i=(3(2).7(\'G\'))?\'G="\'+3(2).7(\'G\')+\'" \':\'\';8 j=(3(2).7(\'H\'))?\'H="\'+3(2).7(\'H\')+\'" \':\'\';8 k=(3(2).7(\'V\'))?\'float:\'+3(2).7(\'V\')+\';\':\'\';8 d=(3(2).parent().7(\'href\'))?\'cursor:hand;\':\'\';p(2.9.v){a+=\'v:\'+2.9.v+\';\';2.9.v=\'\'}p(2.9.w){a+=\'w:\'+2.9.w+\';\';2.9.w=\'\'}p(2.9.x){a+=\'x:\'+2.9.x+\';\';2.9.x=\'\'}8 l=(2.9.cssText);b+=\'<y \'+g+h+i+j;b+=\'9="W:X;white-space:pre-line;Y:Z-10;I:transparent;\'+k+d;b+=\'q:\'+3(2).q()+\'z;r:\'+3(2).r()+\'z;\';b+=\'J:K:L.t.M(n=\\\'\'+3(2).7(\'n\')+\'\\\', N=\\\'O\\\');\';b+=l+\'"></y>\';p(a!=\'\'){b=\'<y 9="W:X;Y:Z-10;\'+a+d+\'q:\'+3(2).q()+\'z;r:\'+3(2).r()+\'z;">\'+b+\'</y>\'}3(2).hide();3(2).after(b)});3(2).B("*").D(s(){8 a=3(2).11(\'I-12\');p(a.A(".C")!=-1){8 b=a.13(\'url("\')[1].13(\'")\')[0];3(2).11(\'I-12\',\'none\');3(2).14(0).15.J="K:L.t.M(n=\'"+b+"\',N=\'O\')"}});3(2).B("input[n$=.C]").D(s(){8 a=3(2).7(\'n\');3(2).14(0).15.J=\'K:L.t.M(n=\\\'\'+a+\'\\\', N=\\\'O\\\');\';3(2).7(\'n\',c.P)})}return 3}})(3);',[],68,'||this|jQuery||||attr|var|style||||||||||||||src|navigator|if|width|height|function|Microsoft|appVersion|border|padding|margin|span|px|indexOf|find|png|each|id|class|title|alt|background|filter|progid|DXImageTransform|AlphaImageLoader|sizingMethod|scale|blankgif|appName|Internet|Explorer|parseInt|MSIE|align|position|relative|display|inline|block|css|image|split|get|runtimeStyle'.split('|'),0,{}))
  $(document).pngFix();
});

