var loginSuccessUrl = '/member/';

$(document).ready(function(){
   prepareSearchForm();
   var frmSearchGps = $('#searchGps');
   
   var featureControl = $('#feature-controls li .button');
   var tabsRight = $('#tabs-right');
   var fadeInSpeed = 1500;
   var bAutoRotateFeatures = 1;
   var btnFeatureSubscribe = $('#link-featured-newsletter'), divSubscribe = $('#div-subscribe'), frmSubscribeDialog = $('#frmSubscribeDialog'), newsLetter = $('#div-newsletter-subscribe');

   var searchErrorLower = $('<div title="Search"><p>A golf course name is required to begin your search. Enter at least three characters and use simple terms to broaden results (i.e. Augusta National Golf Club: search for Augusta). To narrow the results, enter the course name and make a selection from the drop-down menu.</p></div>').
       appendTo('body').
       dialog({
            modal: true,
            autoOpen: false,
            width: 400,
            buttons: {
                'Close': function(){
                    $(this).dialog('close');
                }
            }
       });

   frmSearchGps.validate({
       errorPlacement: function(){},
       rules: {
           course_name: {
               required: true,
               minlength: 3
           }
       },
       invalidHandler: function(){
           searchErrorLower.dialog('open');
       }
   });


   $('#feature-review').fadeIn(fadeInSpeed);

   $('#feature-items').cycle({
        fx:         'fade',
        speed:       2500,
        timeout:     6000,
        pager:      '#feature-controls',
        pagerEvent: 'mouseover',
        fastOnEvent: true
   });

   $('#feature-controls a').each(function(idx, element){
      $(element).addClass($(this).transposeIndex(idx)).html('<span class="arrow"></span><span class="button"></span>');
      
      if(idx == $(this).parent('div:first').children('a').length -1) {
          $(element).addClass('last');
      }
   });

   $('#feature-controls a .button').hover(function(){
       $('#feature-items').cycle('pause');
       $(this).makeMyArrowVisible(this, 'a', 'div:first');
   });

   $('#feature-controls a .button').click(function(){
       $('#feature-items').cycle('pause');
       $(this).makeMyArrowVisible(this, 'a', 'div:first');
   });

   tabsRight.tabs();
   $('#tabs-right li').removeClass('ui-corner-top');
   $('#tabs-right li:first').removeClass('ui-state-default');
   

/**  INTERNET EXPLORER  **/
   if ($.browser.msie && parseInt($.browser.version)==7) {
       $('.ui-dialog').addClass('boxShadow');
       $('.categoryinfo .right a').after(' <b>></b>');
              
       $('span.button').boxShadow(0, 10, 5, "#000");

       $('.panel.ui-corner-all').corner('4px');
       $('.ui-corner-all').corner('4px');
       $('div.ui-corner-bottom').corner("bl 4px").corner("br 4px");
       $('div.ui-corner-top').corner("tl 4px").corner("tr 4px");

       $('#searchbox').corner('6px #fff');

       // TAB CONTROLS
       $('#tabs-right a').click(function(){
          $('#' + $(this).attr('href').replace(/#/,'')).show();
       });
       // END TAB CONTROLS

   } else if ($.browser.msie && parseInt($.browser.version) == 6){
       $('#menu li').hover(
            function () {
              $(this).addClass('hover');
            },
            function () {
              $(this).removeClass('hover');
            }
       );


   }//end msie
   
/**  END INTERNET EXPLORER  **/


   $('.nav .add-content').after('<li class="narrow">-</li>');

   /** FEATURES  **/
   featureControl.click(function(){
      $('#feature-items').cycle('pause');
      $(this).makeMyArrowVisible(this);
      $(this).changeTarget(this.id, 'feature-items', 'control', 'feature', fadeInSpeed);
   });

   var dialogSubscribe = divSubscribe.dialog({
      title: 'Subscribe',
      modal: true,
      autoOpen: false,
      width: 400,
      buttons: {          
          'Submit': function(){
              frmSubscribeDialog.submit();
          },
          'Cancel': function(){
              $(this).dialog('close');
          }
      }
   });

   btnFeatureSubscribe.click(function(e){
       e.preventDefault();
       dialogSubscribe.dialog('open');
   });
   /** END FEATURES  **/

   /**  NEWSLETTER DIALOG  **/
   newsLetter.dialog({
        title: 'Subscription Result',
        modal: true,
        autoOpen: false,
        width: 400,
        buttons: {
            'Close': function(){
                $(this).dialog('close');
            }
        }
   });

   frmSubscribeDialog.submit(function(frm){
       frm.preventDefault();
    });

    frmSubscribeDialog.validate({
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    url: '/ajax.html',
                    type: 'post',
                    data: {
                        s: 'root',
                        r: 'subscribe_email_verification',
                        email: function(){
                            return $(frmSubscribeDialog)[0].email.value;
                        }
                    }
                }
            }
        },
        messages: {
            email: {
                remote: 'This email is already in use.'
            }
        },
        submitHandler: function(frm){
            $.post('/ajax.html', {
                        s: 'root',
                        r: 'subscribe_newsletter',
                        first_name: 'N/A',
                        last_name: 'N/A',
                        email: $(frm).find('#email').val()
                }, function(data){
                        dialogSubscribe.dialog('close');
                        newsLetter.html($(data).find('content').text());
                        newsLetter.dialog('open');
                }, 'xml'
            );
            frm.reset();
        }
    });

   /**  END NEWSLETTER DIALOG  **/

});

$.fn.extend({
  changeTarget: function(myId, targetId, removePrefix, replacePrefix, fadeInSpeed) {
      var targetLis = $('#' + targetId + ' li');

      targetLis.each(function(idx, element){
         if($(element).hasClass('active')) {
             $(element)
                .fadeOut(fadeInSpeed)
                .removeClass('active');
         }
      });

      var sRegExInput = new RegExp(removePrefix, "g");
      var newId = myId.replace(sRegExInput, replacePrefix);

      $('#' + newId)
        .addClass('active')
        .fadeIn(fadeInSpeed);

  },

  makeMyArrowVisible: function(me, parentType, grandParentType) {
      var mygrandpar = $(me).parents(parentType).parents(grandParentType);
      var otherLis = $(mygrandpar).children(parentType);

      $(otherLis).each(function(idx,element){
         if( $(element).children('.arrow').hasClass('active') ) {
             $(element).children('.arrow').removeClass('active');
             $(element).children('.arrow').addClass('inactive');
         }
      });

      if( !$(me).parents(parentType + ':first').children('.arrow').hasClass('active') ){
          $(me).parents(parentType + ':first').children('.arrow').removeClass('inactive');
          $(me).parents(parentType + ':first').children('.arrow').addClass('active');
      }

  },

  transposeIndex: function(idx) {
      var aOrdinals = new Array('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth');
      return aOrdinals[idx];
  }
});
