  var headline_count;
  var headline_interval;
  var old_headline = 0;
  var current_headline = 0;

$(document).ready(function () {
  /* Copyright 2007-2009 by Tobia Conforto <tobia.conforto@gmail.com> */
  $.fn.crossSlide=function(c,f){var a=this,e=this.width(),b=this.height();function d(j){for(var h=1;h<arguments.length;h++){j=j.replace(new RegExp("\\{"+(h-1)+"}","g"),arguments[h])}return j}function g(){arguments[0]="crossSlide: "+arguments[0];throw d.apply(null,arguments)}(function(j){var h=0;function i(l,k){k.onload=function(m){h++;f[l].width=k.width;f[l].height=k.height;if(h==f.length){j()}};k.src=f[l].src;if(l+1<f.length){i(l+1,new Image())}}i(0,new Image())})(function(){function u(y){var i=1;var p=y.replace(/^\s*|\s*$/g,"").split(/\s+/);if(p.length>3){throw new Error()}if(p[0]=="center"){if(p.length==1){p=["center","center"]}else{if(p.length==2&&p[1].match(/^[\d.]+x$/i)){p=["center","center",p[1]]}}}if(p.length==3){i=parseFloat(p[2].match(/^([\d.]+)x$/i)[1])}var z=p[0]+" "+p[1];if(z=="left top"||z=="top left"){return{xrel:0,yrel:0,zoom:i}}if(z=="left center"||z=="center left"){return{xrel:0,yrel:0.5,zoom:i}}if(z=="left bottom"||z=="bottom left"){return{xrel:0,yrel:1,zoom:i}}if(z=="center top"||z=="top center"){return{xrel:0.5,yrel:0,zoom:i}}if(z=="center center"){return{xrel:0.5,yrel:0.5,zoom:i}}if(z=="center bottom"||z=="bottom center"){return{xrel:0.5,yrel:1,zoom:i}}if(z=="right top"||z=="top right"){return{xrel:1,yrel:0,zoom:i}}if(z=="right center"||z=="center right"){return{xrel:1,yrel:0.5,zoom:i}}if(z=="right bottom"||z=="bottom right"){return{xrel:1,yrel:1,zoom:i}}return{xrel:parseInt(p[0].match(/^(\d+)%$/)[1])/100,yrel:parseInt(p[1].match(/^(\d+)%$/)[1])/100,zoom:i}}function j(y,i){switch(i){case 1:var z=0;break;case 2:var z=w/(y.time_ms+2*w);break;case 3:var z=1-w/(y.time_ms+2*w);break;case 4:var z=1;break}return{left:Math.round(y.from.left+z*(y.to.left-y.from.left)),top:Math.round(y.from.top+z*(y.to.top-y.from.top)),width:Math.round(y.from.width+z*(y.to.width-y.from.width)),height:Math.round(y.from.height+z*(y.to.height-y.from.height))}}if(!c.fade){g("missing fade parameter.")}if(c.speed&&c.sleep){g("you cannot set both speed and sleep at the same time.")}var w=Math.round(c.fade*1000);if(c.sleep){var v=Math.round(c.sleep*1000)}if(c.speed){var k=c.speed/1000,q=Math.round(w*k)}a.empty().css({overflow:"hidden",padding:0});if(!/^(absolute|relative|fixed)$/.test(a.css("position"))){a.css({position:"relative"})}if(!a.width()||!a.height()){g("container element does not have its own width and height")}if(c.shuffle){f.sort(function(){return Math.random()-0.5})}for(var n=0;n<f.length;++n){var h=f[n];if(!h.src){g("missing src parameter in picture {0}.",n+1)}if(k){switch(h.dir){case"up":h.from={xrel:0.5,yrel:0,zoom:1};h.to={xrel:0.5,yrel:1,zoom:1};var s=h.height-b-2*q;break;case"down":h.from={xrel:0.5,yrel:1,zoom:1};h.to={xrel:0.5,yrel:0,zoom:1};var s=h.height-b-2*q;break;case"left":h.from={xrel:0,yrel:0.5,zoom:1};h.to={xrel:1,yrel:0.5,zoom:1};var s=h.width-e-2*q;break;case"right":h.from={xrel:1,yrel:0.5,zoom:1};h.to={xrel:0,yrel:0.5,zoom:1};var s=h.width-e-2*q;break;default:g('missing or malformed "dir" parameter in picture {0}.',n+1)}if(s<=0){g("picture number {0} is too short for the desired fade duration.",n+1)}h.time_ms=Math.round(s/k)}else{if(!v){if(!h.from||!h.to||!h.time){g("missing either speed/sleep option, or from/to/time params in picture {0}.",n+1)}try{h.from=u(h.from)}catch(r){g('malformed "from" parameter in picture {0}.',n+1)}try{h.to=u(h.to)}catch(r){g('malformed "to" parameter in picture {0}.',n+1)}if(!h.time){g('missing "time" parameter in picture {0}.',n+1)}h.time_ms=Math.round(h.time*1000)}}if(h.from){$.each([h.from,h.to],function(y,p){p.width=Math.round(h.width*p.zoom);p.height=Math.round(h.height*p.zoom);p.left=Math.round((e-p.width)*p.xrel);p.top=Math.round((b-p.height)*p.yrel)})}var t;if(h.href){t=$(d('<a href="{0}"><img src="{1}"/></a>',h.href,h.src))}else{t=$(d('<img src="{0}"/>',h.src))}if(h.onclick){t.click(h.onclick)}if(h.alt){t.find("img").attr("alt",h.alt)}t.appendTo(a)}k=undefined;var o=a.find("img").css({position:"absolute",visibility:"hidden",top:0,left:0,border:0});o.eq(0).css({visibility:"visible"});if(!v){o.eq(0).css(j(f[0],2))}var x=c.loop;function l(K,p){if(K%2==0){if(v){var A=K/2,O=(A-1+f.length)%f.length,L=o.eq(A),I=o.eq(O);var H=function(){I.css("visibility","hidden");setTimeout(p,v)}}else{var D=K/2,O=(D-1+f.length)%f.length,N=o.eq(D),I=o.eq(O),B=f[D].time_ms,J=j(f[D],3);var H=function(){I.css("visibility","hidden");N.animate(J,B,"linear",p)}}}else{if(v){var z=Math.floor(K/2),C=Math.ceil(K/2)%f.length,M=o.eq(z),y=o.eq(C),P={},G={visibility:"visible"},F={};if(C>z){G.opacity=0;F.opacity=1}else{P.opacity=0}var H=function(){y.css(G);if(P.opacity!=undefined){M.animate(P,w,"linear",p)}else{y.animate(F,w,"linear",p)}}}else{var z=Math.floor(K/2),C=Math.ceil(K/2)%f.length,M=o.eq(z),y=o.eq(C),P=j(f[z],4),G=j(f[C],1),F=j(f[C],2);if(C>z){G.opacity=0;F.opacity=1}else{P.opacity=0}G.visibility="visible";var H=function(){M.animate(P,w,"linear");y.css(G);y.animate(F,w,"linear",p)}}}if(c.loop&&K==f.length*2-2){var E=H;H=function(){if(--x){E()}}}if(K>0){return l(K-1,H)}else{return H}}var m=l(f.length*2-1,function(){return m()});m()});return a};

  //Start Slideshow on Home Page
  if(document.getElementById('container')){slideshow();}

  // Clicking on slideshow displays the QuickTime Movie
  var movie = '<object id="trailerqt" width="853" height="496" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="enablejavascript" value="true" /><param name="postdomevents" value="true" /><param name="src" value="/wp-content/themes/gono9v3/videos/General_Orders_No9_Trailer2.mov" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="scale" value="aspect" /><param name="bgcolor" value="000000" /><param name="cache" value="true" /><embed width="853" height="496" src="/wp-content/themes/gono9v3/videos/General_Orders_No9_Trailer2.mov" bgcolor="000000" cache="true" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/" controller="true" enablejavascript="true" postdomevents="true" autoplay="true" scale="aspect" name="trailerqt" id= "trailerqt"></embed></object>';


  //play trailer local
  $('#container').click(function(){
    if(document.getElementById('overlay')) $(this).html(movie);
    RegisterListeners();
  });

  //Logo Link to Home Page
  $('#logoinside').click(function(){window.location = "/";});

  //Previous Links
  var previous  = $('#pagination .alignleft');
  var prevText  = previous.text();
  var prevLink  = $('#pagination .alignleft a').attr('href');

  if(prevText.length > 38){
    var decPreviousLink = prevText.substring(0,12)
    var newPreviousLink = prevText.substring(12,37);
    newPreviousLink     = '<em>' + decPreviousLink + '</em>' + newPreviousLink + '&#133;';
    previous.html(newPreviousLink);
  }

  if(previous.html()){
    previous.click(function(){window.location = prevLink;})
  }else{
    previous.css('cursor','default');
  }

  //Next Links
  var next        = $('#pagination .alignright');
  var nextText    = next.text();
  var nextLink    = $('#pagination .alignright a').attr('href');
  var nextLength  = nextText.length;

  if(nextLength > 28){
    var endNext         = nextLength - 9;
    var decNextLink     = nextText.substring(endNext,nextLength)
    var newNextLink     = nextText.substring(0,27);
    newNextLink         = newNextLink + '&#133;' + '<em>' + decNextLink + '</em>';
    next.html(newNextLink);
  }

  if(next.html()){
    next.click(function(){window.location = nextLink;})
  }else{
    next.css('cursor','default');
  }

  //Email Newsletter Signup
  $('#emailsignup input').focus(function(){
    if($(this).val()=='Enter Email Address') $(this).val('');
  });

  $('#emailsignup input').blur(function(){
    if($(this).val()=='') $(this).val('Enter Email Address');
  });

  $('#ok').click(function(){$('#emailsignup form').submit();});
  $('#ok2').click(function(){$('#screeningssignup form').submit();});

  //Homepage features scroll source: http://docs.jquery.com/Tutorials:Scroll_Up_Headline_Reader#Demo

  headline_count = $("div.scroll-headline").size();
  $("div.scroll-headline:eq("+current_headline+")").css('top','5px');

  headline_interval = setInterval(headline_rotate,8000); //time in milliseconds
  $('#scrollup').hover(function() {
    clearInterval(headline_interval);
    }, function() {
     headline_interval = setInterval(headline_rotate,8000); //time in milliseconds
     headline_rotate();
  });

  // swap the footer styly on the store page
  // if(document.getElementById('narrowpage')) {
  //   console.log('made it');
  //   $('#footer').attr('id', 'footer-store');
  // };
});

  //Homepage features scroll
function headline_rotate() {
   current_headline = (old_headline + 1) % headline_count;
   $("div.scroll-headline:eq(" + old_headline + ")").animate({top: -35},2000, function() {
     $(this).css('top','40px');
   });
   $("div.scroll-headline:eq(" + current_headline + ")").show().animate({top: 5},2000);
   old_headline = current_headline;
 }



function slideshow(){
  $(function($) {
    $('#slot').crossSlide(
      {sleep:5,fade:3},[
  	  {src:'/wp-content/themes/gono9v3/images/gray.gif',time: 1},
  	  {src:'/wp-content/themes/gono9v3/images/tree.jpg',time: 3},
      {src:'/wp-content/themes/gono9v3/images/geo.jpg',time: 3},
      {src:'/wp-content/themes/gono9v3/images/flower.jpg',time: 3},
      {src:'/wp-content/themes/gono9v3/images/office.jpg',time: 3}
    ]);
  });

  //Movie Trailer Rollover
  $('#overlay').mouseover(function(){
    $(this).css('background','#5c5c5c url(/wp-content/themes/gono9v3/images/overlay.gif) center -248px no-repeat');
  }).mouseout(function(){
    $(this).css('background','#5C5C5C url(/wp-content/themes/gono9v3/images/overlay.gif) no-repeat scroll center 136px');
  });
}

function unloadTrailer(){
  $('#container').html('<div id="overlay"></div><div id="slot"></div>');
  slideshow();
}

function myAddListener(obj, evt, handler, captures)
{
  if ( document.addEventListener )
    obj.addEventListener(evt, handler, captures);
  else
    obj.attachEvent('on' + evt, handler);
}

function RegisterListener(eventName, objID, embedID, listenerFcn)
{
  var obj = document.getElementById(objID);
  if(!obj )
    obj = document.getElementById(embedID);
  if(obj)
    myAddListener(obj, eventName, listenerFcn, false);
}

function RegisterListeners(){
  RegisterListener('qt_ended', 'trailerqt', 'trailerqt', unloadTrailer);
}

