// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});


 function add_product_list(pid){
    $("#spinner_p").show();
    $.post("/products/"+pid+"/add_to_list", {}, null, "script");
 }

 function remove_product_list(pid) {
    $("#spinner_p").show();
    $.post("/products/"+pid+"/remove_from_list", {}, null, "script");
 }

$(document).ready(function() {

    $('div#product_image').mouseenter(function(){
       $('div#zoom_window').show();}).mouseleave(function(){
        $('div#zoom_window').hide();
    });

//    $('div#product_image').click(function(){$('div#zoom_window').show();});

    $('td.shortlist_view').each(function()
    {
      $(this).qtip({
        content:{
        text: $(this).attr('title'),
        title: 'Wishlist Information'
        },
        position:{target: 'mouse', adjust:{ mouse: false}},
        hide: {
            fixed: true
        },
        style: {
            classes: 'ui-tooltip-rounded, ui-tooltip-shadow' // Give it some style
        }
      });
    });

    $('td.warranty_view').each(function()
    {
      $(this).qtip({
        content:{
        text: $(this).attr('title'),
        title: 'Warranty Information'
        },
        position:{target: 'mouse', adjust:{ mouse: false}},
        hide: {
            fixed: true
        },
        style: {
            classes: 'ui-tooltip-tipped, ui-tooltip-shadow' // Give it some style
        }
      });
    });

    $('a#change_location').click(function(){
        $('div#location_light_box').dialog('open');
        return false;
    });

    $(function() {
        $( "#location_light_box" ).dialog({
            modal: true,
            width: 400,
            resizable: false,
            autoOpen: false,
        open: function() {
            $(".location_text").val('Suburb, town or postcode here...');
            $(".location_text").click(function() {
                $(this).val('');
                $(".location_text").unbind('click');
            });
            }
        });
    });

    $('.track_as_event').click(function() {
        var classname = $(this).attr('class');

        var str2 = $.map(classname.split(" "),function(n){return n});
        var str3 = str2[1];
        var event_attributes = $.map(str3.split('-'),function(n){return n});
        var category = event_attributes[0];
        var action = event_attributes[1];
        var label = event_attributes[2];

        if (!category.blank && !action.blank && !label.blank) {
            _gaq.push(['_trackEvent', category, action, label]);
        }
    });

    $("button.modal_popup").click(function() {
      var id = $(this).val();
      $(id).dialog("open");
      return false;
    });

    $("a.modal_popup").click(function() {
      var id = $(this).attr("href");
      $(id).dialog("open");
      return false;
    });

    $(function() {
		$('input:submit').button();
        $('button').button();
        $('a.button').button();
        $('input:button').button();
	});

    $('.reveal_tab').click(function(){
        var link = $(this).attr('href');
        $('#tabs').tabs('select', link);
        return false;
    });

    $("form.submit_with_ajax").submit(function(){
        $(this).find('span.spinner').css('display', 'inline-block');
        $(this).find('p.error').remove();
        $(this).find('input.error').removeClass('error');
        $.post($(this).attr('action'), $(this).serialize(), null, 'script');
        return false;
    });

    $(".click_with_ajax").click(function(){
        $.get($(this).attr('href'));
        return false;
    });

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

    $("form.set_reason_code").submit(function(){
        var reason = $('meta#keywords').attr("content");
        $('input#set_reason').val(reason);
    });


});
