jQuery.noConflict();

jQuery(document).ready(function() {
  jQuery("table#rates tr:first-child").before("<tr><td></td><td></td><td></td></tr>");
  jQuery("table#rates tr:last-child").after("<tr><td></td><td></td><td></td></tr>");
  jQuery("table#rates tr td:last-child").css("border", 0);

  // for mock up purposes only
  jQuery("#menu_main a").click(function() {
    jQuery("#menu_main li a").removeClass("selected");
    jQuery(this).addClass("selected");
  });
  
  
  // Phone case
  var phone=["iphone", "blackberry", "android", "palmpre"];
  // Init
  jQuery("#"+phone[0]).css("color", "#f5af37");
  jQuery("#"+phone[0]).css("paddingBottom", "32px");
  jQuery("#"+phone[0]+"-info").show();
  jQuery("#"+phone[1]+"-info").hide();
  jQuery("#"+phone[2]+"-info").hide();
  jQuery("#"+phone[3]+"-info").hide();
  jQuery("#iphone-case img").css("height", "47px");
  jQuery("#"+phone[0]).prev().css("height", "67px");
  jQuery("#"+phone[0]).next().css("height", "67px");
  
  //Change
  for(var i in phone){
	  i = parseInt(i);

	  jQuery("#"+phone[i]).click(function(){
		  // Set current as active link
		  jQuery(this).css("color", "#f5af37");
		  jQuery(this).css("paddingBottom", "32px");
		  
		  // show current phone info
		  jQuery("#"+this.id+"-info").show();
		  
		  // Set other as deactive link
		  for(var j in phone){
			  j = parseInt(j);
			  if (phone[j] != this.id)
			  {
				  jQuery("#"+phone[j]).css("color", "#565656");
				  jQuery("#"+phone[j]).css("paddingBottom", "15px");
				  
				  // hive other phone info
				  jQuery("#"+phone[j]+"-info").hide();
			  }
		  }
		  
		  // Set button border's
		  jQuery("#iphone-case img").css("height", "47px");
		  jQuery(this).prev().css("height", "67px");
		  jQuery(this).next().css("height", "67px");
	  });
  }

  // Search rates
  // Remove help text
  jQuery('#rates_search').focus(function () {
	  jQuery(this).attr('value', '');
  });
  
  //Search
  jQuery('#rates_submit').click(function() {
	  var text = jQuery('input[name=rates_search]').val();

	  if (text != ''){
		  jQuery.get("/findrates/", { 
	          search: text, 
	      },
	      function(data) {
	    	  if(data == 0)
	    	  {
	    		// Not found
	    		alert("Nothing found.");  
	    	  }
	    	  else
	    	  {	
	    		  // Display Response
	    		  show_popup(data);
	    	  }
	      });
	  }
	  else
	  {
		  alert("Input text!");
	  }
	  return false;
  });
  
  // POP UP find rates
  //When you click on a link with class of poplight and the href starts with a # 
  function show_popup(data) {
	      
      var popID = jQuery('#rates_submit').attr('rel'); //Get Popup Name
      var popURL = jQuery('#rates_submit').attr('href'); //Get Popup href to define size

      //Pull Query & Variables from href URL
      var query= popURL.split('?');
      var dim= query[1].split('&');
      var popWidth = dim[0].split('=')[1]; //Gets the first query string value

      //Fade in the Popup and add close button
      jQuery('#' + popID).empty();
      jQuery('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/media/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
      jQuery('#' + popID).fadeIn().prepend('<div>'+data+'</div>');
      
      //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
      var popMargTop = (jQuery('#' + popID).height() + 80) / 2;
      var popMargLeft = (jQuery('#' + popID).width() + 80) / 2;

      //Apply Margin to Popup
      jQuery('#' + popID).css({
          'margin-top' : -popMargTop,
          'margin-left' : -popMargLeft
      });

      //Fade in Background
      jQuery('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
      jQuery('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

      return false;
  }

  //Close Popups and Fade Layer
  jQuery('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
      jQuery('#fade , .popup_block').fadeOut(function() {
          jQuery('#fade, a.close').remove();  //fade them both out
      });
      return false;
  });

 
});

