// allow jQuery to work with other libraries
jQuery.noConflict();

function cluetipClose() {
	jQuery('#cluetip-close').click();
}

jQuery(document).ready(function($) {
	
/* global modules */
	
	// global navigation
	if($.browser.msie && parseInt($.browser.version) <= 6) {
		
	}
	
	// clear search field
	$("input#keyword").clearField();
	
	// print
	$("#print a").click( function() {
		window.open( $(this).attr('href') );  
		return false;
	});
	
	// feedback layer
	$('#cancelFeedback').click(function() {
		cluetipClose();
	});
	$('#sendFeedback').click(function() {
		$feedbackText = $('#feedbackThanks').html();
		$(this).parent().parent().html($feedbackText);
	});
	$('.feedbackClose').click(function() {
		cluetipClose();
	});
	$('#lookingFor, #emailAddressFeedback').focus(function() {
		$(this).select();
	});
	$('.feedback').cluetip({position:'fixed', leftOffset: '-20', topOffset: '32', cluetipClass: 'feedback', width: '242px'});

/* homepage */

	if ($("body").is("#homepage")) {
		// Coda slider
		$("body#homepage #carousel").codaSlider();
	}

/* guest_* */

	if ($("body").is("#guest")) {
		// Coda slider
		$("body#guest #carousel").codaSlider();
		// clueTip
		$("body#guest a#loginLink, body#guest a#whyNeededLink, body#guest a#passwordGuidelinesLink").cluetip();
		// (just in case...)
		$("body#guest #whyNeeded, body#guest #passwordGuidelines").hide();
			
		// hide on page load
		$("body#guest #utilities form#forgotPasswordForm, body#guest #utilities form#securityQuestionForm, body#guest #confirmation").hide();
		
		// disable form elements on page load
		$("body#guest #sidebar form#registerForm input:gt(0), body#guest #sidebar form#registerForm select").attr("disabled", "disabled").addClass("quiet");
		$("body#guest input#createMyAccount").removeClass("quiet");
		
		$("body#guest #find a").click(function () { 
			$("body#guest form#registerForm input, body#guest form#registerForm select").removeAttr("disabled").removeClass("quiet");
		});
		
		/* guest link triggers
		$("body#guest a.trigger").click(function() {											  
			$(this).parent().parent().hide();
			$("form#registerForm input#organizationName").focus();
		});	*/
		// "Log In" window events
		$("body#guest a#registerLink").click(function() {
			$("form#registerForm input#organizationName").focus();
		});
		// clueTip		
		var topOS = 30;
		var leftOS = -15;
		if ($.browser.mozilla) { $( function() { topOS = -60; } ); }	
		if ($.browser.safari) { $( function() { topOS = -60; leftOS = 80; } ); }
		$("a.overlay").cluetip({  
			width:            300,							
			positionBy:       'fixed',
			topOffset:        topOS,
			leftOffset:       leftOS			
		});
	}
	
/* pharmacy_landing.html */

	if ($("body").is("#pharmacy_landing")) {
		// clueTip
		$("body#pharmacy_landing a.wheresThisLink").cluetip();
		// jQuery UI tabs
		$("body#pharmacy_landing #quickRefill ul").tabs();
		if (detectMac()) {
			$("body#pharmacy_landing .ui-tabs-nav li").addClass("macFix")
		}
		// add more/less refills
		$("body#pharmacy_landing .addMore").toggle(function(){ 
			$(this).parent().next().slideDown(); 
			$(this).addClass("addLess"); 
			$(this).removeClass("addMore");
			$(this).text( $(this).attr("rel") );
		},function(){ 
			$(this).parent().next().slideUp(); 
			$(this).addClass("addMore"); 
			$(this).removeClass("addLess");
    		$(this).text( $(this).attr("title") );
		})

		$("body#pharmacy_landing #addAnother").click(function () { 
			tb_remove();
			$("input#drugName").val("").focus();
		});
		// show/hide fields
		$("body#pharmacy_landing input#transferRx").click(function(){
			$("body#pharmacy_landing #transferFields, body#pharmacy_landing #shareRequestFields, body#pharmacy_landing #dropOffFields, body#pharmacy_landing #mailFields").slideUp();
			$("body#pharmacy_landing #transferFields").slideDown();
		});
		$("body#pharmacy_landing input#shareRx").click(function(){
			$("body#pharmacy_landing #transferFields, body#pharmacy_landing #shareRequestFields, body#pharmacy_landing #dropOffFields, body#pharmacy_landing #mailFields").slideUp();
			$("body#pharmacy_landing #shareRequestFields").slideDown();
		});
		$("body#pharmacy_landing input#requestRx").click(function(){
			$("body#pharmacy_landing #transferFields, body#pharmacy_landing #shareRequestFields, body#pharmacy_landing #dropOffFields, body#pharmacy_landing #mailFields").slideUp();
			$("body#pharmacy_landing #shareRequestFields").slideDown();
		});
		$("body#pharmacy_landing input#dropOffRx").click(function(){
			$("body#pharmacy_landing #transferFields, body#pharmacy_landing #shareRequestFields, body#pharmacy_landing #dropOffFields, body#pharmacy_landing #mailFields").slideUp();
			$("body#pharmacy_landing #dropOffFields").slideDown();
		});
		$("body#pharmacy_landing input#mailRx").click(function(){
			$("body#pharmacy_landing #transferFields, body#pharmacy_landing #shareRequestFields, body#pharmacy_landing #dropOffFields, body#pharmacy_landing #mailFields").slideUp();
			$("body#pharmacy_landing #mailFields").slideDown();
		});
	}
	/* insurance_card.html */

	if ($("body").is("#insuranceCard")) {
		// Add more/less profiles
		$("body#insuranceCard .addMore").toggle(function(){ 
			$('#additionalProfiles').fadeIn('slow');
			$(this).addClass("addLess"); 
			$(this).removeClass("addMore");
			$(this).text( $(this).attr("rel") ); 
		},function(){ 
			$('#additionalProfiles').fadeOut('slow');
			$(this).addClass("addMore"); 
			$(this).removeClass("addLess");
    		$(this).text( $(this).attr("title") );
		});
		
		if ($('#profId5').val() > '0') {
			$("body#insuranceCard .addMore").click();
		};
		
	}

	// subcat and search pages
	$('ul.firstShown').css('display', 'block');

});
