﻿


//*  ==  MODEL  ==============================================================  *

$(document).ready(function () {

    // animated rollovers
    $('.Site .entryFooter .morelink a').hover(
        function () { $(this).animate({ left: '4px' }, 150) },
        function () { $(this).animate({ left: '0px' }, 150) }
    );


    if ($('#mainAdminMenu').exists() == false) {
        $('body').addClass('js');

        galleryThumbList();
    }

    // initialise homepage promotion gallery
    promotionList();

    if ($('#ReviewForm').find('.alert').exists() || $('#ReviewForm .sentMessage').exists()) {
        openModal('#ReviewForm');
    }


    $('a[href="#ReviewForm"]').addClass('modalLink');
    //$('.modalContent h2').addClass('modal');\


    /*
    * SimpleModal Basic Modal Dialog
    * http://www.ericmmartin.com/projects/simplemodal/
    * http://code.google.com/p/simplemodal/
    *
    * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
    *
    * Licensed under the MIT license:
    *   http://www.opensource.org/licenses/mit-license.php
    *
    * Revision: $Id: basic.js 243 2010-03-15 14:23:14Z emartin24 $
    *
    */

    $('.modalLink').click(function (e) {
        e.preventDefault();

        // open relevant content in basic modal window
        openModal($(this).attr('href'));
        //_gaq.push(['_trackPageview', '/ReviewForm']);
        //return false;

    });


    // World Map controls
    $('area').hover(

    //alert();
        function () {
            var area = $(this).attr('class');
            $('#' + area).fadeIn();
        },
        function () {
            var area = $(this).attr('class');
            $('#' + area).fadeOut();
        }
    );

//    var zIndexNumber = 1000;
//    $('div').each(function () {
//        $(this).css('zIndex', zIndexNumber);
//        zIndexNumber -= 10;
//    });


    reviewToggle();

    zoomInitialiser();

});




function zoomInitialiser() {

    // Reset Font Size
    var originalFontSize = $('html').css('font-size');
    $(".resetFont").click(function () {
        $('html').css('font-size', originalFontSize);
    });
    // Increase Font Size
    $(".increaseFont").click(function () {
        var currentFontSize = $('html').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 1.2;
        $('html').css('font-size', newFontSize);
        return false;
    });
    // Decrease Font Size
    $(".decreaseFont").click(function () {
        var currentFontSize = $('html').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 0.8;
        $('html').css('font-size', newFontSize);
        return false;
    });

}




function galleryThumbList() {
    // setup for .GalleryImageList
    if ($('.thumbList').exists() == true) {

        // link hover hide and show
        $('.GalleryImageList .active').css('display', 'block');
        var gallery = $('.GalleryImageList');
        var imgLarge = $('.lightbox.active');

        centreItem(gallery, imgLarge);

        //        add indicator arrow graphic
        //        $('<span id="linkArrow"></span>').appendTo('#linkNav');
        //        linkArrowPos();

        // Create and setup background images for fade in and out
        $('.thumbList a').each(function () {

            var itemId = $(this).attr('id').replace(/tn/g, 'det');
            var itemImgName = $(this).attr('rel');

            //            $('<div id="homeBg' + itemId + '" class="homeBg"></div>').appendTo('#main');
            //            $('#homeBg' + itemId).css('background-image', 'url(' + itemImgName + ')');
            //            $('.homeBg:first').show();
        });

        $('.thumbList a').click(function (e) {
            e.preventDefault();
            var gallery = $(this).parents('.GalleryImageList');
            var imgLarge = gallery.find('.lightbox');
            var itemId = $(this).attr('id').replace(/tn/g, 'det');
            //          var linkOffset = $(this).offset().left;
            //          var linkWidth = ($(this).width() + 10) / 2;

            if (!$('#' + itemId).is(':visible')) {
                // fade all large images out
                imgLarge.each(function () {
                    $(this).fadeOut().removeClass('active');
                });
                //                $('.homeBg').each(function () {
                //                    $(this).fadeOut()
                //                });

                // locate the relevant large image and show
                $('#' + itemId).addClass('active').fadeIn();

                //              $('#homeBg' + itemId).fadeIn();

                //              linkArrowPos(linkOffset, linkWidth);

                //centre the large image in the gallery space
                centreItem(gallery, $('#' + itemId));
            }
        });

        // set link href to use the title attribute
        //        $('#linkNav a').each(function () {
        //            $(this).attr('href', $(this).attr('title'));
        //        });
    }
}

function centreItem(container, item) {

    var containerWidth = container.width();

    item.each(function () {
        var itemWidth = $(this).width();
        var offset = (containerWidth - itemWidth) / 2;

        item.css('left', offset + 'px');
    });
}

function linkArrowPos(offset, width) {

    var navOffset = $('#linkNav').offset().left;
    var linkNavOffset = offset;
    var linkNavWidth = width;
    var arrowWidth = $('#linkArrow').width() / 2;

    if (linkNavOffset == null) {
        linkNavOffset = $('#linkNav a:first').offset().left;
    }
    if (linkNavWidth == null) {
        linkNavWidth = ($('#linkNav a:first').width() + 10) / 2;
    }

    $('#linkArrow').animate({ left: linkNavOffset - navOffset - arrowWidth + linkNavWidth }, 200);
}

function menuPos(menu, position) {

    var parentWidth = $(menu).parent().width();
    var menuWidth = $(menu).width();

    if (position == 'right') {
        var pos = '1';
    } else if (position == 'centre') {
        var pos = '2';
    }
    $(menu).css('padding-left', (parentWidth - menuWidth) / pos)
}

function openModal(content) {
    $(content).modal({
        maxWidth: 460,
        minWidth: 230,
        maxHeight: 420,
        minHeight: 250,
        onClose: function (dialog) {
            dialog.data.fadeOut('400', function () {
                dialog.container.slideUp('400', function () {
                    dialog.overlay.fadeOut('400', function () {
                        $.modal.close(); // must call this!
                    });
                });
            });
        },
        onOpen: function (dialog) {
            dialog.overlay.fadeIn('400', function () {
                dialog.container.slideDown('400', function () {
                    dialog.data.fadeIn('400');
                });
            });
        },
        opacity: 80,
        overlayCss: { backgroundColor: "#fff" },
        overlayClose: true
    });
}

function promotionList() {
	// Initialise adGallery plugin
	if ($('.ad-gallery').exists()) {

		var galleries = $('.ad-gallery').adGallery({
			animation_speed: 1500,
			slideshow: {
				speed: 12500 // Timeout in ms
			}
		});

		$('#switch-effect').change(function () {
			galleries[0].settings.effect = $(this).val();
			return false;
		});

		$('#toggle-slideshow').click(function () {
			galleries[0].slideshow.toggle();
			return false;
		});
	}
}

function reviewToggle() {

    // target all review lists marked with a group class of 'reviews'
    var reviews = $('.reviews');
    var review = $('.hreview');
    var count = $('.hreview').length;

    // add hide/show button below review list
    if (count > 1) {
        $('<a href="#" class="principle">Show all reviews</a>').appendTo(reviews);
    }

    var toggleButton = $('.reviews .principle');

    // add a class to differentiate the first item in the list
    $('.hreview:first').addClass('first');

    toggleButton.click(function (e) {
        e.preventDefault();
        var button = $(this);
        var countVisible = $('.hreview:visible').length;

        if (countVisible < 2) {
            review.slideDown(function () {
                button.html('Hide all reviews');
            });
        } else {
            review.not('.first').slideUp(function () {
                button.html('Show all reviews');
            });
        }
    });
}

/*
* 
* ImageScroller - a Image Horizental Scroll Viewer 
* Version 0.1
* @requires jQuery v1.2.1
* 
* Copyright (c) 2007 Luan
* Email verycss-ok@yahoo.com.cn 
* 
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* 
* Example:
*   #viewer {height:100px; width:300px; clear:both; overflow:hidden; border:3px solid #e5e5e5;}
*   #viewerFrame {width:505px; clear:both; padding:0;}
*   #viewer img {width:90px; height:90px; margin:5px; display:inline; border:0;}
*   #viewer a {display:block; float:left; width:100px; height:100px;}
*   <script type="text/javascript">
*   $(function(){
*   	$("#viewer").imageScroller({
*   	next:"btn1",
*   	prev:"btn2",
*   	frame:"viewerFrame",
*   	width:100, 
*   	child:"a",
*   	auto:true
*   	});	 
*   });
*   </script>
*   <div id="viewer"><div id="viewerFrame">
*   <a href=""><img src="pre1.jpg"></a>
*   <a href=""><img src="pre2.jpg"></a>
*   <a href=""><img src="pre3.jpg"></a>
*   <a href=""><img src="pre4.jpg"></a>
*   <a href=""><img src="pre5.jpg"></a>
*   </div></div>
*   <span id="btn1">prev</span><br/><span id="btn2">next</span>   
*/

$.fn.imageScroller = function (params) {
    var p = params || {
        next: "buttonNext",
        prev: "buttonPrev",
        frame: "viewerFrame",
        width: 100,
        child: "a",
        auto: false
    };
    var _btnNext = $("#" + p.next);
    var _btnPrev = $("#" + p.prev);
    var _imgFrame = $("#" + p.frame);
    var _width = p.width;
    var _child = p.child;
    var _auto = p.auto;
    var _itv;

    var turnLeft = function () {
        _btnPrev.unbind("click", turnLeft);
        if (_auto) autoStop();
        _imgFrame.animate({ marginLeft: -_width }, 'fast', '', function () {
            _imgFrame.find(_child + ":first").appendTo(_imgFrame);
            _imgFrame.css("marginLeft", 0);
            _btnPrev.bind("click", turnLeft);
            if (_auto) autoPlay();
        });
    };
    var active;
    var turnRight = function () {
        _btnNext.unbind("click", turnRight);
        if (_auto) autoStop();

        // [eonic 2011-06-16] additional script to show hide large version of context thumbnail
        _imgFrame.find(_child).removeAttr('class');
        $('.lightbox').fadeOut(400);
        // [eonic end]

        _imgFrame.find(_child + ":last").clone().show().prependTo(_imgFrame)

        // [eonic 2011-06-16] additional script to add active class
        .addClass('active');
        var activeLarge = _imgFrame.find(_child + ":first").attr('id').replace(/tn/g, 'det');
        $('#' + activeLarge).fadeIn(400);
        // [eonic end]

        _imgFrame.css("marginLeft", -_width);
        _imgFrame.animate({ marginLeft: 0 }, 'fast', '', function () {
            _imgFrame.find(_child + ":last").remove();
            _btnNext.bind("click", turnRight);
            if (_auto) autoPlay();
        });
    };

    _btnNext.css("cursor", "hand").click(turnRight);
    _btnPrev.css("cursor", "hand").click(turnLeft);

    var autoPlay = function () {
        _itv = window.setInterval(turnRight, 3000);
    };
    var autoStop = function () {
        window.clearInterval(_itv);
    };
    if (_auto) autoPlay();
};
