$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};		





$(function(){


	if (prodSite==true)
	{
		$('#IW_DATABASE1').val('ACDiseaseCategories');
		$('#IW_DATABASE2').val('ACAlgorithmTopics');
		//alert($('#IW_DATABASE1').val());
	 }
	else{
		$('#IW_DATABASE1').val('ACCert');
		$('#IW_DATABASE2').val('ACCertAlgorithmTopics');
		//alert($('#IW_DATABASE1').val());

	}

 $('#getunsubscribeForm').click(function(){
		$('#subscription_form').hide();
		$('#unSubscribe').show();


	});


//console.log('s');
$(".tabs").tabs();

var crntFnt = $.cookie('userFontSize');
$('.TopicSection').css('font-size', crntFnt);





 var tabs = $('.tabs'),
    
    // This selector will be reused when selecting actual tab widget A elements.
    tab_a_selector = 'ul.ui-tabs-nav a';
  
  // Enable tabs on all tab widgets. The `event` property must be overridden so
  // that the tabs aren't changed on click, and any custom event name can be
  // specified. Note that if you define a callback for the 'select' event, it
  // will be executed for the selected tab whenever the hash changes.
  tabs.tabs({ event: 'change' });
  
  // Define our own click handler for the tabs, overriding the default.
  tabs.find( tab_a_selector ).click(function(){
    var state = {},
      
      // Get the id of this tab widget.
      id = $(this).closest( '.tabs' ).attr( 'id' ),
      
      // Get the index of this tab.
      idx = $(this).parent().prevAll().length;
    
    // Set the state!
    state[ id ] = idx;
    $.bbq.pushState( state );
  });
  
  // Bind an event to window.onhashchange that, when the history state changes,
  // iterates over all tab widgets, changing the current tab as necessary.
  $(window).bind( 'hashchange', function(e) {
    
    // Iterate over all tab widgets.
    tabs.each(function(){
      
      // Get the index for this tab widget from the hash, based on the
      // appropriate id property. In jQuery 1.4, you should use e.getState()
      // instead of $.bbq.getState(). The second, 'true' argument coerces the
      // string value to a number.
      var idx = $.bbq.getState( this.id, true ) || 0;
      
      // Select the appropriate tab for this tab widget by triggering the custom
      // event specified in the .tabs() init above (you could keep track of what
      // tab each widget is on using .data, and only select a tab if it has
      // changed).
      $(this).find( tab_a_selector ).eq( idx ).triggerHandler( 'change' );
    });
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
$(window).trigger( 'hashchange' );


$('#tabs a').click(function(){
var topic=$('#short_name').text();
var tab=$(this).text(); 
var topictab= topic+"_"+tab;
s.prop7=topictab; 
s.linkTrackVars='prop7'; 
s.tl(this,'o');
//console.log(s.prop7);
});

	$('#generalcontentPage > h2').next().css('margin-top','0');

	$('#subscription_button').click(function(){
	$('#subscription_form').load('/resources/updateForm.html');
	});
 

		$('#feedbackFormResults').hide();
		$('#comments').html("Click here to comment");
		$('#restoftheForm').hide();
		$('#comments').click(function(){
		$('#topic').remove();	 
		$('<input type="hidden" id="topic" name="topic" value="' + $('#short_name').html() + '" />').appendTo($('#feedbackForm'));
        $(this).height($(this).height() != 100 ? 100 : 33 );
		$(this).text($(this).text() == 'Click here to comment' ? '' : 'Click here to comment');
		$('#restoftheForm').toggle();
		});

		$('#sendfeedbackForm').click(function(){
			var topic = $('#topic').val();
			var name = $('#name').val();
			var email = $('#email').val();
			var cred = $('#cred').val();
			var inst = $('#inst').val();
			var spec = $('#spec').val();
			var comments = $('#comments').val();

			$.ajax({
				url: '/resources/sendFeedback.php',
				type: 'POST',
				data: 'feedback=1&topic=' + topic + '&name=' + name + '&email=' + email + '&cred=' + cred + '&inst=' + inst + '&spec=' + spec + '&comments=' + comments,
					success: function(r){
					$('#comments').hide();
					$('#restoftheForm').hide();
					//$('#feedbackForm').append(r);
					//console.log(r);
					if ($('#feedbackFormResults').length == 1)
					{
					$('#feedbackFormResults').append(r);				
					}
					//console.log($('#colleagueFormResults').length);
					$('#feedbackFormResults').show();
					$('#feedbackFormResults').fadeOut(4000);
					//$('#feedbackFormResults').html('');



					}
				});
	return false;
	});

	$('.yname-missing').hide();
	$('.cname-missing').hide();
	$('.cemail-missing').hide();
	$('#colleagueForm').hide();
	$('#colleagueFormResults').hide();
	$('#emailtoColleague').hover().css('cursor', 'hand');

	$('#emailtoColleague').click(function(){

		$('#colleagueForm').toggle();
		
				

		});


$('#sendColleagueEmail').click(function(){
		var url = window.location.href;
		var curl = url.replace(/#+$/, "");

				$('#colleagueFormResults').hide();
			//statements to validate the form	
				var topic = $('#short_name').text();
				var yname = $('#yname').val();
				var cname = $('#cname').val();
				var cemail = $('#cemail').val();
				var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(cemail)) {
			$('.cemail-missing').show();
		} else {$('.cemail-missing').hide();}
		if (yname == "") {
			$('.yname-missing').show();
		} else {$('.yname-missing').hide();}	
		if (cname == "") {
			$('.cname-missing').show();
		} else {$('.cname-missing').hide();}		
		if ((yname == "") || (!filter.test(cemail)) || (cname == "")){
			return false;
		} 
		if ((yname != "") && (filter.test(cemail)) && (cname != "")) {
									$.ajax({
										url: '/resources/emailTopic.php',
										type: 'POST',
										data: 'feedback=1&url=' + curl + '&topic=' + topic + '&yname=' + yname + '&cname=' + cname + '&cemail=' + cemail,
										success: function(r){
											$('#colleagueForm').hide();
									
											if ($('#colleagueFormResults').length == 1)
											{
												$('#colleagueFormResults').append(r);				
											}
											$('#colleagueFormResults').show();
											$('#colleagueFormResults').fadeOut(4000);
											
											$('#colleagueFormResults').html('');

											}
										
											});
		
					return false;
		}
					});

$("span.externalLink a").each(function(){
	//$(this).append('&nbsp;<img src="' +CLT_imageBase+ 'remote_icon.gif">')
});


$("span.externalLink a").click(function(){
	window.open(this.href);
	return false;
});

 






	//$('span.expandingItemLink').next('div').hide();
	$('span.expandingItemLink').toggle(function(){ 
		 $(this).find('img').attr('src', CLT_imageBase+ 'minus.gif');
		 $(this).next('div').toggle();
		}, function() {  
			 $(this).find('img').attr('src', CLT_imageBase + 'plusik.gif');
			$(this).next('div').toggle();
		})
       
$('.noBullet').css('margin-left','-16px');
$('.expandingItemLink').children('img').css('vertical-align','middle');
		
 $('#textSize a,.expandingItemLink').hover(function() {
  $(this).addClass('handcursor');
}, function() {
  $(this).removeClass('handcursor');
});

 





$('#showRefs').click(function(){
	if($('#revs_data').is(':visible'))
	{
	$('#revs_data').hide();
	}
	$('#refs_data').toggle();
})
	
$('#showRevs').click(function(){
	if($('#refs_data').is(':visible'))
	{
	$('#refs_data').hide();
	}
	$('#revs_data').toggle();
})

$('.closerefsLayer').click(function(){
	$(this).parent().parent().parent().toggle();
})



		xOffset = 10;
		yOffset = 30;

$("a.preview").hover(function(e){

		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	


});

 function changeFontSize(s){
  
   var domain = document.domain;
	var c = $('.TopicSection');
// console.log(c);
	if(s == 'normal'){
    c.css('font-size','1.0em');
	$.cookie('userFontSize', '1.0em', { expires: 365, path: '/', domain: domain });
	}
		if(s == 'medium'){
    c.css('font-size','1.2em');
	$.cookie('userFontSize', '1.2em', { expires: 365, path: '/', domain: domain });
	}
		if(s == 'large'){
    c.css('font-size','1.4em');
	$.cookie('userFontSize', '1.4em', { expires: 365, path: '/', domain: domain });
	}

}