/* Written by samnadine */

/* No conflict for $ variables */
var $j = jQuery.noConflict();

$j(document).ready(function(){			   
	$j(".btn-qr").click(function(){
	$j("#quick-reply").slideToggle("slow"); 
		$j(this).toggleClass("active-qr"); return false;
	});
	
});

$j(document).ready(function() {
		$j("#demo-bar").jixedbar({
			hoverOpaque: false,
			roundedCorners: false
		});
    });

(function($j) {

	// jixedbar plugin
	$j.fn.jixedbar = function(options) {
		var constants = {
				constOverflow: "hidden",
				constPosition: "fixed",
				constTop: "0px"
			};
		var defaults = {
				hoverOpaque: false,
				hoverOpaqueEffect: {enter: {speed: "fast", opacity: 1.0}, leave: {speed: "fast", opacity: 0.80}},
				roundedCorners: false, // only works in FF
				roundedButtons: true // only works in FF
			};
		var options = $j.extend(defaults, options);
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);		
		
		this.each(function() {
			var obj = $j(this);
			var $screen = jQuery(this);
			var fullScreen = $screen.width(); // get screen width
			var centerScreen = (fullScreen/2)*(1); // get screen center
			
			// set html and body style for jixedbar to work
			//$("html").css({"overflow" : "hidden", "height" : "100%"});
			$j("html").css({"height" : "100%"});
			$j("body").css({"margin": "0px", "overflow": "auto", "height": "100%"});

			// initialize bar
			$j(this).css({
				"overflow": constants['constOverflow'],
				"position": constants['constPosition'],
				"top": constants['constTop']
			});
			
			// add bar style (theme)
			$j(this).addClass("jx-bar");
			
			// rounded corner style (theme)
			if (defaults.roundedCorners) {
				$j(this).addClass("jx-bar-rounded-tl jx-bar-rounded-tr");
			}

			// button style (theme)
			$j(this).addClass("jx-bar-button");
			
			// rounded button corner style (theme)
			if (defaults.roundedButtons) {
				$j(this).addClass("jx-bar-button-rounded");
			}

			// calculate and adjust bar to center
			//marginLeft = centerScreen-($(this).width()/2);
			//$(this).css({'margin-left': marginLeft});
			
			// fix image vertical alignment and border
			$j("img", obj).css({
				"vertical-align": "top",
				"border": "#ffffff solid 0px" // no border
			});
			
			// check for alt attribute and set it as button text
			$j(this).find("img").each(function() {
				altName = "&nbsp;" + $j(this).attr('alt');
				$j(this).parent().append(altName);
			});

			// check of hover effect is enabled
			if (defaults.hoverOpaque) {
				$j(this).fadeTo(defaults.hoverOpaqueEffect['leave']['speed'], defaults.hoverOpaqueEffect['leave']['opacity']); 
				$j(this).bind("mouseenter", function(e){
					$j(this).fadeTo(defaults.hoverOpaqueEffect['enter']['speed'], defaults.hoverOpaqueEffect['enter']['opacity']);
			    });
				$j(this).bind("mouseleave", function(e){
					$j(this).fadeTo(defaults.hoverOpaqueEffect['leave']['speed'], defaults.hoverOpaqueEffect['leave']['opacity']);
			    });
			}
			
			// create tooltip container
			$j("<div />").attr("id", "__jx_tooltip_con__").appendTo("body"); // create div element and append in html body
			$j("#__jx_tooltip_con__").css({
				"height": "auto",
				"margin-top": $j(this).height() + 6, // put spacing between tooltip container and fixed bar
				"margin-left": "0px",
				"width": "100%", // use entire width
				"overflow": constants['constOverflow'],
				"position": constants['constPosition'],
				"top": constants['constTop']
			});
			
			// hover in and out event handler
			$j(".navbar-li", obj).hover(
				function () { // in/over event
					var elemID = $j(this).attr('id'); // get ID (w/ or w/o ID, get it anyway)					
					var barTooltipID = elemID + "__tooltip__"; // set a tooltip ID
					var tooltipTitle = $j(this).attr('title');
					
					if (tooltipTitle == '') { // if no 'title' attribute then try 'alt' attribute
						tooltipTitle = $j(this).attr('alt'); // this prevents IE from showing its own tooltip
					}
					
					if (tooltipTitle != '') { // show a tooltip if it is not empty
						// create tooltip wrapper; fix IE6's float double-margin bug
						barTooltipWrapperID = barTooltipID + '_wrapper';
						$j("<div />").attr("id", barTooltipWrapperID).appendTo("#__jx_tooltip_con__");
						// create tooltip div element and put it inside the wrapper
						$j("<div />").attr("id", barTooltipID).appendTo("#" + barTooltipWrapperID);
						
						// tooltip default style
						$j("#" + barTooltipID).css({
							"float": "left",
							"display": "none"
						});
						
						// theme for tooltip (theme)
						$j("#" + barTooltipID).addClass("jx-bar-button-tooltip");
						
						// set tooltip text
						$j("#" + barTooltipID).html(tooltipTitle);
	
						// fix tooltip wrapper relative to the associated button
						$j("#" + barTooltipWrapperID).css({
							"margin-left": ($j(this).offset().left - ($j("#" + barTooltipID).width() / 2)) + ($j(this).width()/2)
						});
						
						// show tooltip
						$j("#" + barTooltipID).show();
					}
				}, 
				function () { // out event
					var elemID = $j(this).attr('id'); // get ID (whether there is an ID or none)					
					var barTooltipID = elemID + "__tooltip__"; // set a tooltip ID
					var barTooltipWrapperID = barTooltipID + '_wrapper';
					$j("#" + barTooltipID).remove(); // remove tooltip element
					$j("#" + barTooltipWrapperID).remove(); // remove tooltip's element DIV wrapper
				}
			);
			
			// click on Stick, by samnadine
			$j('.jixedbar-stick').click(function() {
 			$j("#demo-bar").css("position", "relative");
			$j("#wrapper-n").css("margin-top", "0px");
			$j(".jixedbar-stick").css("display", "none");
			$j(".jixedbar-unstick").css("display", "block");
			$j.cookie('fhnavbar', 'stickied');
			});
			
			$j('.jixedbar-unstick').click(function() {
  			$j("#demo-bar").css("position", "fixed");
			$j("#wrapper-n").css("margin-top", "35px");
			$j(".jixedbar-stick").css("display", "block");
			$j(".jixedbar-unstick").css("display", "none");
			$j.cookie('fhnavbar', 'unstickied');
			});
			// Stick function cookie, by samnadine
			var barStick = $j.cookie('fhnavbar');
			if (barStick == 'stickied') {
				$j("#demo-bar").css("position", "relative");
				$j("#wrapper-n").css("margin-top", "0px");
				$j(".jixedbar-stick").css("display", "none");
				$j(".jixedbar-unstick").css("display", "block");
			};
			
			
			// News rotator, by samnadine
			$j(function() {
				 $j('#navbar-news').innerfade({   
     			 speed: 'slow',   
				 timeout: 4000,   
				 type: 'sequence',   
				 containerheight: '14px'  
				 });  
			});

			

			// fix PNG transparency problem in IE6
			if ($j.browser.msie && ie6) {
				$j(this).find('li').each(function() {
					$j(this).find('img').each(function() {
						imgPath = $j(this).attr("src");
						altName = $j(this).attr("alt");
						srcText = $j(this).parent().html();
						$j(this).parent().html( // wrap with span element
							'<span style="cursor:pointer;display:inline-block;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgPath + '\');">' + srcText + '</span>' + altName
						);
					});
					$j(this).find('img').each(function() {
						$j(this).attr("style", "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);"); // show image
					})
				});
			}
			
			/**
			 * To-do:
			 * 	1. Element click event:
			 * 		$("li", obj).click(function() {
			 * 			// event handler
			 * 		});
			 */
			
		});
		
		return this;
		
	};
	
})(jQuery);
