/*////GALLERY THREE///*/

/*global jQuery, $ */

/*

	jQuery.PictureSlides is developed by Robert Nyman, http://www.robertnyman.com

	For more information, please see http://www.robertnyman.com/picture-slides

	Released under a MIT License

*/

jQuery.PictureSlides = function () {

	var useMSFilter = false,

		slideshows = [],

		set = function (settings) {

			slideshows.push(settings);

		},

		

		init = function () {

			var counter = 0;

			$(".picture-slides-container").each(function () {

				var elm = $(this),

					

					// Element references

					loader_image = '.ad-preloads',

					settings = slideshows[counter++],

					mainImage = elm.find("." + settings.mainImageClass),

					mainImageFailedToLoad = elm.find("." + settings.mainImageFailedToLoadClass),

					imageLink = elm.find("." + settings.imageLinkClass),

					fadeContainer = elm.find("." + settings.fadeContainerClass),

					imageTextContainer = elm.find("." + settings.imageTextContainerClass),

					previousLink = elm.find("." + settings.previousLinkClass),

					nextLink = elm.find("." + settings.nextLinkClass),

					imageCounter = elm.find("." + settings.imageCounterClass),

					startSlideShowLink = elm.find("." + settings.startSlideShowClass),

					stopSlideShowLink = elm.find("." + settings.stopSlideShowClass),

					thumbnailContainer = elm.find("." + settings.thumbnailContainerClass),

					thumbnailEvent = settings.thumbnailActivationEvent,

					thumbnailLinks,

					dimBackgroundOverlay = $("." + settings.dimBackgroundOverlayClass),

					

					// General image settings

					usePreloading = settings.usePreloading,

					useAltAsTooltip = settings.useAltAsTooltip,

					useTextAsTooltip = settings.useTextAsTooltip,

					images = settings.images,

					startIndex = (settings.startIndex > 0)? (settings.startIndex - 1) : settings.startIndex,

					imageIndex = startIndex,

					currentImageIndex = imageIndex,

					startSlideShowFromBeginning = settings.startSlideShowFromBeginning,

					dimBackgroundAtLoad = settings.dimBackgroundAtLoad,

					

					// General fade settings

					useFadingIn = settings.useFadingIn,

					useFadingOut = settings.useFadingOut,

					useFadeWhenNotSlideshow = settings.useFadeWhenNotSlideshow,

					useFadeForSlideshow = settings.useFadeForSlideshow,

					useDimBackgroundForSlideshow = settings.useDimBackgroundForSlideshow,

					loopSlideshow = settings.loopSlideshow,

					fadeTime = settings.fadeTime,

					timeForSlideInSlideshow = settings.timeForSlideInSlideshow,

					startSlideshowAtLoad = settings.startSlideshowAtLoad,

					slideshowPlaying = false,

					timer,

					

					// Sets main image

					setImage = function () {

						// Set main image values

						var imageItem = images[imageIndex];

						mainImage.attr({

							src : imageItem.image,

							alt : imageItem.alt

						});

						

						// If the alt text should be used as the tooltip

						if (useAltAsTooltip) {

							mainImage.attr("title", imageItem.alt);

						}

						

						// If the image text should be used as the tooltip

						if (useTextAsTooltip) {

							mainImage.attr("title", imageItem.text);

						}

						

						// Set image text

						if (imageTextContainer.length > 0) {

							imageTextContainer.text(imageItem.text);

						}

						

						// Set image link

						/*if (imageLink.length > 0) {

							var url = imageItem.url;

							if (typeof url !== "undefined" && url.length > 0) {

								imageLink.attr({"href": imageItem.url,"rel": "single"}).addClass("pirobox");//--rayChange:---open image with pirobox 

								//dynamic content encapsulated into 1 event.

							}

							else {

								imageLink.removeAttr("href");

								imageLink.removeClass("pirobox");

							}	

						}*/

						

						// Set image counter values

						if (imageCounter.length > 0) {

							imageCounter.text((imageIndex + 1) + "/" + (images.length));

						}

						

						if (!loopSlideshow) {

						// Enabling/disabling previous link

							/*if (imageIndex === 0) {

								previousLink.addClass("picture-slides-disabled");

							}

							else {

								previousLink.removeClass("picture-slides-disabled");

							}*/

						

							// Enabling/disabling next link

							if (imageIndex === (images.length - 1)) {

								nextLink.addClass("picture-slides-disabled");



							}

							else {

								nextLink.removeClass("picture-slides-disabled");

							}

						}

						

						// Keeping a reference to the current image index

						currentImageIndex = imageIndex;

						

						// Adding/removing classes from thumbnail

						if (thumbnailContainer[0]) {							

							thumbnailLinks.removeClass("picture-slides-selected-thumbnail");

							$(thumbnailLinks[imageIndex]).addClass("picture-slides-selected-thumbnail");

						}

					},

					

					

					

					

					

					// Navigate to previous image

					prev = function () {

						if (imageIndex > 0 || loopSlideshow) {

							if (imageIndex === 0) {

								imageIndex = (images.length -1);

							}

							else {

								imageIndex = --imageIndex;

							}

							if (useFadingOut && (useFadeWhenNotSlideshow || slideshowPlaying) && imageIndex !== currentImageIndex) {

								fadeContainer.stop();

								fadeContainer.fadeTo(fadeTime, 0, function () {

									setImage(imageIndex);						

								});	

							}

							else {

								if (useFadingIn && imageIndex !== currentImageIndex) {

									fadeContainer.css("opacity", "0");

								}

								setImage(imageIndex);

							}

						}

					},

					

					// Navigate to next image

					next = function (specifiedIndex) {

						if (imageIndex < (images.length -1) || typeof specifiedIndex !== "undefined" || loopSlideshow) {

							if (typeof specifiedIndex !== "undefined") {

								imageIndex = specifiedIndex;

							}

							else if (imageIndex === (images.length-1)) {

								imageIndex = 0;

							}

							else {

								imageIndex = ++imageIndex;

							}

							if (useFadingOut && (useFadeWhenNotSlideshow || slideshowPlaying) && imageIndex !== currentImageIndex) {

								fadeContainer.stop();

								fadeContainer.fadeTo(fadeTime, 0, function () {

									setImage(imageIndex);								

								});

							}

							else {

								if (useFadingIn && imageIndex !== currentImageIndex) {

									fadeContainer.css("opacity", "0");

								}	

								setImage(imageIndex);

							}

						}

						else {

							stopSlideshow();

						}

					},

					

					// Start slideshow

					/*startSlideshow = function () {

						slideshowPlaying = true;

						startSlideShowLink.hide();

						stopSlideShowLink.show();

						if (startSlideShowFromBeginning) {

							next(0);

						}

						clearTimeout(timer);

						timer = setTimeout(function () {

							next();

						}, timeForSlideInSlideshow);

						if (useDimBackgroundForSlideshow && dimBackgroundOverlay[0]) {

							elm.addClass("picture-slides-dimmed-background");

							dimBackgroundOverlay.show();

						}

					},*/

					

					// Stop slideshow

					stopSlideshow = function () {

						clearTimeout(timer);

						slideshowPlaying = false;

						startSlideShowLink.show();

						stopSlideShowLink.hide();

						if (useDimBackgroundForSlideshow && dimBackgroundOverlay[0]) {

							elm.removeClass("picture-slides-dimmed-background");

							dimBackgroundOverlay.hide();

						}

					};



				// Fade in/show image when it has loaded

				mainImage[0].onload = function () {

					if (useFadingIn && (useFadeWhenNotSlideshow || slideshowPlaying)) {

						fadeContainer.fadeTo(fadeTime, 1, function () {

							if (slideshowPlaying) {

								clearTimeout(timer);

								timer = setTimeout(function () {

									next();

								}, timeForSlideInSlideshow);

							}

						});

					}

					else {

						fadeContainer.css("opacity", "1");

						fadeContainer.show();

						

						

						/*//rayChange:--RESIZE IMAGE */

						/*function imgResize(){

	

							var theContainer = document.getElementById('containerTwo');

							theContainer.style.height = window.innerHeight - 100;

							

							var theImg = document.getElementById('scaleImage');

						

							if (theImg.offsetWidth > theContainer.offsetWidth) 

							{

								theImg.style.width='100' + '%';

								theImg.style.height='auto';

							}

							else if(theImg.offsetHeight > theContainer.offsetHeight)

							{

								theImg.style.height='100' + '%';

								theImg.style.width='auto';

							}

						}

						// onload

						$(window).load(function(){ imgResize(); });

						

						window.onresize = function(){

							imgResize();

						}*/

						

						if (slideshowPlaying) {

							clearTimeout(timer);

							timer = setTimeout(function () {

								next();

							}, timeForSlideInSlideshow);

						}

					}

					mainImageFailedToLoad.hide();

				};

				

				mainImage[0].onerror = function () {

					fadeContainer.css("opacity", "1");

					mainImageFailedToLoad.show();

					if (slideshowPlaying) {

						clearTimeout(timer);

						timer = setTimeout(function () {

							next();

						}, timeForSlideInSlideshow);

					}

				};

										

				// Previous image click

				previousLink.click(function (evt) {

					prev();

					return false;



				});

				

				// Next image click

				nextLink.click(function (evt) {

					next();

					return false;

				});

				

				// Start slideshow click

				startSlideShowLink.click(function () {

					startSlideshow();

					return false;

				});

				

				// Stop slideshow click

				stopSlideShowLink.click(function () {

					stopSlideshow();

					return false;

				});

				

				// Shows navigation links and image counter

				previousLink.show();

				nextLink.show();

				startSlideShowLink.show();

				imageCounter.show();

				

				// Stop slideshow click

				stopSlideShowLink.click(function () {

					stopSlideshow();

				});

				

				// Thumbnail references

				if (thumbnailContainer[0]) {

					thumbnailLinks = $(thumbnailContainer).find("a");

					$(thumbnailLinks[imageIndex]).addClass("picture-slides-selected-thumbnail");

					for (var i=0, il=thumbnailLinks.length, thumbnailLink; i<il; i++) {

						thumbnailLink = $(thumbnailLinks[i]);

						thumbnailLink.data("linkIndex", i);

						thumbnailLink.bind(thumbnailEvent, function (evt) {

							next($(this).data("linkIndex"));

							this.blur();

							return false;					

						});

					}

				}

				

				// Sets initial image

				setImage();

				

				// If play slideshow at load

				if (startSlideshowAtLoad) {

					startSlideshow();

				}

				

				if (dimBackgroundAtLoad) {

					elm.addClass("picture-slides-dimmed-background");

					dimBackgroundOverlay.show();

				}

				

				if (usePreloading) {

					var imagePreLoadingContainer = $("<div />").appendTo(document.body).css("display", "none");

					$('.picture-slides-image').css("opacity", "0");//image alpha 0 unti resized and loaded

					for (var j=0, jl=images.length, image; j<jl; j++) {

						$('<img src="' + images[j].image + '" alt="" />').appendTo(imagePreLoadingContainer);

					}

				}

			});

		};

	return {

		set : set,

		init : init

	};

}();

/*/////ALL DOCUMENT READY FUNCTIONS////////*/

$(document).ready(function () {

	jQuery.PictureSlides.init();

	

	 ///////NAV FADE SCRIPT////////

	 $("div#inside .nav").hover(

	  function () {

	  $(this).stop().animate({color: '#ffa51c'}, 500 );

	  }, 

	  function () {

	  $(this).stop().animate({color: '#999999'}, 500 );

	  }

	);

	 

	 $("div#insideTwo .nav").hover(

	  function () {

	  $(this).stop().animate({color: '#ffa51c'}, 500 );

	  }, 

	  function () {

	  $(this).stop().animate({color: '#999999'}, 500 );

	  }

	);

	 

	 $("div#insideThree .nav").hover(

	  function () {

	  $(this).stop().animate({color: '#ffa51c'}, 500 );

	  }, 

	  function () {

	  $(this).stop().animate({color: '#999999'}, 500 );

	  }

	);

	 ///////THUMB FADE SCRIPT////////

	  $(".thumbFade").hover(

	  function () {

	  $(this).stop().animate({'opacity':'.7'},350);

	  }, 

	  function () {

	  $(this).stop().animate({'opacity':'1'},350);

	  }

	);

				

	//////PYROBOX////////

	 $().piroBox_ext({

        piro_speed : 350,

        bg_alpha : 0.7,

        piro_scroll : true //pirobox always positioned at the center of the page

    });

	

});



/*//////RESIZE  /http://www.isme.nl/testResize/testResize.html////////*/ 

function setSizes() {

	var margin_top = 223;/*this evens out width to 1100px*/

	var margin_right = 10;

	var margin_bottom = 10;

	var margin_left = 10;

	

	// get image width and height

	var img_w = $('.picture-slides-image').width();

	var img_h = $('.picture-slides-image').height();

	// calculate viewport width and height

	var vp_w = $(window).width() - margin_right - margin_left;

	var vp_h = $(window).height() - margin_top - margin_bottom;

	//

	if (vp_w <= img_w || vp_w > img_w) {

		// new width

		var img_w_new=vp_w;

		// calculate new height

		var img_h_new=Math.round((img_h*img_w_new) / img_w);

	}

	//

	if (vp_h <= img_h || vp_h > img_h) {

		// new height

		var img_h_new=vp_h;

		// calculate new width

		var img_w_new=Math.round((img_w*img_h_new) / img_h);

	}

	// change image width and height to new width and new height

	$('.picture-slides-image').width(img_w_new);//.animate({width:img_w_new}, 100);//

	$('.picture-slides-image').height(img_h_new);//.animate({height:img_h_new}, 100);//

	$('.picture-slides-image').css("opacity", "1");

	$('.ad-preloads').css({visibility: 'hidden'});//hides preloader

}

	



// onload

$(window).load(function(){ setSizes(); });

// on resize

$(window).bind("resize", function() { setSizes(); });





/*//////TRACK HEIGHT AND WIDTH////////

function showWidth(ele, w) {

		  $("#test").text("The width for the " + ele + 

						" is " + w + "px.");

	}

	function showHeight(ele, h) {

		  $("#test2").text("The height for the " + ele + 

						" is " + h + "px.");

	}

	function showWrapperWidth(ele, w) {

      $("#test3").text("The width for the " + ele + 

                    " is " + w + "px.");

    }

	function showWrapperHeight(ele, h) {

      $("#test4").text("The height for the " + ele + 

                    " is " + h + "px.");

}*/

	

/*//////RUN RESIZE TEST FUNTION////////

	$(window).resize(function() {

			showWidth("image", $(".picture-slides-image").width()); 

			showHeight("image", $(".picture-slides-image").height());

			showWrapperWidth("container", $('#container').width()); 

			showWrapperHeight("container", $('#container').height()); 

	});*/
