/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * jFlow
 * Version: 1.2 (July 7, 2008)
 * Requires: jQuery 1.2+
 */
 
(function($) {

	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var randNum = Math.floor(Math.random()*11);
		var jFC = opts.controller;
		var jFS =  opts.slideWrapper;
		var jSel = opts.selectedWrapper;
		
		var timer;
		var cur = 0;
		var maxi = $(jFC).length;
		// sliding function
		var slide = function (dur, i) {
			$(opts.slides).children().css({
				overflow:"hidden"
			});
			$(opts.slides + " iframe").hide().addClass("temp_hide");
			$(opts.slides).animate({
				marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")}, 
				opts.duration*(dur),
				opts.easing,
				function(){
					$(opts.slides).children().css({
						overflow:"auto"
					});
					$(".temp_hide").show();
				}
			);
			
		}
		$(this).find(jFC).each(function(i){
			$(this).click(function(){
				if ($(opts.slides).is(":not(:animated)")) {
					$(jFC).removeClass(jSel);
					$(this).addClass(jSel);
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
				}
			});
		});	
		
		$(opts.slides).before('<div id="'+jFS.substring(1, jFS.length)+'"></div>').appendTo(jFS);
		
		$(opts.slides).find("div").each(function(){
			$(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());
		});
		
		//initialize the controller
		$(jFC).eq(cur).addClass(jSel);
		
		var resize = function (x){
			$(jFS).css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
			//opts.slides or #mySlides container
			$(opts.slides).css({
				position:"relative",
				width: $(jFS).width()*$(jFC).length+"px",
				height: $(jFS).height()+"px",
				overflow: "hidden"
			});
			// jFlowSlideContainer
			$(opts.slides).children().css({
				position:"relative",
				width: $(jFS).width()/2+"px",
				height: $(jFS).height()+"px",
				"float":"left",
				overflow:"auto"
			});
			
			$(opts.slides).css({
				marginLeft: "-" + (cur * $(opts.slides).find(":eq(0)").width() + "px")
			});
		}
		
		// sets initial size
		resize();

		// resets size
		$(window).resize(function(){
			resize();						  
		});
		
		$(opts.prev).click(function(){
			dotimer();
			doprev();
		});
		
		$(opts.next).click(function(){
			dotimer();
			donext();
		});
		var doprev = function (x){
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur > 0)
					cur--;
				else {
					cur = maxi -1;
					dur = cur;
				}
				$(jFC).removeClass(jSel);
				slide(dur,cur);
				$(jFC).eq(cur).addClass(jSel);
			}
		}
		var donext = function (x){
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur < maxi - 1)
					cur++;
				else {
					cur = 0;
					dur = maxi -1;
				}
				$(jFC).removeClass(jSel);
				slide(dur, cur);
				$(jFC).eq(cur).addClass(jSel);
			}
		}
		var dotimer = function (x){
			if(timer != null) 
			clearInterval(timer);
			timer = setInterval(function() {
				donext();
				}, opts.interval);
			}
			dotimer();
		};
	
	$.fn.jFlow.defaults = {
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		easing: "swing",
		duration: 500,
		interval: 15000,
		width: "100%",
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	};
	
})(jQuery);
/*********************
//* jQuery Multi Level CSS Menu (horizontal)- By Dynamic Drive DHTML code library: http://www.dynamicdrive.com
//* Menu instructions page: http://www.dynamicdrive.com/dynamicindex1/ddlevelsmenu/
//* Last modified: Sept 6th, 08'. Usage Terms: http://www.dynamicdrive.com/style/csslibrary/tos/
*********************/

//Specify full URL to down and right arrow images (25 is padding-right to add to top level LIs with drop downs):
var arrowimages={down:['downarrowclass', 'arrow-down.gif', 25], right:['rightarrowclass', 'arrow-right.gif']}

var jquerycssmenu={

fadesettings: {overduration: 350, outduration: 100}, //duration of fade in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})
			$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
				'<img src="'+ (this.istopheader? arrowsvar.down[1] : arrowsvar.right[1])
				+'" class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
				+ '" style="border:0;" />'
			)
			$curobj.hover(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					$targetul.css({left:menuleft+"px"}).fadeIn(jquerycssmenu.fadesettings.overduration)
				},
				function(e){
					$(this).children("ul:eq(0)").fadeOut(jquerycssmenu.fadesettings.outduration)
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}) //end document.ready
}
}

//build menu with ID="myjquerymenu" on page:
jquerycssmenu.buildmenu("myjquerymenu", arrowimages)

/**
*	@auth Logan Cai
*	@email cailongqun#yahoo.com.cn  (replace # with @)
*	@ this plugin is based on the nice javascript provided by http://www.jtricks.com/javascript/navigation/floating.html
*   @version 1.0
*	
*/
function floatingBox(box, options, index)
{
    this.targetX = -250;
    this.targetY =  10;
    this.interval = 50;
    this.menu = box;
    this.index = index;
    this.options = options;
    this.hasInner = typeof(window.innerWidth) == 'number';
    this.hasElement =  document.documentElement
        && document.documentElement.clientWidth;    
	this.move = function ()
	{//move function start
		jQuery(this.menu).css('left', this.nextX + 'px').css('top', this.nextY + 'px');
	};  
	this.computeShifts = function ()
	{
	    var de = document.documentElement;	
	    this.shiftX =
	        this.hasInner
	        ? pageXOffset
	        : this.hasElement
	          ? de.scrollLeft
	          : document.body.scrollLeft;
	    if (this.targetX < 0)
	    {
	        if (this.hasElement && this.hasInner)
	        {
	            // Handle Opera 8 problems
	            this.shiftX +=
	                de.clientWidth > window.innerWidth
	                ? window.innerWidth
	                : de.clientWidth
	        }
	        else
	        {
	            this.shiftX +=
	                this.hasElement
	                ? de.clientWidth
	                : this.hasInner
	                  ? window.innerWidth
	                  : document.body.clientWidth;
	        }
	    }
	
	    this.shiftY = 
	        this.hasInner
	        ? pageYOffset
	        : this.hasElement
	          ? de.scrollTop
	          : document.body.scrollTop;
	    if (this.targetY < 0)
	    {
	        if (this.hasElement && this.hasInner)
	        {
	            // Handle Opera 8 problems
	            this.shiftY +=
	                de.clientHeight > window.innerHeight
	                ? window.innerHeight
	                : de.clientHeight
	        }
	        else
	        {
	            this.shiftY +=
	                this.hasElement
	                ? document.documentElement.clientHeight
	                : this.hasInner
	                  ? window.innerHeight
	                  : document.body.clientHeight;
	        }
	    }
	};
	this.doFloat = function()
	{
	    var stepX, stepY;
	
	    this.computeShifts();
	
	    stepX = (this.shiftX + 
	        this.targetX - this.nextX) * .07;
	    if (Math.abs(stepX) < .5)
	    {
	        stepX = this.shiftX +
	            this.targetX - this.nextX;
	    }
	
	    stepY = (this.shiftY + 
	        this.targetY - this.nextY) * .07;
	    if (Math.abs(stepY) < .5)
	    {
	        stepY = this.shiftY + 
	            this.targetY - this.nextY;
	    }
	
	    if (Math.abs(stepX) > 0 ||
	        Math.abs(stepY) > 0)
	    {
	        this.nextX += stepX;
	        this.nextY += stepY;
	        this.move();
	    }
		
	    setTimeout('funcFloating[' + this.index + '].func()', this.interval);
	};	
	this.initSecondary = function()
	{
	    this.computeShifts();
	    this.nextX = this.shiftX +
	        this.targetX;
	    this.nextY = this.shiftY +
	        this.targetY;
	    this.move();
	};	
	this.position = function(targetX, targetY)
	{
		if(typeof(targetX) == 'number')
		{
			this.targetX = targetX;
		}else if(typeof(targetX) == 'string')
		{
			switch(targetX)
			{
				case 'left':
					this.targetX = 5;
					break;
				case 'right':
					this.targetX = -((jQuery(this.menu).outerWidth()) + 5);
					break;									
			}
			
		}	
		if(typeof(targetY) == 'number')
		{
			this.targetY = targetY;
		}else if(typeof(targetY) == 'string')
		{
			switch(targetY)
			{
				case 'top':
					this.targetY= 5;
					break;
				case 'bottom':

					this.targetY = -((jQuery(this.menu).outerHeight() + 5));
					
					break;								
			}
			
		}				
		
	};
	this.init = function()
	{
		jQuery(this.menu).css('position', 'absolute');
		if(typeof(this.options)  == 'object')
		{
			for(var i in this.options)
			{
				switch(i)
				{
					
					case 'targetX':
					case 'targetY':
						if(typeof(this.options[i]) == 'number')
						{
							this[i] = this.options[i];
						}else if(typeof(this.options[i]) == 'string')
						{
							switch(this.options[i])
							{
								case 'top':
									this.targetY= 5;
									break;
								case 'bottom':
									this.targetY = -((jQuery(this.menu).outerHeight() + 5));									
									break;
								case 'left':
									this.targetX = 5;
									break;
								case 'right':
									this.targetX = -((jQuery(this.menu).outerWidth()) + 5);
									break;									
							}
							
						}
						break;	
					default:
						this[i] = this.options[i];				
				}
			}
		}		
		
	    this.initSecondary();
	    this.doFloat();
	}
		      
};

// Some browsers init scrollbars only after
// full document load.
var funcFloating = {};
jQuery.fn.floating = function(options)
{
	return jQuery(this).each(
	
		function(i)
		{		
			var nextIndex = 0;
			for(var index in funcFloating)
			{
				nextIndex = parseInt(index);
			}	
			funcFloating[nextIndex + 1] = {};	
			funcFloating[nextIndex + 1].box = this;	
			funcFloating[nextIndex + 1].obj = new floatingBox(this, options, (nextIndex + 1));
			funcFloating[nextIndex + 1].func = function(){ funcFloating[nextIndex + 1].obj.doFloat(); };
			if (document.layers)
			{
				funcFloating[nextIndex + 1].obj.init();
			}else
			{
				
				funcFloating[nextIndex + 1].obj.init();
				funcFloating[nextIndex + 1].obj.initSecondary();
			}
			
		}
	);
};
jQuery.fn.floatingPosition = function(targetX, targetY)
{
	return jQuery(this).each(
	
		function(i)
		{	
			for(var j in funcFloating)
			{
				if(funcFloating[j].box == this)
				{
					funcFloating[j].obj.position(targetX, targetY);
				}
			}
			
		}
	);	
};