﻿$(document).ready(function() {
scrollerImgs();
scrollImg();
var imgLength=$("#imgWrapper img").length;
makeGallery(imgLength);
navUlBtns();
setTitle();
setCopyrightYear();
});

/////////
function scrollerImgs() {
for (i=0;i<4;i++) {
var $id="scrDiv"+(i+1);
$("<div>").attr("id",$id).appendTo("#scroller");
}
$("#scroller div").hide();
$("#scroller div:eq(0)").addClass("on");
}

//////////////////////////

/////
function scrollImg() {
$("#scroller").find(".on").fadeIn(1000).delay(1000).fadeOut(1000, function() {
var nextImg=$(this).next();
if (nextImg.index()===-1) {
$("#scroller div").removeClass("on");
nextImg=$("#scroller div:eq(0)");
nextImg.addClass("on");
}
else {
nextImg.addClass("on");
$(".on:first").removeClass("on");
}
scrollImg()
});
}
/////




///BEGIN GALLERY JS
function makeGallery(numOfPics){
$("<div></div>").attr("id", "thumbnails").prependTo("#rightCol");
$("<ul>").appendTo("#thumbnails");
for (i=0;i<numOfPics;i++) {
$("<li>").appendTo("#thumbnails ul:first");
$("<div>").addClass("btns").text(i+1).appendTo("#thumbnails li:last");
}
btnHover(".btns","btnsOver");
$(".btns:first").addClass("btnsOn");
$("#thumbnails ul li").not("#thumbnails ul li:last").addClass("thumbMarginRight");
$liMarginRight=parseInt($("#thumbnails ul li").css("margin-right"));
$totalMargin=$liMarginRight*($("#thumbnails ul li:last").index()
);
$ulWidth=$("#thumbnails ul li").width()*numOfPics+($totalMargin);
$("#thumbnails ul").width($ulWidth);
$("#imgWrapper img:gt(0)").css("display", "none");
$("#imgWrapper img:first").addClass("activeImg");
$("#imgWrapper").find("img").wrapAll("<div></div>");
$("#imgWrapper div:first").attr("id", "picBox");
var imgOneWidth=$("#imgWrapper img:first").width();
var imgOneHeight=$("#imgWrapper img:first").height();
$("#picBox").width(imgOneWidth).height(imgOneHeight);
$("<div>").appendTo("#imgWrapper").attr("id", "toAndFroBtns");
$("<div></div><div></div>").appendTo("#toAndFroBtns");
$("#toAndFroBtns div:first").attr("id", "forwardBtn")
$("#toAndFroBtns div:last").attr("id", "backwardBtn");
$("<span>").appendTo("#toAndFroBtns");
btnHover("#forwardBtn","toAndFro_On");
btnHover("#backwardBtn","toAndFro_On");
$("#thumbnails").insertAfter("#toAndFroBtns")
currentImgText();
//
$(".btns").click(function() {
var $nuImgIndex=$(this).parent().index();
var $oldImg=$("#picBox img").filter(".activeImg").index();
//alert($nuImgIndex+"   "+$oldImg);
if($(this).hasClass("noClick")) {
return false;
}
if($nuImgIndex===$oldImg) {
//alert("NOPE");
return false;
}
$(".btns").removeClass("btnsOn");
$(this).addClass("btnsOn");
$(".btns").addClass("noClick");
$("#toAndFroBtns").find("div").addClass("noClick");
var $index=$(this).parent().index();
var $activeImg=$(".activeImg");
$activeImg.fadeOut("fast", function() {
$(this).removeClass("activeImg");
});
$activeImg=$("#picBox img").eq($index).addClass("activeImg");
var imgOneWidth=$activeImg.width();
var imgOneHeight=$activeImg.height();
$("#picBox").delay(300).animate({
    width: imgOneWidth,
    height: imgOneHeight
  }, "slow",function() {
$activeImg.fadeIn("slow");
$(".btns").removeClass("noClick");
$("#toAndFroBtns").find("div").removeClass("noClick");
//
currentImgText();
//

});
});
arrows();
}

///////////////////////////////////////////////////
function btnHover(theBtn,hoverClass) {
$theBtn=$(theBtn);
$theBtn.hover(function() {
$(this).addClass(hoverClass);
},
function() {
$(this).removeClass(hoverClass);
});
}

function setToAndFroBtns(activePic) {

var $btnHeight=$("#toAndFroBtns").height();
var $activePic=$(".activeImg");
var picHeight=$activePic.height();
var picWidth=$activePic.width();
$("#imgWrapper").find("img").each(function(i) {
picWidths[i]=$(this).width();
picHeights[i]=$(this).height();
});
}

////////////////


////////////
function arrows() {
$("#toAndFroBtns").find("div").click(function() {
if($(this).hasClass("noClick")) {
return;
}
$(".btns").removeClass("btnsOn");
$(".btns").addClass("noClick");
$("#toAndFroBtns").find("div").addClass("noClick");
var $activeImg=$(".activeImg");
var $index=$activeImg.index();
$activeImg.fadeOut("fast", function() {
$(this).removeClass("activeImg");
});
if ($(this).index()===1) {
$index=$index+1;
}
else {
$index=$index-1;
}
$activeImg=$("#picBox img").eq($index);
if($index>$("#picBox img:last").index()) {
$activeImg=$("#picBox img").eq(0);
}
if($index<0) {
$activeImg=$("#picBox img:last");
}
$activeImg.addClass("activeImg")

var imgOneWidth=$activeImg.width();
var imgOneHeight=$activeImg.height();
$("#picBox").delay(300).animate({
    width: imgOneWidth,
    height: imgOneHeight
  }, "slow",function() {
$activeImg.fadeIn("slow");
$("#toAndFroBtns").find("div").removeClass("noClick");
$(".btns").removeClass("noClick");
$(".btns").eq($activeImg.index()).addClass("btnsOn");
//
currentImgText();
//
});

});
}

////////////////////
function currentImgText() {
var imgLength=$("#imgWrapper img").length;
var $text=$("#imgWrapper img.activeImg").index()+1;
$("#toAndFroBtns span").text($text+"/"+imgLength);
}
///END GALLERY JS

function navUlBtns() {
$("#leftCol div:gt(0)").hide();
$("#navUl li a:eq(0)").addClass("active");
$("#navUl li a").each(function() {
$(this).attr("href", "#");
});
$("#navUl li a").click(function() {
$("#navUl li a").removeClass("active");
$(this).addClass("active");
$("#leftCol div").hide();
var $index=$(this).parent().index();
$("#leftCol div").eq($index).show();
})
}

function setTitle() {
$("#navUl li a").click(function() {
var $linkText=$(this).text();
$("title:eq(0)").html("::: "+$linkText+" :::");
});
}

function setCopyrightYear() {
var dateObj=new Date();
var theYear=dateObj.getFullYear();
$("#footer span.year").text(theYear);
 }

