var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function mainmenuDD_open()
{
    mainmenuDD_canceltimer();
    mainmenuDD_close();
    ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function mainmenuDD_close()
{  
    if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function mainmenuDD_timer()
{  
    closetimer = window.setTimeout(mainmenuDD_close, timeout);
}

function mainmenuDD_canceltimer()
{  
    if(closetimer)
    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

$(document).ready(function()
{
    $('#mainmenuDD > li').bind('mouseover', mainmenuDD_open)
    $('#mainmenuDD > li').bind('mouseout',  mainmenuDD_timer)
});
document.onclick = mainmenuDD_close;


$(document).ready(function()
{
    //Pagination controller
    $('#view_link_pagination').click(function(){
        $('.pagination_control').fadeOut(function(){
              $('.pagination').fadeIn();
        });
    });
    
    $('#view_link_pagination_bottom').click(function(){
        $('.pagination_control_bottom').fadeOut(function(){
              $('.pagination_bottom').fadeIn();
        });
    })


    
    $("a[rel=image_group]").fancybox({
            'titleShow'		: false
    });

    $('#del_inline').fancybox({
            'titleShow'		: false
    });

    if($('#datePicker').length)
    {
        $('#datePicker').simpleDatepicker();
    }
});


function lookup(inputString) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
        $('.fadeHolder').fadeOut();
    } else {
        $.post("quicksearch.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                //fade out main site div
                $('.fadeHolder').fadeIn();
                $('.suggestionsBox').fadeIn();
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
            else
            {
                $('.fadeHolder').fadeOut();
                 $('.suggestionsBox').fadeOut();
            }
        });
    }
}
function hideThis()
{
    $('.suggestionsBox').fadeOut();
    $('.fadeHolder').fadeOut();
}

var selected;

function selectRowEffect(object, buttonSelect) {
  if (!selected) {
    if (document.getElementById) {
      selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.className = 'moduleRow';
  object.className = 'moduleRowSelected';
  selected = object;

// one button is not an array
  if (document.checkout_address.shipping[0]) {
    document.checkout_address.shipping[buttonSelect].checked=true;
	    calHeader = document.getElementById('calendarTitle');
		calBody = document.getElementById('calendarBody');
		nonCalMessage = document.getElementById('nonCalMessage');

		if (document.checkout_address.shipping[buttonSelect].value=='date_date')
		{
			calHeader.style.display = '';
			calHeader.style.visibility = 'visible';

			calBody.style.display = '';
			calBody.style.visibility = 'visible';

			nonCalMessage.style.display = 'none';
			nonCalMessage.style.visibility = 'hidden';
		} else
		{
			calHeader.style.display = 'none';
			calHeader.style.visibility = 'hidden';

			calBody.style.display = 'none';
			calBody.style.visibility = 'hidden';

			nonCalMessage.style.display = '';
			nonCalMessage.style.visibility = 'visible';
		}
  } else {
    document.checkout_address.shipping.checked=true;
	    calHeader = document.getElementById('calendarTitle');
		calBody = document.getElementById('calendarBody');

		if (document.checkout_address.shipping.value=='date_date')
		{
			calHeader.style.display = '';
			calHeader.style.visibility = 'visible';

			calBody.style.display = '';
			calBody.style.visibility = 'visible';

			nonCalMessage.style.display = 'none';
			nonCalMessage.style.visibility = 'hidden';
		} else
		{
			calHeader.style.display = 'none';
			calHeader.style.visibility = 'hidden';

			calBody.style.display = 'none';
			calBody.style.visibility = 'hidden';

			nonCalMessage.style.display = '';
			nonCalMessage.style.visibility = 'visible';
		}
  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

$(document).ready(function()
{
    if( $('.hover-star').length)
    {
        $('.hover-star').rating({
        focus: function(value, link){
        var tip = $('#hover-test');
        tip[0].data = tip[0].data || tip.html();
        tip.html(link.title || 'value: '+value);
        },
        blur: function(value, link){
        var tip = $('#hover-test');
        $('#hover-test').html(tip[0].data || '');
        }
        });
    }
});

function saveRating(products_id, customers_id)
{
   //Get form values
   var name = $('input:text[name=displayName]').val();
   var review = $('textarea[name=review]').val();
   var rating = $('input:radio[name=prod_rating]:checked').val();
   var date = $('input:text[name=date]').val();
   var error = false;
   var message = '';

   
   if(!rating)
   {
       error = true;
       $('.errorTestimonial').html('Please select a rating for this product');
       $('.errorTestimonial').fadeIn();
   }

   if(name.length == 0)
   {
       error = true;
       $('.errorTestimonial').html('Please enter a name');
       $('.errorTestimonial').fadeIn();
   }

   if(review.length == 0)
   {
       error = true;
       $('.errorTestimonial').html('Please enter a review');
       $('.errorTestimonial').fadeIn();
   }

   if(error == false)
   {
    $(document).ready(function()
    {
        $('.errorTestimonial').fadeOut();
      $.post("handle_testimonial.php", { product: products_id, customer: customers_id, rname:name, rreview: review, rrating:rating, rdate:date } );

        $('.review_form').fadeOut(function(){
            $('.review_pending').fadeIn();
        });
    });
   }
}
