$(document).ready(function(){
    	
    $("#loginform").submit( function() { $("input[type=submit]").attr("value", "Sending...");})
		
		$('form input.highlight').eq(0).focus();

		var searchvalue = $('.search').val();		
		$('.search').focus(function() {
			$(this).val("");
		});
		
		$('.search').blur(function() {
			if($(this).val() == "") {
				$(this).val(searchvalue);
			}
		});
		
		$('#InstallSearchPlugin').click(function() {
		  window.external.AddSearchProvider($(this).attr('rel'));
		});
		
		$("div.data table").tablesorter(); 
		
	  $('small.tip').eq(0).show();		
		$('fieldset input').focus(function() {
		  $('small.tip').hide();		
		  $(this).next('small.tip').show();
		});
		
		$('.small_tabs li', '#column_right').click(function() {
		  var rel = $(this).children().attr('rel');
			$('#column_right div').hide();
			$('.small_tabs li').removeClass('selected');
			$(this).addClass('selected');
			$(rel).show();
		});
		
		var partcode = $('#retailer_1').attr('rel');
		if (partcode != '') {
		  $('#retailer_1').load("/scrape.php?partcode="+partcode+"&retailerID=1", function() {$("div.data table").tablesorter();});
		  $('#retailer_2').load("/scrape.php?partcode="+partcode+"&retailerID=2", function() {$("div.data table").tablesorter();});
		  $('#retailer_3').load("/scrape.php?partcode="+partcode+"&retailerID=3", function() {$("div.data table").tablesorter();});
		  $('#retailer_4').load("/scrape.php?partcode="+partcode+"&retailerID=4", function() {$("div.data table").tablesorter();});
		  $('#retailer_5').load("/scrape.php?partcode="+partcode+"&retailerID=5", function() {$("div.data table").tablesorter();});			
		}					
		
	//select all the a tag with name equal to modal
	$('a[name=modalstuff]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn('fast');	
		//$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
	  $(id).html('<iframe border="0" src="/ajax/part_edit.php?PartCode=NTC9027" height="100%" width="100%"></iframe>');		
		$(id).append('<a href="#" class="close">X</a>');
	  
		//if close button is clicked
	 		$('.window .close').click(function (e) {
			  //Cancel the link behavior
				e.preventDefault();
				$('#mask, .window').hide();
			});						
	
		//transition effect
		$(id).fadeIn('fast'); 
	
	});
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
		
		//$('.actions').toggle(
		//function() {$('.actions').next('ul').hide();$('.actions').removeClass('pressed');$(this).next('ul').show();$(this).addClass('pressed');return false;}, 
		//function () {$(this).next('ul').hide();$(this).removeClass('pressed');return false;});
		
		var height;
		$('.question').hover(
		function (e) {
						 $desc = $(this).attr('rel');
						 $('#tooltip').html($desc);
						 offsetX = $('#tooltip').width();
						 height = $('#tooltip').height();						 
						 offsetX = offsetX/2;
						 offsetY = -1*(40 + height);
						 $('#tooltip').css('top', e.pageY+offsetY+'px').css('left', e.pageX-offsetX+'px'); 
         		 var fade = $('#tooltip');
		           if (fade.is(':animated')) {
			            fade.stop().fadeTo(500, 1);
        		   } else {
                  fade.fadeIn(500);
        		   }	 
						 }, 
		function () {
		         var fade = $('#tooltip');
		           if (fade.is(':animated')) {
			            fade.stop().fadeTo(500, 0);
		           } else {
			            fade.fadeOut(500);
		           }
						 //$('#tooltip').html('');
						 }
		);
 
    $('.expand').click(function () {
		   
		   if (!$(this).hasClass('table-select')) {
					$('.expand').removeClass('table-select');
					$('.data').hide();
					
					$(this).next('.data').eq(0).show();
					$(this).addClass('table-select');
			 }										
		});		
 });

var timeout = 100;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
//	ddmenuitem = $(this).closest("li").find('ul').eq(0).css('visibility', 'visible');}
	ddmenuitem = $(this).children('ul').css('visibility', 'visible');}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

$(document).ready(function()
{	$('#topnav li a').bind('mouseover', jsddm_open);
	$('#topnav li ul').bind('mouseout',  jsddm_timer);
});

document.onclick = jsddm_close;
 
