/* 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+
 * Modified by: Diwa G. Fernandez http://poeticpixel.info
 * (November 3, 2009)
 * @package WordPress
 * @subpackage Dragonica_Theme

 */
 
(function($) {

	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var randNum = Math.floor(Math.random()*11);
		var jFC = opts.controller;
		var jOCtrl = opts.outercontroller;
		var jFS =  opts.slideWrapper;
		var jSel = opts.selectedWrapper;
		var cur = 0;
		var maxi = $(jFC).length;
		
		var bm = ["#home","#community","#shop","#about","#download"];
		//var bmcount = bm.length;
		var myLoc = window.location.href.toString();
		// 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();
				}
			);			
		}
		
		var myAnchor = function(myLoc) {
			return myAnchor;
		}
		var diwa = function(myLoc){			
			if (myLoc.match('#')) { // the URL contains an anchor
				// slide the navigation item corresponding to the anchor
				var myAnchor = '#' + myLoc.split('#')[1];
				var $i = 0; 
				for($i=0;$i<bm.length; $i++) {
					if (myAnchor == bm[$i]) {
						slide($i,$i)
						cur = $i;
						$(jFC).removeClass(jSel);
						$(jFC).eq(cur).addClass(jSel);
					}
				}
			}
		}
		
		$(diwa(myLoc));

		/* This is the navigation inside #myDragonica */
		/* How to use: 
		 * Data 3 is the panel 4
			<span class="outtercontrol"><strong><a href="#news" data="3"></a></strong></span>
		*/
		
		$('.outterControl').click(function(bm) {
			//var $templateimg=$(this).find('a["data"]:eq(0)'); //reference to the first attribute
			var $tempLink = $(this).find('a');
			var $data = $(this).find('a').attr("href");
			//alert ('i r klikd: ' + $data);
			//alert ($data);
				if ($data) {
					$(diwa($data));
				}
			});
		
		// This is the navigation inside .jFlowControl
		$(this).find(jFC).each(function(i){
				var dur2 = Math.abs(cur-i);
				$(this).attr({ 
					data: "slide("+dur2+","+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(".parentdiv").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()+"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(){
			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);
				theroot = bm[cur];
				thePrev = bm[cur-1];
				$(opts.prev).find('a').attr('href',thePrev);
				window.location.hash = theroot;
				//alert( theroot );
			}
		});
		$(opts.prev2).hover(function(){
			$(opts.prev2).animate({
				opacity: '1'
			},300);
			$(opts.prev2).css({
				cursor: 'pointer'
			})
		},function(){
			$(opts.prev2).animate({
				opacity: '0'
			},300);			
		});
		
		$(opts.prev2).click(function(){
			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);
				theroot = bm[cur];
				thePrev = bm[cur-1];
				$(opts.prev).find('a').attr('href',thePrev);
				window.location.hash = theroot;
				//alert( theroot );
			}
		});
		$(document).keydown(function(e){
			if ($(opts.slides).is(":not(:animated)")) {
				if (e.keyCode == 37) {
					var dur = 1;
					if (cur > 0) {
						cur--;
					} else {
						//cur = maxi -1;
						//dur = cur;
					}
					$(jFC).removeClass(jSel);
					slide(dur,cur);
					$(jFC).eq(cur).addClass(jSel);
					theroot = bm[cur];
					thePrev = bm[cur-1];
					$(opts.prev).find('a').attr('href',thePrev);
					window.location.hash = theroot;
				}
			}
			if ($(opts.slides).is(":not(:animated)")) {
				if (e.keyCode == 39) {
					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);
					theroot = bm[cur];
					theNext = bm[cur+1];
					$(opts.next).find('a').attr('href',theNext); 
					window.location.hash = theroot;
				}
			}
		});
		
		$(opts.next).click(function(){
			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);
				theroot = bm[cur];
				theNext = bm[cur+1];
				$(opts.next).find('a').attr('href',theNext); 
				window.location.hash = theroot;
			}
		});
		$(opts.next2).hover(function(){
			$(opts.next2).animate({
				opacity: '1'
			},300);
			$(opts.next2).css({
				cursor: 'pointer'
			})
		},function(){
			$(opts.next2).animate({
				opacity: '0'
			},300);			
		});

		$(opts.next2).click(function(){
			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);
				theroot = bm[cur];
				theNext = bm[cur+1];
				$(opts.next).find('a').attr('href',theNext); 
				window.location.hash = theroot;
			}
		});
	};

	$.fn.jFlow.defaults = {
		controller: "#myController .jFlowControl", // must be class, use . sign
		outtercontroller: ".outterControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		easing: "swing",
		duration: 400,
		width: "100%",
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext", // must be class, use . sign
		prev2: ".jFlowPrev2", // must be class, use . sign
		next2: ".jFlowNext2" // must be class, use . sign
	};



})(jQuery);
