/// <reference path="/msbuild/jquery-1.4.4-vsdoc.js" />

/*
*	Hesta Healthcare Awards
*	Author Adam Phin
*   Copyright © 2011 Hesta
*   All rights reserved.
*/

var $overlay;


// Slideshow & Carousel variables
var clicked = false;


var hesta = {
    init: function () {
        hesta.cufon.init();
        hesta.navigation.init();
        hesta.promptTexts.init();
        hesta.tooltips.init();
        hesta.overlay.init();
        hesta.slideshow.init();
		hesta.gallery.init();


        $(document).ready(function () {
            hesta.cufon.ready();



            $('textarea.wordLimited').maxlength({
                'feedback': '.wordsLeft',
                'words': true,
                'length': 100,
                'useInput': true
            });


            $('textarea.wordLimitedA').maxlength({
                'feedback': '.wordsLeftA',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedB').maxlength({
                'feedback': '.wordsLeftB',
                'words': true,
                'length': 600,
                'useInput': true
            });


            $('textarea.wordLimitedC').maxlength({
                'feedback': '.wordsLeftC',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedD').maxlength({
                'feedback': '.wordsLeftD',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedE').maxlength({
                'feedback': '.wordsLeftE',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedF').maxlength({
                'feedback': '.wordsLeftF',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedG').maxlength({
                'feedback': '.wordsLeftG',
                'words': true,
                'length': 600,
                'useInput': true
            });
           

           $('textarea.wordLimitedH').maxlength({
                'feedback': '.wordsLeftH',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedI').maxlength({
                'feedback': '.wordsLeftI',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedJ').maxlength({
                'feedback': '.wordsLeftJ',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedK').maxlength({
                'feedback': '.wordsLeftK',
                'words': true,
                'length': 600,
                'useInput': true
            });

            $('textarea.wordLimitedL').maxlength({
                'feedback': '.wordsLeftL',
                'words': true,
                'length': 600,
                'useInput': true
            });


        });
    },

    cufon: {
        init: function () {
            Cufon.replace('h1:not(.nocufon)', { fontFamily: 'FrutigerLight' });
            Cufon.replace('.nominatePanel h1', { fontFamily: 'MyriadProRegular' });
            Cufon.replace('h2:not(.nocufon)', { fontFamily: 'MyriadProRegular' });
            Cufon.replace('.nominateBar p', { fontFamily: 'MyriadProRegular' });
        },
        ready: function () {
            Cufon.now();
        }
    },

    navigation: {
        init: function () {
            var $nav = $('#nav');

            $("ul.primaryNav > li a").not("ul.primaryNav li ul a").wrap($('<p></p>'));
            $("ul.primaryNav > li p").not("ul.primaryNav li ul a").prepend($('<span></span>'));
            $("ul.primaryNav > li a").not("ul.primaryNav li ul a").after($('<strong></strong>'));

            $('.primaryNav > li', $nav).hover(function (e) {
                hesta.navigation.navOver(this);
            }, function () {
                hesta.navigation.navOut(this);
            });
            //fix for ie7 nav jump
            $("#nav .primaryNav").css({ 'display': 'block' });
        },
        navOver: function ($item) {
            $('p a', $item).css({ background: 'url(/images/navTabCenterBG.png) 0 0 repeat-x' });
            $('span', $item).css({ background: 'url(/images/navTabLeftBG.png) no-repeat' });
            $('strong', $item).css({ background: 'url(/images/navTabRightBG.png) 0 0 no-repeat' });
            $('> .secondaryNav, ul', $item).css({ display: 'block' });
        },
        navOut: function ($item) {
            $('p a', $item).css({ background: 'none' });
            $('span', $item).css({ background: 'none' });
            $('strong', $item).css({ background: 'url(/images/navDivider.png) no-repeat right' });
            $('.secondaryNav, ul', $item).css({ display: 'none' });
        }
    },

    promptTexts: {
        init: function () {
            // SerachBar
            $('#header .siteSearchInput').val('Search');

            $('#header .siteSearchInput').bind('focus', function () {
                if ($(this).val() == 'Search') {
                    $(this).val('');
                }
            });
            $('#header .siteSearchInput').bind('blur', function () {
                if ($(this).val() == '') {
                    $(this).val('Search');
                }
            });

            // Name

            $('.name').val('Enter your full name');

            $('.name').bind('focus', function () {
                if ($(this).val() == 'Enter your full name') {
                    $(this).val('');
                }
            });
            $('.name').bind('blur', function () {
                if ($(this).val() == '') {
                    $(this).val('Enter your full name');
                }
            });

            // Email
            $('.email').val('Enter your email address');

            $('.email').bind('focus', function () {
                if ($(this).val() == 'Enter your email address') {
                    $(this).val('');
                }
            });
            $('.email').bind('blur', function () {
                if ($(this).val() == '') {
                    $(this).val('Enter your email address');
                }
            });
        }
    },

    tooltips: {
        init: function () {

            var emailTooltip = $('#emailTooltip');
            var recommendTooltip = $('#recommendTooltip');

            // Subscribe 			
            $('#subscribeButton').hover(function () {
                emailTooltip.css({ display: 'block' });
            }, function () {
                emailTooltip.css({ display: 'none' });
            });

            // Email a Friend 			
            $('#recommendButton').hover(function () {
                recommendTooltip.css({ display: 'block' });
            }, function () {
                recommendTooltip.css({ display: 'none' });
            });
        }
    },


    overlay: {
        init: function () {
            $('body').append('<div id="overlay"/>');
            $overlay = $('#overlay');
        },
        show: function () {
            $overlay.fadeTo(1, 0.7);
        },
        hide: function () {
            $overlay.hide();
        }
    },
	
	dropdown: {
        init: function (item) {
            item.each(function () {
                var defaults = {
                    showTextClearButton: false,
                    debugMode: false
                };

                var options = $.extend(defaults, options);

                var $obj = $(this);

                var selected = $('option:selected', $obj);

                var items = $("option", $obj);
                $obj.after('<dl id="chisholmform' + $obj.attr('id') + '" class="dropdown"></dl>');
                var target = $('#chisholmform' + $obj.attr('id'));
                target.append('<dt><a href="#">' + selected.text() + '</a></dt>');
                target.append('<dd><ul></ul></dd>');
                if (!options['debugMode']) { $obj.hide(); }

                items.each(function (i) {
                    var lineClass = "";
                    if (selected.text() == $(this).text()) {
                        lineClass = "selected";
                    }
                    $("dd ul", target).append('<li class="' + lineClass + '"><a href="#"><span class="text">' +
						 $(this).text() + '</span><span class="value">' +
						 $(this).val() + '</span></a></li>');
                });

                $('dt a', target).bind('click', function () {
                    $(".dropdown").css({ "z-index": "1000" });
                    target.css({ "z-index": "1001" });
                    $('ul', target).slideDown('fast').trigger('focus');
                    return false;
                });

                $(target).bind('mouseleave', function () {
                    var timeoutTarget = $('ul', target);
                    var timeoutTargetSrc = $('dt a', this);
                    dropDowntimer = setTimeout(function () {
                        timeoutTarget.slideUp('fast');
                    }, 200);
                });

                $('ul', target).bind('mouseenter', function () {
                    if (typeof dropDowntimer != 'undefined')
                        clearTimeout(dropDowntimer);
                });

                $("dd ul li a", target).bind('click', function () {
                    var text = $('.text', this).html();
                    $("dt a", target).html(text);
                    $("dd ul", target).fadeOut(200);
                    $('dd ul li.selected', target).removeClass('selected');
                    $(this).parent().addClass('selected');
                    $obj.val($(this).find("span.value").html());
                    $obj.change();
                    return false;
                });
            });
        }
    },

    slideshow: {
        init: function () {
           


            // Creat Slide Indicator
                        $('#slideContainer').after('<div id="slideNavCont"><ul id="slideNav">').cycle({
                            fx: 'fade',
                            speed: 1000,
                            timeout: 5000,
                            pager: '#slideNav',
                            pagerAnchorBuilder: function (idx, slide) {
                                idx = idx + 1;
                                return '<li><a href="#">' + idx + '</a></li>';
                            }
                        });





		}
    },
	gallery: {
        init: function () {
			
			hesta.dropdown.init($('#gallery select'));
			
			/*$("a#inline").fancybox({
				'overlayOpacity' : '0.8',
				'overlayColor' : '#000'
				});*/
				
			$("a.downloadBtn").fancybox({
				'width'				: 610,
				'height'			: 720,
				'autoScale'     	: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe',
				'overlayOpacity' : '0.8',
				'overlayColor' : '#000'
			});
		
			var galleryObj = new Object();
            galleryObj.slides = $('#gallerySlides');
			galleryObj.nav = $('#galleryNav');
			galleryObj.image = $('#galleryImage');
			galleryObj.bar = $('#gallerySlides .galleryBar');
            galleryObj.currentPage = 1;
            galleryObj.nextPage = 2;
			galleryObj.pageSection = 0;
			galleryObj.pageSections = 0;
            galleryObj.totalPages = galleryObj.nav.find('li').length;
		
			if (galleryObj.totalPages > 0) {
				$single = $("li",galleryObj.nav).filter(':first');
				singleWidth = $single.outerWidth();
				galleryObj.singleWidth = singleWidth;
								
				/*$('li', galleryObj.slides).each(function () {
					$(this).addClass("p" + ($(this).index() + 1));
				});*/
				
				
				var totalNavWidth = 0;
				$('li', galleryObj.nav).each(function () {
					$(this).addClass("p" + ($(this).index() + 1));
					if($(this).index() < 6) {
						$(this).addClass("isVisible");
					}
					totalNavWidth += $(this).width();
				});
				
				$("ul", galleryObj.nav).width( ($("li", galleryObj.nav).width() + 8) * $("li", galleryObj.nav).length );
				
				galleryObj.pageSections = Math.ceil(galleryObj.totalPages / 6) - 1;
				
				$('li.p1', galleryObj.nav).addClass('active');
				$('li .galleryBar', galleryObj.slides).css({ 'display': 'block' });	
        	}			
			
			window.galleryObj = galleryObj;
			
			
			var link = $(".active a", window.galleryObj.nav).attr('rel');
			var imgURL = link;
			var downloadURL = '/news/gallery/download-form.html?highres=' + link.split("?")[0];
						 
			window.galleryObj.image.html("<img src='" + link +"' />");
			$(".downloadBtn" , galleryObj.bar).attr("href", downloadURL); 
				
			
			// Change Arrows			   
			$('.nextBtn', galleryObj.slides).bind('click', function (e) {
				e.preventDefault();
				window.galleryObj.nextPage = ((window.galleryObj.currentPage +1) > window.galleryObj.totalPages) ? 1 : (window.galleryObj.currentPage +1);
				hesta.gallery.highlightThumb(window.galleryObj.nextPage);
				hesta.gallery.changeSlide('NEXT');
				hesta.gallery.positionCheck(window.galleryObj.nextPage);
				window.galleryObj.currentPage = window.galleryObj.nextPage
				

			});
			
			$('.backBtn', galleryObj.slides).bind('click', function (e) {
				e.preventDefault();
				window.galleryObj.nextPage = ((window.galleryObj.currentPage -1) < 1) ? window.galleryObj.totalPages : (window.galleryObj.currentPage -1);
				hesta.gallery.highlightThumb(window.galleryObj.nextPage);
				hesta.gallery.changeSlide('BACK');
				hesta.gallery.positionCheck(window.galleryObj.nextPage);
				window.galleryObj.currentPage = window.galleryObj.nextPage;
			});
			
			// Scroll Arrows		
			$('.nextBtn', galleryObj.nav).bind('click', function (e) {
				e.preventDefault();
				hesta.gallery.updateThumbs('NEXT');
			});
			
			$('.backBtn', galleryObj.nav).bind('click', function (e) {
				e.preventDefault();
				hesta.gallery.updateThumbs('BACK');
			});
			
			// Image thumbs			
			$('li',galleryObj.nav).bind('click', function (e) {
				e.preventDefault();				
				var index = $(this).attr("class").split("p")[1];
				hesta.gallery.highlightThumb(parseFloat(index));
				var dir = "BACK";
				if(index > window.galleryObj.currentPage) {
					dir = "NEXT"
					window.galleryObj.nextPage = ((index) > window.galleryObj.totalPages) ? 1 : (parseFloat(index));
				} else {
					window.galleryObj.nextPage = ((index) < 1) ? window.galleryObj.totalPages : (parseFloat(index));
				}
				hesta.gallery.changeSlide(dir);
				window.galleryObj.currentPage = window.galleryObj.nextPage;
			});

        },
		changeSlide: function(dir) {
			 var galleryObj = window.galleryObj;
			 if(galleryObj.currentPage == galleryObj.nextPage) return;
			 
			 var link = $(".active a", window.galleryObj.nav).attr('rel');
			 var imgURL = link;
			 var downloadURL = '/news/gallery/download-form.html?highres=' + link.split("?")[0];
						
			 if(dir == "NEXT") {
				window.galleryObj.image.html("<img src='" + link +"' />");
				$(".downloadBtn" , galleryObj.bar).attr("href", downloadURL); 
				
			 } else {
				window.galleryObj.image.html("<img src='" + link +"' />");
				$(".downloadBtn" , galleryObj.bar).attr("href", downloadURL); 
			 }
		},
		updateThumbs: function(dir) {
			var galleryObj = window.galleryObj;
			if(dir == "NEXT") {
				galleryObj.pageSection = ((galleryObj.pageSection +1) <= galleryObj.pageSections) ? (galleryObj.pageSection +1) : 0;
				$('ul', galleryObj.nav).animate({'left' : -(galleryObj.pageSection * (galleryObj.nav.width() -58))}, {duration:300, easing:'easeOutCubic'});
				
			} else {
				galleryObj.pageSection = ((galleryObj.pageSection -1) < 0) ? galleryObj.pageSections : (galleryObj.pageSection -1);
				$('ul', galleryObj.nav).animate({'left' : -(galleryObj.pageSection * (galleryObj.nav.width() -58)) }, {duration:300, easing:'easeOutCubic'});
			}
		},
		
		highlightThumb: function(index) {
			var galleryObj = window.galleryObj;
			$('.p' + index, galleryObj.nav).addClass('active');
			$('li', galleryObj.nav).filter(':not(.p' + index + ')').removeClass('active');
		},
		
		positionCheck: function(selectedPage) {		
			var page = (Math.ceil(selectedPage / 6) - 1);
			$('ul', galleryObj.nav).animate({'left' : -(page * (galleryObj.nav.width() -58))}, {duration:300, easing:'easeOutCubic'});
		}
	}
};




hesta.init();
