/* 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 nex = opts.next;
		var pre = opts.prev;
		

		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:"hidden"
						//overflow:"auto"
					});
					$(".temp_hide").show();
				}
			);
			
		}
		
		
		
		$(this).find(jFC).each(function(i){
			$(this).click(function(){
								   
				
						
				if ($(opts.slides).is(":not(:animated)")) {
					
					
					
					//alert("dot clicked");
					//alert("Starting Cur: " + cur);
					
					mycounter = cur;
					//alert("mycounter: "+mycounter);
					//alert ("dot");				  
					videoID = "video"+mycounter;
					//alert (videoID);
					var myPlayer = _V_(videoID);
					myPlayer.pause();
					swfID = "flash_fallback"+mycounter;
					$f(swfID).pause();
						
						
					$(jFC).removeClass(jSel);
					$(this).addClass(jSel);
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
					
					
					//alert("Cur for arrows: " + cur);
					//if this dot is first or last one, disable appropriate arrow, otherwise, enable both arrows
					if ( cur == 0) {
						
						
						
						//disable prev arrow
						$('.jFlowPrev').addClass('jFlowPrev-disabled');
						$('.idx_sliderPrev').addClass('idx_sliderPrev-disabled');
						//enable next arrows
						$('.jFlowNext-disabled').removeClass('jFlowNext-disabled');
						$('.idx_sliderNext-disabled').removeClass('idx_sliderNext-disabled');
						
						//alert("disable prev arrow");
					} else if ( cur == maxi-1) {
						//disable next arrow
						$('.jFlowNext').addClass('jFlowNext-disabled');
						$('.idx_sliderNext').addClass('idx_sliderNext-disabled');
						//enable prev arrows
						$('.jFlowPrev-disabled').removeClass('jFlowPrev-disabled');
						$('.idx_sliderPrev-disabled').removeClass('idx_sliderPrev-disabled');
						//alert("disable next arrow");
					} else {
						//enable both arrows
						$('.jFlowNext-disabled').removeClass('jFlowNext-disabled');
						$('.jFlowPrev-disabled').removeClass('jFlowPrev-disabled');
						$('.idx_sliderNext-disabled').removeClass('idx_sliderNext-disabled');
						$('.idx_sliderPrev-disabled').removeClass('idx_sliderPrev-disabled');
						//alert("enable both arrow");
					}
					
					
					
						//alert("dot clicked");
						//dynamically add next item's content to the slider
						var item_to_add = cur;
						var item_to_add_next = cur+1;
						var item_to_add_prev = cur-1;
						var add_item = false;
						//search through slide spans for id#, then add content to it, else, do nothing
						
						
						
						$(opts.slides).find("span").each(function(){
								if ( $(this).attr("id") == item_to_add ) { //if this is our target span
									if ( document.getElementById(item_to_add).firstChild == "div" ) { //if this span has first child as a div, then don't add anything, content is already there
										add_item = false;
										//alert("item " + item_to_add + " content already exists");
									} else {
										//alert("item " + item_to_add + " needs to be added");
										add_item = true;
									}
								}
						})
						
						
						
					
					
					
				}
				//alert(add_item);
				
				if (add_item == true) {
					//alert("Class: " + $(this).attr("class"));
						if ( $(this).attr("class") == "idx_sliderControl idx_Slide_Selected" ) {
							//alert("add idx");
							add_itemHTML(idx_carousel_itemList[item_to_add], item_to_add);
							add_itemHTML(idx_carousel_itemList[item_to_add_prev], item_to_add_prev);
							add_itemHTML(idx_carousel_itemList[item_to_add_next], item_to_add_next);
						} else {
							//alert(maxi);
							add_itemHTML(mycarousel_itemList[item_to_add], item_to_add);
							if (item_to_add_prev != -1) {
								add_itemHTML(mycarousel_itemList[item_to_add_prev], item_to_add_prev);
							}
							if (item_to_add_next != maxi) {
								add_itemHTML(mycarousel_itemList[item_to_add_next], item_to_add_next);
							}						
						}
				}
				//alert("mycounter: "+mycounter);
			});
		});	
		
		$(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()+"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();						  
		});
		
		//initialize prev arrow and dots to first disabled and on states
		$('.jFlowPrev').addClass('jFlowPrev-disabled');
		//alert("disabled");
		$('.idx_sliderPrev').addClass('idx_sliderPrev-disabled');
		$('.idx_workPrev' + '.smalldot').addClass('smalldoton'); //turn dot1 on
		$('.idx_newsPrev' + '.smalldot').addClass('smalldoton'); //turn dot1 on
		$('.idx_workPrev' + '.smallprevarrow').addClass('smallprevarrow-disabled'); 
		$('.idx_newsPrev' + '.smallprevarrow').addClass('smallprevarrow-disabled'); 
		
		//if viewer is coming from a featured click on index page, auto scroll to that one
		if ($scrolltoJS != "-1") {
			//alert ("jFlow scrollto: " + $scrolltoJS);
			//autoscroll
			if ($(opts.slides).is(":not(:animated)")) {
						//$scrolltoJS = maxi-1;
				
						//add that item's content first
						
						var dur = Math.abs(8);
						slide(dur,$scrolltoJS);
						
						cur = $scrolltoJS;
						//cur = parseInt($scrolltoJS);
						
						$(jFC).removeClass(jSel);
						$(document.getElementById("dot" + $scrolltoJS)).addClass(jSel);
						
						//alert("Cur for arrows: " + cur);
						//if this dot is first or last one, disable appropriate arrow, otherwise, enable both arrows
						if ( cur == 0) {
							//disable prev arrow
							$('.jFlowPrev').addClass('jFlowPrev-disabled');
							$('.idx_sliderPrev').addClass('idx_sliderPrev-disabled');
							//enable next arrows
							$('.jFlowNext-disabled').removeClass('jFlowNext-disabled');
							$('.idx_sliderNext-disabled').removeClass('idx_sliderNext-disabled');
							
							//alert("disable prev arrow");
						} else if ( cur == maxi-1) {
							//disable next arrow
							$('.jFlowNext').addClass('jFlowNext-disabled');
							$('.idx_sliderNext').addClass('idx_sliderNext-disabled');
							//enable prev arrows
							$('.jFlowPrev-disabled').removeClass('jFlowPrev-disabled');
							$('.idx_sliderPrev-disabled').removeClass('idx_sliderPrev-disabled');
							//alert("disable next arrow");
						} else {
							//enable both arrows
							$('.jFlowNext-disabled').removeClass('jFlowNext-disabled');
							$('.jFlowPrev-disabled').removeClass('jFlowPrev-disabled');
							//alert("enable both arrow");
						}
						
						
						//dynamically add next item's content to the slider
						var item_to_add = cur;
						var item_to_add_next = parseInt(cur)+1;
						var item_to_add_next2 = parseInt(cur)+2;
						item_to_add_next.toString();
						item_to_add_next2.toString();
						var item_to_add_prev = cur-1;
						var add_item = false;
						
						//search through slide spans for id#, then add content to it, else, do nothing
						$(opts.slides).find("span").each(function(){
								if ( $(this).attr("id") == item_to_add ) { //if this is our target span
									if ( document.getElementById(item_to_add).firstChild == "div" ) { //if this span has first child as a div, then don't add anything, content is already there
										//alert ("item_to_add: " + item_to_add);
										add_item = false;
										//alert("item " + item_to_add + " content already exists");
									} else {
										//alert ("item_to_add: " + item_to_add);
										//alert("item " + item_to_add + " needs to be added");
										add_item = true;
									}
								}
						})
						
						
						if (add_item == true) {
								//alert(item_to_add_next);
								
								if (cur == 0) {
									add_itemHTML(mycarousel_itemList[item_to_add_next2], item_to_add_next2);
								} else if ( cur == maxi-1) {
									//add current and prev items
									add_itemHTML(mycarousel_itemList[item_to_add], item_to_add);
									add_itemHTML(mycarousel_itemList[item_to_add_prev], item_to_add_prev);
									
								} else {
									add_itemHTML(mycarousel_itemList[item_to_add], item_to_add);
									add_itemHTML(mycarousel_itemList[item_to_add_prev], item_to_add_prev);
									add_itemHTML(mycarousel_itemList[item_to_add_next], item_to_add_next);
									add_itemHTML(mycarousel_itemList[item_to_add_next2], item_to_add_next2);
								}
								
			}
			}
			
			
		} 
		
		$(opts.prev).click(function(){
									
			mycounter = cur;
									
			if ($(opts.slides).is(":not(:animated)")) {
				
				//enable next arrow
				if ( $(this).attr('class') == "jFlowPrev" ) {
					$('.jFlowNext-disabled').removeClass('jFlowNext-disabled');
				} else 
				if ( $(this).attr('class') == "idx_sliderPrev" ) {
					$('.idx_sliderNext-disabled').removeClass('idx_sliderNext-disabled');
					//alert("idx photo prev arrow");
				} else
				if ( $(this).attr('class') == "idx_workPrev smallprevarrow" || $(this).attr('class') == "idx_workPrev smalldot" ) {
					$('.idx_workNext' + '.smalldot' + '.smalldoton').removeClass('smalldoton'); //turn dot2 off
					$('.idx_workPrev' + '.smalldot').addClass('smalldoton'); //turn dot1 on
					$('.idx_workNext' + '.smallnextarrow').removeClass('smallnextarrow-disabled'); //enable next arrow
					$('.idx_workPrev' + '.smallprevarrow').addClass('smallprevarrow-disabled'); //disable prev arrow
					//alert("work prev clicked");
				} else
				if ( $(this).attr('class') == "idx_newsPrev smallprevarrow" || $(this).attr('class') == "idx_newsPrev smalldot" ) {
					$('.idx_newsNext' + '.smalldot' + '.smalldoton').removeClass('smalldoton'); //turn dot2 off
					$('.idx_newsPrev' + '.smalldot').addClass('smalldoton'); //turn dot1 on
					$('.idx_newsNext' + '.smallnextarrow').removeClass('smallnextarrow-disabled'); //enable next arrow
					$('.idx_newsPrev' + '.smallprevarrow').addClass('smallprevarrow-disabled'); //disable prev arrow
					//alert("news prev clicked");
				}
				
					
				//alert("Maxi: " + maxi);
				//alert("Current: " + cur);
				var dur = 1;
				
				if (cur > 0) {
					if (cur == 1) {
						//disable prev arrow, and don't add any elements
						if ( $(this).attr('class') == "jFlowPrev" ) {
							$(this).addClass('jFlowPrev-disabled');
						} else 
						if ( $(this).attr('class') == "idx_sliderPrev" ) {
							$(this).addClass('idx_sliderPrev-disabled');
						} 
				
										
					} else {
						//if prev arrow was clicked for port or news section
						if ($(this).attr('class') == "jFlowPrev" || $(this).attr('class') == "idx_sliderPrev") {
							//dynamically add prev item's content to the slider
							var item_to_add = cur-2;
							var add_item = false;
							//search through slide spans for id#, then add content to it, else, do nothing
							$(opts.slides).find("span").each(function(){
								if ( $(this).attr("id") == item_to_add ) { //if this is our target span
									if ( document.getElementById(item_to_add).firstChild == "div" ) { //if this span has first child as a div, then don't add anything, content is already there
										add_item = false;
										//alert("item " + item_to_add + " content already exists");
									} else {
										//alert("item " + item_to_add + " needs to be added");
										add_item = true;
									}
								}
							})
						}
						
					}
					cur--;
				} else {
					//alert("top of stack");
					/* this part makes the slider go to the last item if prev is clicked while on the first item
					cur = maxi -1;
					dur = cur;
					*/					
				}
				
				$(jFC).removeClass(jSel);
				slide(dur,cur);
				$(jFC).eq(cur).addClass(jSel);
				
				if (add_item == true) {
					if ( $(this).attr('class') == "idx_sliderPrev" ) {
							//alert("adding item " + item_to_add);
							add_itemHTML(idx_carousel_itemList[item_to_add], item_to_add);
						} else {
						//alert("adding item " + item_to_add);
						add_itemHTML(mycarousel_itemList[item_to_add], item_to_add);
						}
				}
			}
		});
		
		$(opts.next).click(function(){
			//alert('next');
			mycounter = cur;
				
			if ($(opts.slides).is(":not(:animated)")) {
				//enable prev arrow, 
				if ( $(this).attr('class') == "jFlowNext" ) {
					$('.jFlowPrev-disabled').removeClass('jFlowPrev-disabled');
				} else 
				if ( $(this).attr('class') == "idx_sliderNext" ) {
					$('.idx_sliderPrev-disabled').removeClass('idx_sliderPrev-disabled');
				} else
				if ( $(this).attr('class') == "idx_workNext smallnextarrow" || $(this).attr('class') == "idx_workNext smalldot" ) {
					$('.idx_workPrev' + '.smalldot' + '.smalldoton').removeClass('smalldoton'); //turn dot1 off
					$('.idx_workNext' + '.smalldot').addClass('smalldoton'); //turn dot2 on
					$('.idx_workNext' + '.smallnextarrow').addClass('smallnextarrow-disabled'); //disable next arrow
					$('.idx_workPrev' + '.smallprevarrow').removeClass('smallprevarrow-disabled'); //enable prev arrow
					//alert("work next clicked");
				} else
				if ( $(this).attr('class') == "idx_newsNext smallnextarrow" || $(this).attr('class') == "idx_newsNext smalldot" ) {
					$('.idx_newsPrev' + '.smalldot' + '.smalldoton').removeClass('smalldoton'); //turn dot1 off
					$('.idx_newsNext' + '.smalldot').addClass('smalldoton'); //turn dot2 on
					$('.idx_newsNext' + '.smallnextarrow').addClass('smallnextarrow-disabled'); //disable next arrow
					$('.idx_newsPrev' + '.smallprevarrow').removeClass('smallprevarrow-disabled'); //enable prev arrow
					//alert("news next clicked");
				}
				
				
				//alert("Maxi: " + maxi);
				//alert("Current: " + cur);
				var dur = 1;
				
				if (cur < maxi - 1) {
					//if next arrow was clicked for port or news section
					if ($(this).attr('class') == "jFlowNext" || $(this).attr('class') == "idx_sliderNext") {
						//dynamically add next item's content to the slider
							var item_to_add = cur+2;
							var add_item = false;
							//search through slide spans for id#, then add content to it, else, do nothing
							$(opts.slides).find("span").each(function(){
								if ( $(this).attr("id") == item_to_add ) { //if this is our target span
									if ( document.getElementById(item_to_add).firstChild == "div" ) { //if this span has first child as a div, then don't add anything, content is already there
										add_item = false;
										//alert("item " + item_to_add + " content already exists");
									} else {
										//alert("item " + item_to_add + " needs to be added");
										add_item = true;
									}
								}
							})
					}
					
					
					if (cur == maxi - 2) {
						//disable next arrow
						if ( $(this).attr('class') == "jFlowNext" ) {
							$(this).addClass('jFlowNext-disabled');
						} else 
						if ( $(this).attr('class') == "idx_sliderNext" ) {
							$(this).addClass('idx_sliderNext-disabled');
						} 			

						add_item = false;
						//alert("no more to add");
					}
									
					cur++;
				} else {
					//alert("end of stack");
					/* this part makes the slider go to the first item if next is clicked while on the last item
					cur = 0;
					dur = maxi -1;
					*/					
				}
				
				$(jFC).removeClass(jSel);
				slide(dur, cur);
				$(jFC).eq(cur).addClass(jSel);
				
				
				if (add_item == true) {
						if ( $(this).attr('class') == "idx_sliderNext" ) {
							//alert("adding item " + item_to_add);
							add_itemHTML(idx_carousel_itemList[item_to_add], item_to_add);
						} else {
						//alert("adding item " + item_to_add);
						add_itemHTML(mycarousel_itemList[item_to_add], item_to_add);
						}
				}
								
			}
			
			
		
		});
		
	};
	
	$.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: 400,
		width: "100%",
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	};
	
})(jQuery);

