var loginForm;
function gotoTweet(str) { if (str.indexOf('instance')<0) window.open('http://www.twitter.com/'+str);}

function loginFormHook() {
     $('#loginform').ajaxForm( { success: onLogin } );// { target: '#loginForm', success:onLogin);
}
function contactFormHook() {
     $('.wpcf7-form').ajaxForm( { target: '#signupForm', success:onSignup}  );
}
function onLogin(r) {
     $('.loginErr').remove();
     if (r.indexOf('Sorry')<0)  {
	    $('#loginform').html("<h1>You've been logged in. Welcome back!</h1><p>Be sure to <a href='javascript:window.location.reload();'>Reload</a> your webpage before you try to comment.</p>");
     } else {
	    $('#loginform').append('<div class="loginErr">'+r+"</div>");
     }
}
function onSignup(r) {
     if ($('#signupForm .wpcf7-validation-errors').length==0) {
          $('#signupForm').html("<div class='postHeadline'><h1>Thank you, your password will be emailed to you.</h1></div><p>Welcome to Zoomer Club.</p>");
     }
     contactFormHook();
}
function listenLive() {
     window.open('/listen-live','ListenLive','width=723,height=600,menubar=no,status=no,toolbar=no,scrollbars=yes');
     //window.open('http://am740.ca/site/player.html','ListenLive','width=723,height=600,menubar=no,status=no,toolbar=no,scrollbars=yes');
}
function onDemand() {
     window.open('/on-demand','OnDemand','width=723,height=600,menubar=no,status=no,toolbar=no,scrollbars=yes');
     //window.open('http://am740.ca/site/player.html','OnDemand','width=723,height=600,menubar=no,status=no,toolbar=no,scrollbars=yes');
     return false;
}
jQuery(
	function () {
          loginForm=$('#loginform').html();
          //$('.category-bios .entry').css( { height: (140+$('.grid_4').eq(0).height())+'px' } );

          $('#listenLive').flash({ swf: '/images/listenLive.swf', width: '207px', height: '38px', params: { wmode: 'transparent', scale: 'noborder' } });
          $('#listenLive object').css( { verticalAlign: '' });


          /* Navigation events */
          $('.menuBar a').mouseover(
            function () {
                roll=$(this).find('img').eq(0);
                roll.attr({ src:roll.attr('src').replace('.jpg','_over.png')});
            }
          );

          $('#onDemand').click(onDemand);

          /* Follows link text-less links inside divs [styled with cursor:
          ** pointer]
          */
          $('.divLink').click(
            function () {
               href=$( '#'+$(this).attr('rel') ).attr('href');
               if (href) window.location.href=href;
            }
          );

          if ($('.postHeadline h1').height()) $('.postHeadline').css({height:$('.postHeadline h1').height()+'px'});
          if (pch=$('.postContent').height()) $('.postContent').css({height: (pch<380?380:pch)+'px' });

          $('.openOverlay').overlay( { oneInstance: false, closeOnClick: false });
          $('.wpcf7-form').attr({id:'signupForm'});

          //contactFormHook();
          loginFormHook();

          $('.menuBar a').mouseout(
            function () {
                roll=$(this).find('img').eq(0);
                roll.attr({ src:roll.attr('src').replace('_over.png','.jpg')});
             }
          );

          $('.contentBox a').click(
            function () {
              //$('#contentDrop').load($(this).attr('href'), { contentDrop: true }, resetScroll);
              $('#contentDrop').html($.ajax( { async: false,  url: $(this).attr('href'), data: { contentDrop: true}}).responseText);
              $('.contentBox a').removeClass('yellow');
              $(this).addClass('yellow');
              return false;
            }
          );

          $('.contentBox a').mouseover(
               function () {
                    $(this).css( { color: '#000' });
                    $(this).parent().css({ backgroundImage: $(this).parent().css('backgroundImage').replace('.png','_over.png') });
               }
          );


          $('.contentBox a').mouseout(
               function () {
                    $(this).css( { color: '#D5D0D0' });
                    $(this).parent().css({ backgroundImage: $(this).parent().css('backgroundImage').replace('_over.png','.png') });
               }
          );

          $('.newWindow').click(
            function () {
              window.open($(this).attr('href'));
              return false;
            }
          );

          $('.loadRel').click(
               function () {
                  if ($(this).attr('rev')) {
                    $('#'+$(this).attr('rev')).load($(this).attr('rel'), { contentDrop: 1 } );
                    return false;
                  }
                  window.location.href=$(this).attr('rel');
               }
          );

          $('.loginBox input').click( function () { if ($(this).val().indexOf('Enter')==0) $(this).val(''); });

          $('.submit').click(function() { $(this).closest("form").submit(); });

          $('.lessBigButton').mouseover( function () { $(this).attr('class','lessBigButton_hover'); } );
          $('.bigButton').mouseover( function () { $(this).attr('class','bigButton_hover'); } );
          $('.lessBigButton').mouseout( function () { $(this).attr('class','lessBigButton'); } );
          $('.bigButton').mouseout( function () { $(this).attr('class','bigButton'); } );

          /* Form Processing */
          $('#loginForm').submit(
                function () {
                    return false;
                    $(this).find('input').eq(0).hide();
                    $(this).find('input').eq(1).show();
                    return false;
                }
          )
          $('#search').click(function() { if($(this).val()=='Search') $(this).val(''); });

          $('.showTooltip').tooltip( { fixPNG: true } );

          $('#login').unbind('click');
          $('#login').click(
               function () {
                    $(this).overlay().load();
                    return false;
               }
          );

          $('#register').unbind('click');
          $('#register').click(
               function () {
                    $('#socialClub').trigger('click');
                    return false;
               }
          );

          $('.leaveReply').click(
            function() { $('#commentform').css( { display: 'block'}); }
          );

          /* Dynamic navigation system(s) */
          /*
          $('#onAirPersonas').jcarousel( { 
            buttonPrevHTML:'<div><img src="/images/carouselLeft.gif" width="61" height="107" /></div>',
            buttonNextHTML: '<div><img src="/images/carouselRight.gif" width="61" height="107" /></div>',
            scroll: 2,
            animation: 'slow'
            });
          */
          $('div.scrollable').scrollable( { next: 'div.next', prev: 'div.prev' } );
          $('div.prev').mouseover( function () { $(this).css({ backgroundImage: 'url(/images/carouselLeft_over.png)'}); });
          $('div.prev').mouseout( function () { $(this).css({ backgroundImage: 'url(/images/carouselLeft.png)'}); });
          $('div.next').mouseover( function () { $(this).css({ backgroundImage: 'url(/images/carouselRight_over.png)'}); });
          $('div.next').mouseout( function () { $(this).css({ backgroundImage: 'url(/images/carouselRight.png)'}); });

//          $('div.tweetScroll').scrollable( { vertical:true } );

          $('.contentBox a:first').eq(0).trigger('click');

          if ($('.pageContent').length) if ($('.pageContent').height()<300) $('.pageContent').css( { height: '420px' });

          var listenLiveCode='';
          $('#showSongInfo').mouseover(
            function () {
                listenLiveCode=$('#listenLive').html();
                $('#listenLive').html('<img src="/images/menu_listen.jpg" />');
                $('#songInfoBox').html('<img src="/images/loading.gif" class="center pad8 marg8" />');
                $('#songInfoBox').show();
                $('#songInfoBox').load("/nowplaying.php?bustCache="+Math.random());
            }
          );

          $('#showSongInfo').mouseout( function () { 
                $('#listenLive').html(listenLiveCode);
                hideSongInfo();
                //window.setTimeout(hideSongInfo,1000);
          });

          $('.icoBox').click(
            function () {
                window.location.href=$(this).find('a').eq(0).attr('href');
            }
          );

          $('.subscribeBox').click(
            function () {
              //$(this).html("<p>Signup for Newsletters</p><form class='pad8 marg8' method='post' action=''><input type='text' name='email' onclick='this.value=\"\"' value='Enter your email' /><input type='submit' value='subscribe'></form>");
              $('.subscribeBox').unbind('click');
              return false;
            }
          );
	}
);

function hideSongInfo() { $('#songInfoBox').hide(); }

