/** default javascript file**/
$(document).ready(function(){
     $('.first').hover(
         function() {

		    $(this).find('ul').eq(0).css('visibility', 'visible');
		    var img = $('img', $(this));
		    if (img.attr('src').indexOf('_over_') != -1) {
			  return;
		    }
		    else {
		     var links = img.attr('src').split('_');
             img.attr('src', links[0] + '_over_' + links[1]);
		    }
		 },
         function() {
			 $(this).find('ul').eq(0).css('visibility', 'hidden');

			 var img = $('img', $(this));
		     if (img.attr('src').indexOf('_over_') == -1) {
			   return;
		     }
		     else {
		       var links = img.attr('src').split('_over_');
               img.attr('src', links[0] + '_' + links[1]);
		     }
	     }
     )

     // enter key
	 $('#search input').keydown(function(e) {
        if (e.keyCode == 13) {
             moduleSearch();
        }
     });


     $('input:checkbox[name=checkall]').click(function() {

          if ($(this).checked) {
              $(this).checked = false;
          }
          else {
            $(this).checked = true;
          }

          $.each($('table tbody input:checkbox'),function(i, e) {

              if (e.checked) {
                e.checked = false;
              }
              else {
                e.checked = true;
              }




          });
     });



     $(window).scroll(function() {

           adjustPositionSidebar();

     });

     $('.sidebar ul li .del').live('click', function() {

        $(this).parent().parent().remove();

        removeMyQuote($(this).attr('ref'));

     });

     $('.arrow', '.sidebar').live('click',function() {

         $(".sidebar .item_list").animate({width:'toggle'},350);

         $('.sidebar .arrow').switchClass('show', 'hide');

         $('.sidebar .link .btn').toggle();

         /*$(".sidebar").css('width','330px');

         $(".sidebar .item_list").css('width','300px');

         setTimeout(function() {

         var margin_top = ($(".sidebar").height()/2) - 36;

         $('.sidebar .arrow').css('margin-top', margin_top);
         }, 500);*/

         setTimeout(function() {

            adjustPositionSidebar();

         }, 500);

     });

     $('.sidebar .link .cleanall').live('click', function() {

            if (confirm('Are you sure?')) {


               $.map($('.sidebar ul li .del'), function(item, i) {

                     var product_id = $(item).attr('ref');

                     removeMyQuote(product_id);


               });


               $('.sidebar .item_list').html('');

               $('.sidebar .item_list').hide();

               $('.sidebar .link .btn').hide();

               adjustPositionSidebar();

            }


     });

     $('.sidebar a.close').click(function() {

          var sidebar = $(this).parent().parent();
          sidebar.hide();
          var COOKIE_NAME = sidebar.attr('id');
          var date = new Date();
          date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
          $.cookie(COOKIE_NAME, '0', { path: '/', expires: date });

     });

     loadQuoteList();

     adjustPositionSidebar();


     if ('undefined' != typeof(initSection)) {

         initSection();

     }

     if ($('div.success')[0] != null) {


        $('div.success').fadeOut(3000);
     }
});



function moduleSearch() {
  url = 'index.php?route=product/search';

  var filter_keyword = $('#filter_keyword').attr('value')

  if (filter_keyword) {
    url += '&keyword=' + encodeURIComponent(filter_keyword);
  }

  var filter_category_id = $('#filter_category_id').attr('value');

  if (filter_category_id) {
    url += '&category_id=' + filter_category_id;
  }

  location = url;
}

function addToCart(product_id) {
    // alert(product_id);
    $("input[name=product_id]").val(product_id);

    $('#quick_add_cart_form').submit();

}

var init_done = false;

function addToQuote(product_id) {
    // alert(product_id);

    $('.sidebar .link .btn').show();

    $.ajax({
      url: 'index.php?route=account/quote/ajax_quote',
      type: "POST",
      data: "product_id=" + product_id,
      dataType: "json",
      success: function(data) {

         parseQuoteList(data);

         $('.item_list').show();

      }
    });

}

function removeMyQuote(product_id) {
    $.ajax({
      url: 'index.php?route=account/quote/remove_quote_ajax',
      type: "POST",
      data: "product_id=" + product_id,
      dataType: "json",
      success: function(data) {

         //parseQuoteList(data);

      }
    });

    adjustPositionSidebar();
}

function loadQuoteList() {

    $.getJSON('index.php?route=account/quote/ajax_quote', function(data){


         parseQuoteList(data);

    });

}

function parseQuoteList(data) {

     if (undefined != data.quotes) {


             if ($('#quote-sidebar')[0] == null) {

                var sidebar_html  = '<div id="quote-sidebar" class="sidebar">';
                    sidebar_html += '<div class="link">';
                    sidebar_html += '<a class="title" href="'+data.my_quote_link+'">My Quote List</a>';
                    sidebar_html += '<a class="btn cleanall">Clean All</a><a class="btn">Save It.</a>';
                    sidebar_html += '</div>';
                    sidebar_html += '<div class="spacer"></div>';
                    sidebar_html += '<div class="item_list"></div>';
                    sidebar_html += '<div class="arrow show"></div>';
                    sidebar_html += '</div>';

                $(sidebar_html).prependTo($("#container"));

                $('#quote-sidebar').css("left","0px").addClass("sidebar-pos-left");

             }

             var html = '<ul>';

             $.each(data.quotes, function(i, item) {

                 html += '<li><div class="image_wrapper">';
                 html += '<span class="del" title="remove from my quote list" ref="'+item.product_id+'">X</span>';
                 html += '<a href="'+item.href+'"><img src="'+item.image+'" alt= "' + item.model + '" title="'+item.model+'" ></a>';
                 html += '</div>';
                 //html += '<div><span class="model">'+item.model+'</span></div>';
                 html += '</li>';

             });

             html += '</ul>';



             $('#quote-sidebar .item_list').html(html);




             adjustPositionSidebar();

      }
}

function openOlServicePopup(url) {

    newwindow = window.open(url,'name','height=600,width=650');

    if (window.focus) {
		newwindow.focus()
	}
    return false;
}




function adjustPositionSidebar() {

     var name = ".sidebar";

     var sidebar_top_limit = 100;
     var sidebar_top_margin = 20;
     var sidebar_slide_duration = 500

     offset = $(document).scrollTop() + sidebar_top_margin;

     if(offset < sidebar_top_limit) {

        offset = sidebar_top_limit;
     }

     //$(name).animate({top:offset},{duration:sidebar_slide_duration,queue:false});

     var last_right_top = 0;

     $.map($('.sidebar-pos-right'),function(e, i) {

        var $e = $(e);

        var top_height = offset + last_right_top;

            //alert(i+ ' - ' + offset +' - ' + $e.height());

        $e.animate({top:top_height},{duration:sidebar_slide_duration,queue:false});

        last_right_top += ($e.height() + 40);

     });

     var last_left_top = 0;

     $.map($('.sidebar-pos-left'),function(e, i) {

        //alert(i);

        var $e = $(e);


        var top_height = offset + last_left_top;

            //alert(i+ ' - ' + offset +' - ' + $e.height());

        $e.animate({top:top_height},{duration:sidebar_slide_duration,queue:false});

        last_left_top += ($e.height() + 40);

     });

     //alert($(name).height());

    // var margin_top = ($(name).height()/2) - 36;

    // $('.arrow', name).css('margin-top', margin_top);

}

function copyQuoteToCart() {

   $.map($('input[type=checkbox]:checked'), function(e, i) {

       if ('checkall' == e.name) {
          return;
       }

    var value = e.name.substring(7, e.name.length-1);
    $.ajax({
      url: 'index.php?route=checkout/cart/ajax_add',
      type: "POST",
      data: "product_id=" + value,
      dataType: "json",
      success: function(data) {

         //parseQuoteList(data);

      }
    });
      //alert(e.value);
   });

}

function copyCartToQuote() {

   $.map($('input[type=checkbox]:checked'), function(e, i) {


       if ('checkall' == e.name) {
          return;
       }

       var value = e.name.substring(7, e.name.length-1);

       //alert(value);

      /**/
    $.ajax({
      url: 'index.php?route=account/quote/ajax_quote',
      type: "POST",
      data: "product_id=" + value,
      dataType: "json",
      success: function(data) {

         //parseQuoteList(data);

      }
    });
    /**/
      //alert(e.value);
   });

}


var _EMAIL_FILTER   = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

var _PHONE_FILTER   = /^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$/;

var _ZIPCODE_FILTER = /^(?!0{5})(\d{5})(?!-?0{4})(-?\d{4})?$/;

