var $j = jQuery.noConflict();

var fpVideoInstance = false;

$j(function() {
  $j("body").addClass("jsEn");
    
  $j('#nav-tier1 ul li').mouseover(function() {
    $j(this).parent().find('li.selected > div.nav-strip-container-2').css("display","none");
    $j(this).find('div.nav-strip-container-2').css("display","block");
  });

  $j('#nav-tier1 ul li').mouseout(function() {
    $j(this).find('div.nav-strip-container-2').css("display","none");
    $j(this).parent().find('li.selected > div.nav-strip-container-2').css("display","block");
  });
  
  if(swfobject.hasFlashPlayerVersion("9")) {
		var att = { data:"flash/logo.swf", width:"240", height:"95" };
		var par = { menu:"false" };
		var par = { wmode:"transparent" };
		var id = "chol-logo-target";
		var myFlashContent = swfobject.createSWF(att, par, id);
		
		$j("#chol-logo-target").css("background-image","none");
	}
  
  $j("div#nav-strip-container div#right-hold p.text-size-controls a").click(function() {
		$j.getJSON(MIYOLib._conf.siteBaseURL+'/accessibility/textsize/' + $j(this).attr("class") + '/?mode=ajax', function(data) {
			$j("body")
				.removeClass("small")
				.removeClass("medium")
				.removeClass("large")
				.addClass(data.text_size);
		});
			
		return false;
	});
	
  $j("a[href^=http://]")
  	.not("[href^="+MIYOLib._conf.siteBaseURL+"]")
  	.each(function() {
  			this.target = "_blank";
  	});
  
  $j("a.external").each(function() {
    this.target = "_blank";
  });
  
  $j("a.twitter-link").click(function() {
    $j("#footer-twitter-container").toggle();
    return false;
  });

  initVideoPlayer();
  initImageSlideshow();
  
  $j(window).bind('resize', videoHandleResize);
});

function initVideoPlayer() {
  if(!swfobject.hasFlashPlayerVersion("9.0.0")) {
    return false;
  }
  
  $j("#fp-play").each(function() {
    var vidLink = this;

    vidLink.contId = $j(vidLink).attr("id");
    vidLink.video = $j(vidLink).attr("href");
    
    browserwidth = $j(window).width();

    fpVideoInstance = $f(vidLink.contId, {
  			src: "flash/flowplayer/flowplayer-3.2.2.swf",
  			wmode: "transparent"
  		},
  		{
  			clip: {
  				baseUrl: MIYOLib._conf.siteBaseURL,
  				width: "100%",
  				height: "100%"
  			},
  			playlist: [
  				{
  					url: vidLink.video,
  					autoPlay: true,
  					autoBuffering: true
  				}
  			],
  			//key: '#$923bc42c9a13ba56ed5',
  			contextMenu: [ 

  					// 1. "About Flowplayer...";  selecting this item goes to our homepage 
  					{'About Wonder Associates ...' : function() { 
  							location.href = "http://www.wonderassociates.com"; 
  					}}, 

  					// 2. menu separator. 
  					'-', 

  					// 3. version information. 
  					'Wonder Media Player v3.2.1' 
  			],
  			plugins: {
  				controls: null
  			}
  		}
  	).controls(vidLink.contId+"-pc");
  });
  
  if($j("#fp-play").length > 0) {
    $j("#play-video").toggle();
  
    $j("#play-video").click(function() {
  	  browserheight = $j(window).height();
  	  browserwidth = $j(window).width();
  	  $vidoPlayContainer = $j("#play-video-container");
  	  $vidoPlayContainer.css("height", browserheight);
	
  	  if($j("#play-video-container").is(':hidden') && typeof(fpVideoInstance) == 'object') {
        $j("#fp-play-pc").height(42);
        $j("body").addClass("video-playing");
	    } else if($j("#play-video-container").is(':visible') && typeof(fpVideoInstance) == 'object') {
	      $j("#fp-play-pc").height(0);
	      $j("body").removeClass("video-playing");
	    }
    
      $j("#play-video-container").toggle();
      $j("#bg").toggle();
      $j("#sign-up").toggle();
      $j("#footer-logos").toggle();
      $j("#main-content-block").toggle();
    
	    return false;
    });
  }
}

function videoHandleResize() {
  if(!swfobject.hasFlashPlayerVersion("9.0.0")) {
    return false;
  }
  
  if($j("#play-video-container").is(':visible')) {
    //our min video sizes
  	minwidth = 400;
  	minheight = 250;
	
  	ratio = 16/9;
  	
  	$vidoPlayContainer = $j("#play-video-container");
  	
  	//current window sizes
  	browserwidth = $j(window).width();
  	browserheight = $j(window).height();

    topoffset = 0;
    leftoffset = 0;
    
    newvideowidth = 0;
    newvideoheight = 0;
    
    if(browserwidth <= minwidth && browserheight <= minheight) {
      newvideowidth = minwidth;
      newvideoheight = minheight;
      $vidoPlayContainer.css("height", minheight);
    } else {
      $vidoPlayContainer.css("height", browserheight);
      
      newvideowidth = browserheight * ratio;
      newvideoheight = browserheight;
    
      if(newvideowidth < browserwidth) {
        newvideoheight = browserwidth / ratio;
        newvideowidth = browserwidth;
        topoffset = (newvideoheight - browserheight) / 2;
      } else {
        if(newvideowidth > browserwidth) {
          leftoffset = (newvideowidth - browserwidth) / 2;
        }
      }
    }
    
    $j("#fp-play-pc").width(browserwidth);
	  $j("#fp-play-pc .track").width((browserwidth - 132));
  
    $j("#fp-play").css("width", newvideowidth);
		$j("#fp-play").css("height", newvideoheight);
		if(leftoffset > 0) {
		  $j("#fp-play").css("left", "-"+leftoffset+"px");
	  } else {
	    $j("#fp-play").css("left", "0px");
	  }
	  
		if(topoffset > 0) {
		  $j("#fp-play").css("top", "-"+topoffset+"px");
	  } else {
	    $j("#fp-play").css("top", "0px");
	  }
  }
}

function initImageSlideshow() {
  $j("div.image-list").each(function() { 
	  /*** Parent container with overflow hidden ***/
  	var container = this;
  	
  	container.animRefs = {};

  	/*** The actual carosel content holder ***/
  	container.$carosel = $j(container).find('ul');

  	/*** The items in the carosel to transition between ***/
  	container.$mediaItems = container.$carosel.find('li');

  	/*** Get an entry to calculate dimensions ***/
  	container.$singleEntry = container.$mediaItems.filter(':first');

  	/*** How wide each media entry is (with the border) ***/
  	container.mediaEntryWidth = container.$singleEntry.outerWidth();

  	/*** How many are visible at any given time ***/
  	container.numberVisible = Math.ceil(($j(container).innerWidth() / container.mediaEntryWidth));

  	/*** How many slides do we have to transition? ***/
  	container.slideCount = Math.ceil(container.$mediaItems.length / container.numberVisible);

  	/*** Start by showing the first slide ***/
  	container.currentSlide = 1;

  	/*** Variable to hold slideAnimation call ***/
  	container.slideAnimation;

  	/*** Variable to hold gotoSlide call ***/
  	container.gotoSlide;

  	/*** The timeout holder ***/
  	container.animationDelay;

  	container.checkRealPos = function(isStart) {
  		isStart = (typeof(isStart) == 'undefined')?false:true;
		 
  		var caroStartPos = container.$carosel.position();
  		var realPos = caroStartPos.left;
  		var expectedPos = Math.abs((-1 * container.currentSlide * container.mediaEntryWidth));

  		if(realPos != (-1 * expectedPos)) {
  			if(realPos != 0) {
  				if(isStart) {
  					realPos = realPos - container.mediaEntryWidth;
  				}
  				container.currentSlide = Math.round((Math.abs(realPos) / container.mediaEntryWidth));
  			} else {
  				if(isStart) {
  					container.currentSlide = 1;
  				} else {
  					container.currentSlide = 0;
  				}
  			}
  		}
  	}
	
  	if(container.slideCount > 1) {
  	  /*** We need to duplicate the content of the first and last slide content to make it seamless ***/
  		container.$mediaItems.filter(':first').before(container.$mediaItems.slice(-container.numberVisible).clone().each(function() {
  		  var firstMediaItem = this;

  			$j(firstMediaItem)
  				.addClass('cloned');
  		}));
  		container.$mediaItems.filter(':last').after(container.$mediaItems.slice(0, container.numberVisible).clone().each(function() {
  		  var lastMediaItem = this;
	
  			$j(lastMediaItem)
  				.addClass('cloned');
  		}));
  	}
	
  	/*** Refresh the $mediaItems list ***/
  	container.$mediaItems = container.$carosel.find('li');

  	/*** Calculate the new width of the carosel content holder ***/
  	container.newWidth = container.$mediaItems.length * container.mediaEntryWidth;

  	container.checkRealPos(true);

  	/*** Set new properties for the carosel content holder ***/
  	container.$carosel.css('width', container.newWidth);
	
  	if(container.slideCount > 1) {
  	  container.$carosel.css('left', (-1 * container.currentSlide * container.mediaEntryWidth));
    }
  
  	container.$carosel.find("li.entry-"+container.currentSlide).addClass("selected");

  	container.gotoSlide = function(slideNo,navClick) {
  		navClick = (typeof(navClick) == 'undefined')?false:true;
		
  		clearTimeout(container.animationDelay);
	
  		/*** Get where we currently think we are ***/
  		var originalPos = container.currentSlide;
	
  		/*** Check where we REALLY are ***/
  		container.checkRealPos();
	
  		/*** If they are not equivalent correct values ***/
  		if(originalPos != container.currentSlide) {
  			container.$carosel.css('left', (-1 * container.currentSlide * container.mediaEntryWidth));
		
  			if(!navClick) {
  				if(container.currentSlide >= container.slideCount) {
  					slideNo = 2;
  				} else {
  					slideNo = container.currentSlide + 1;
  				}
  			}
  		}
	
  		/*** Used for setting "selected" class ***/
  		var actualSlide;
	
  		if(slideNo > container.slideCount) {
  			actualSlide = 1;
  		} else if(slideNo < 1) {
  			actualSlide = container.slideCount;
  		} else {
  			actualSlide = slideNo;
  		}
	
  		/*** Determine the direction of the transition ***/
  		var direction = slideNo < container.currentSlide ? -1 : 1;
	
  		/*** Get the required slide shift ***/
  		var slideShift = Math.abs(container.currentSlide - slideNo);
	
  		/*** How much in pixels we are shifting ***/
  		var leftShift = direction * container.mediaEntryWidth * container.numberVisible * slideShift;
	
  		container.$carosel.find('li.selected').removeClass("selected");
	
  		container.$carosel.filter(':not(:animated)').animate({
  			left: '-='+leftShift
  		}, 500, function () {					
  			if (slideNo == 0) {
  				container.$carosel.css('left', -(container.mediaEntryWidth * container.numberVisible * container.slideCount));
  				slideNo = container.slideCount;
  			} else if (slideNo > container.slideCount) {
  				container.$carosel.css('left', -(container.mediaEntryWidth * container.numberVisible));
  				slideNo = 1;
  			}
  			
  			container.$carosel.find('li.entry-'+slideNo).addClass("selected");
  			container.currentSlide = slideNo;
  		});
    
  	  container.animationDelay = setTimeout(container.slideAnimation, 3000);
	
  		return false;
  	}

  	container.slideAnimation = function() {
  		var jumpTo = container.currentSlide + 1;
	 
  		container.gotoSlide(jumpTo);
  	}
  	
  	if(container.slideCount > 1) {
  	  container.animationDelay = setTimeout(container.slideAnimation, 3000);
	  }
	});
}
