/* Global vars */
var currentPageIndex = -1;
var rotatorClickCount = 0;

jQuery(function() {
	$(".nav-1 .li-1").hover(function() {
		$(this).children(".link-1").attr("class", "link-1-on");
		$(this).children(".nav-2").show();
	}, function() {
		$(this).children(".link-1-on").attr("class", "link-1");
		$(this).children(".nav-2").hide();
	});

	//Page click handlers
	//------------------
	$('#btnBookmark').click(function() {
		var isChrome = /chrome/.test(navigator.userAgent.toLowerCase());
		var isSafari = /safari/.test(navigator.userAgent.toLowerCase());
		if (isChrome)
			alert('Sorry! Google Chrome doesn\'t support this feature. You can bookmark this page by clicking the star icon next to the address bar.');
		else if (isSafari)
			alert('Sorry! Safari doesn\'t support this feature. You can bookmark this page by clicking the plus (+) icon next to the address bar.');
		else
			CreateBookmarkLink(document.title, window.location);
	});
	$(".login").fancybox({
		width: 400,
		height: 245,
		padding: 0,
		overlayShow: false,
		centerOnScroll: false
	});
	$(".quickreg").fancybox({
	    width: 500,
	    height: 255,
		padding: 0,
		overlayShow: false,
		centerOnScroll: false
	});
	/*
	$("#btnSendToFriend").fancybox({
		frameWidth: 540,
		frameHeight: 480,
		padding: 0,
		overlayShow: false,
		centerOnScroll: false
	});

	//http://fancybox.net/howto
	$("a.quickread").fancybox({
		frameWidth: 490,
		frameHeight: 550,
		padding: 0,
		overlayShow: false,
		centerOnScroll: true
	});
	
*/
});

/*  Helper Methods  */
function $$(id, context) {
    var el = jQuery("#" + id, context);
    if (el.length < 1)
        el = jQuery("[id$=_" + id + "]", context);
    return el;
}

function MakeDropCap(wrapperID) {
  var first_paragraph = $('#' + wrapperID + ' p')[0];
  if (!first_paragraph) return false;
  var node = first_paragraph;
  while (node.childNodes.length) {
    node = node.firstChild;
  }
  var text = node.nodeValue;
  var first_letter = text.substr(0,1);
  var match = /[a-zA-Z]/.test(first_letter);
  if ( match ) {
    node.nodeValue = text.slice(1);
    $('<div>' + first_letter.toUpperCase() + '</div>')
        .addClass('firstLetter')
        .prependTo(first_paragraph);
  }
}

function CreateBookmarkLink(pageTitle, url) {
    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(pageTitle, url, "");
    } else if (window.external) { // IE Favorite
        window.external.AddFavorite(url, pageTitle);
    }
    else if (window.opera && window.print) { // Opera Hotlist
        return true;
    }
}

function ShowingLoadingPanel(sender, args) {
    args.set_cancelNativeDisplay(true);
    $telerik.$(args.get_loadingElement()).fadeIn("fast");
}
function HidingLoadingPanel(sender, args) {
    args.set_cancelNativeDisplay(true);
    $telerik.$(args.get_loadingElement()).fadeOut("fast");
}

(function($) {
    // Vertically Align function
    $.fn.vAlign = function() {
        return this.each(function(i) {
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = ((ph - ah) / 2) - 3;
            $(this).css('margin-top', mh);
        });
    };

    $.fn.removeCss = function(cssName) {
        return this.each(function() {
            var curDom = $(this);
            jQuery.grep(cssName.split(","),
                    function(cssToBeRemoved) {
                        curDom.css(cssToBeRemoved, '');
                    });
            return curDom;
        });
    };

})(jQuery);

//Slidewhow
function InitSlideshow() {
    var rotatorContainer = $('#slideshow-photos').cycle({
        fx: 'fade',
        speed: 300,
        timeout: 0,
        after: function(curr, next, opts) {
            var ele = $("#" + next.id);
            var index = opts.currSlide;
            var slidesCount = opts.slideCount;

            //Set element values
            if (ele.attr('credit') == '')
                $("#slideshow-image-credits").html('');
            else
                $("#slideshow-image-credits").html("Image credit: " + ele.attr('credit'));


            $("#photo-deck").html(ele.attr('rel'));
            $("#slideshow-total-slides").html(opts.slideCount);
            $("#slideshow-current-index").html(index + 1);

            $("#photo-title").html(ele.attr('title'));
            $("#photo-title").removeClass('sIFR-replaced');

            //Unbind any existing click handlers
            $("#slideshow-prev").unbind('click');
            $("#slideshow-next").unbind('click');

            //Bind new click event
            $("#slideshow-prev").click(function() {
                var cycleTo = index - 1;
                if (cycleTo <= -1)
                    cycleTo = slidesCount - 1;

                rotatorContainer.cycle(cycleTo);
                return false;
            });

            //Bind new click event
            $("#slideshow-next").click(function() {
                var cycleTo = index + 1;
                if (cycleTo >= slidesCount)
                    cycleTo = 0;

                rotatorContainer.cycle(cycleTo);
                return false;
            });
        }
    });
}

/* Tabbed Image Rotator */
function InitRotator() {

    var rotatorContainer = $('#rotator-images').cycle({
        fx: 'fade',
        speed: 300,
        timeout: 8000,
        pause: true,
        pagerEvent: 'mouseover',
        before: function() {
            
            if (rotatorClickCount > 0)
                rotatorContainer.cycle('pause');

            currentPageIndex++;
            
            if (currentPageIndex > 2)
                currentPageIndex = 0;
            
            var tabs = $("#rotator-tabs-wrapper a");
            
            tabs.removeClass("activeTab");
            tabs.filter(":eq(" + currentPageIndex + ")").addClass("activeTab");

            var indexCount = 0;
            $("#rotator-tabs-wrapper").find("a.rotator-tab").each(function() {
                var currentAnchor = $(this);

                if (indexCount == currentPageIndex) {
//                    hideElement(currentAnchor.find("span.hadrianBlue:first"));
//                    hideElement(currentAnchor.find("h4:first"));

//                    showElement(currentAnchor.find("span.hadrianWhite:first"));
//                    showElement(currentAnchor.find("h4.current:first"));

//                    currentAnchor.addClass('current');
                }
                else {

//                    showElement(currentAnchor.find("span.hadrianBlue:first"));
//                    showElement(currentAnchor.find("h4:first"));

//                    hideElement(currentAnchor.find("span.hadrianWhite:first"));
//                    hideElement(currentAnchor.find("h4.current:first"));

//                    currentAnchor.removeClass('current');
                }

                indexCount++;
            });

        },
        after: function(curr, next, opts) {
            
            var ele = $("#" + next.id + " img.rotator-img");
            //Set element values
            if (ele.attr('credit') == '')
                $("#rotator-image-credits" + currentPageIndex).html('');
            else
                $("#rotator-image-credits" + currentPageIndex).html("Image credit: " + ele.attr('credit'));
        }
    });

    $("#rotator-tabs-wrapper").find("a.rotator-tab").each(function(intIndex) {
        //Vertically align the tab's content
        //$(this).find(".content").each(function() { $(this).vAlign(); });
        //$(this).find(".tab-number").each(function() { $(this).vAlign(); });

        $(this).hover(function() {
            rotatorClickCount++;
            currentPageIndex = (intIndex - 1);
            rotatorContainer.cycle(intIndex);
            return false;
        });
    });

    //Animate the image caption on hover
//    $('.boxgrid.peek').hover(function() {
//        $(".cover", this).stop().animate({ top: '194px' }, { queue: false, duration: 160 });
//    }, function() {
//        $(".cover", this).stop().animate({ top: '252px' }, { queue: false, duration: 160 });
//    });

//    $('.single-image.peek').hover(function() {
//        $(".cover", this).stop().animate({ top: '252px' }, { queue: false, duration: 160 });
//    }, function() {
//        $(".cover", this).stop().animate({ top: '310px' }, { queue: false, duration: 160 });
//    });
    //Disable the text on each tab from being selected
    $("#rotator-tabs-wrapper span.tabdeck").disableTextSelect();
}
function hideElement(ele) {
    $(ele).css('left', '-9999px').css('position', 'absolute');
}

function showElement(ele) {
    $(ele).css('left', '0').css('position', 'relative');
}
