jQuery(document).ready(function($){

    //slider
    $('#slider').superBanner({width: '898',
                              height: '513',
                              speed: '5000',
                              dotPagination: false,
                              numperPagination: true,
                              pauseable: true});


    //Pretty Photo
    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_scared',
                                            opacity: '0.3'});

    $('input#email').one('click',function(){
        $(this).val('');
    })

    $('input#query').one('click',function(){
        $(this).val('');
    })

    ///Dropdown menu
    $('#menu > li').hover(function(){
        if($(this).attr('class')!='selected')
        {
            var alt = $(this).find('img').attr('alt');
            $(this).find('img').attr('src', '/images/menu-'+alt+'-hover_'+ language +'.jpg');
        }

        if($(this).find('img').attr('alt')=='products')
        {
            $(this).find('div[class|="submenu"]').show();
        }
    });

    $('#menu > li').mouseleave(function(){
        if($(this).attr('class')!='selected')
        {
            var alt = $(this).find('img').attr('alt');
            $(this).find('img').attr('src', '/images/menu-'+alt+'_'+ language +'.jpg');
        }

        if($(this).find('img').attr('alt')=='products')
        {
            $(this).find('div[class|="submenu"]').hide();
        }
    });

    //Img viewer
    $('div.thumbnail').click(function(){
        $('div.img-viewer > div.big > a.main > img').attr('src',$(this).children().attr('alt'));
        $('div.img-viewer > div.big > a.main').attr('href',$(this).children().attr('alt'));
        
        $('div.thumbnail-selected').addClass('thumbnail');
        $('div.thumbnail-selected').removeClass('thumbnail-selected');
        $(this).addClass('thumbnail-selected');
        $(this).removeClass('thumbnail');
    });

    $('div.thumbnail:first').addClass('thumbnail-selected');
    $('div.thumbnail:first').removeClass('thumbnail');

    //Product category
    $('div.product-thumbnail').hover(function(){
        $(this).append('<div class="stripe"></div>');
        $(this).find('a.name').css('color', '#cd621a');
    });

    $('div.product-thumbnail').mouseleave(function(){
        $('div.stripe').hide().remove();
        $(this).find('a.name').css('color', '#5E6E66');
    });


    /**
     * SALES AGENTS
     */
    $('select#country').change(function(){
        var countryID = $(this).val();
        if (countryID > 0)
        {
            window.location.href = "?country="+countryID;
        }
    });

    $('select#state').change(function(){
        var stateID = $(this).val();
        if (stateID > 0)
        {
            window.location.href = "?state="+stateID;
        }
    });

    // Toggle on social networks icons.
    $('.twitter a img').hover(function(){
        $(this).attr('src', '/images/layout/twitter-on.png')
    }, function(){
        $(this).attr('src', '/images/layout/twitter-off.png')
    });
    
    $('.facebook a img').hover(function(){
        $(this).attr('src', '/images/layout/facebook-on.png')
    }, function(){
        $(this).attr('src', '/images/layout/facebook-off.png')
    });


    /**
     * ADMIN AREA
     */

    // Nice even rows highlighting for the Admin Area's results sets.
    $('.admin-resultset tr:nth-child(odd)').addClass('highlighted');

    // Modal dialogs to confirm deletions.
    $("#admin-deletion-dialog").dialog({
      autoOpen: false,
      modal: true
    });

    // Bind this routine on the click event of the delete buttons.
    $('.admin-delete-button').click(function(event) {
      event.preventDefault();

      $('#admin-deletion-dialog').dialog({
        buttons : {
          'Confirm' : function() {
            $(event.target).parent().submit();
            $(this).dialog("close");
          },
          'Cancel' : function() {
            $(this).dialog("close");
          }
        }
      })
      .dialog('open');
    });

    // Generate date pickers for special fields under Zend Form's control.
    $('.admin-zend-form #begins_at').datepicker();
    $('.admin-zend-form #ends_at').datepicker();

    // Auto-refresh for finishes.
    $('div#update-finishes select#product_id').change(function(){
      var productID = $(this).val();
      if (productID > 0) {
        window.location.href = "?productID="+productID;
      }
    });

    // Auto-refresh for finishes.
    $('div#update-related select#source_id').change(function(){
      var productID = $(this).val();
      if (productID > 0) {
        window.location.href = "?productID="+productID;
      }
    });

});
