/**
* Performs actions when the document is ready
*/
$(document).ready(function () {

    //---------- Set the font size cookie -----------------------------------------
    if (getCookie('font-size') != null) {
        $('body').removeClass('normal');
        $('body').removeClass('large');
        $('body').removeClass('largest');
        $('body').addClass(getCookie("font-size"));
    }

    else {
        setCookie("font-size", "normal");
    }

    // resize when 'A' is clicked
    $('#font-size-1').click(function (e) {
        e.preventDefault();

        $('body').removeClass('large');
        $('body').removeClass('largest');

        $('body').addClass('normal');
        setCookie("font-size", "normal");
    });

    $('#font-size-2').click(function (e) {
        e.preventDefault();

        $('body').removeClass('normal');
        $('body').removeClass('largest');

        $('body').addClass('large');
        setCookie("font-size", "large");
    });

    $('#font-size-3').click(function (e) {
        e.preventDefault();

        $('body').removeClass('normal');
        $('body').removeClass('large');

        $('body').addClass('largest');
        setCookie("font-size", "largest");
    });


    //---------- Load videos into the holder --------------------------------------
    $(".video-holder").click(function () {
        var videoid = $(this).children("div").html();
        $(this).replaceWith('<div class="video-holder-playing">\
		<object width="301" height="215">\
            <param name="allowfullscreen" value="true" />\
            <param name="allowscriptaccess" value="always" />\
            <param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + videoid + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" />\
            <embed src="http://vimeo.com/moogaloop.swf?clip_id=' + videoid + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="301" height="215"></embed>\
        </object>\
        </div>');
    });

    $(".testimonial-video-container").click(function () {
        $(this).replaceWith('<div class="testimonial-video-container-playing">\
		<object width="401" height="328"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=15385670&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=15385670&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="401" height="328"></embed></object></div>');
    });

    //---------- Enable who spotlight boxes to act as links -----------------------
    $('.spotlight').mouseover(function () {
        $(this).find('p a').css('text-decoration', 'underline');
    })
	.mouseout(function () {
	    $(this).find('p a').css('text-decoration', 'none');
	});


    $('.spotlight').click(function () {

        // Grab link from the header link
        var link = $(this).find('.spotlight-headings a').attr('href');
        window.location = link;
    });


    //---------- Academy image transition -----------------------------------------
    if ($('#gallery.ajax-gallery').length != 0) {

        // Retrieve image height so know how much to shift images by
        var height = $('#gallery').css('height');
        height = parseInt(height.substr(height, height.indexOf('px')));

        // Retrieve number of images		
        var numImages = $('.gallery-contents > div').size();
        var currPos = 1;

        // Create number navigation links
        for (var i = 1; i <= numImages; i++) {
            $('#prev-next-numbers').append('<li><a class="btn btn-' + i + (i == 1 ? ' prev-next-select' : '') + '" href="#">' + i + '</a></li>');
        }
        $('#prev-next-numbers li.placeholder').remove();

        // Detect a number navigation click and load correct image
        $('.btn').click(function (e) {

            e.preventDefault();
            var num = parseInt($(this).html());

            $('.prev-next-link').removeClass('inactive');
            $('.gallery-contents').fadeOut(300, function () {

                $('.gallery-contents').css('top', height * (num - 1) * -1).fadeIn(300);
                currPos = num;

                // If at beginning of list, make link inactive
                if (currPos == 1) {
                    $('.prev-next-left').addClass('inactive');
                }

                // If at end of list, make link inactive
                else if (currPos == numImages) {
                    $('.prev-next-right').addClass('inactive');
                }
            });

            // Assign the active class to correct link
            $('.btn').removeClass('prev-next-select');
            $(this).addClass('prev-next-select');
        });


        // Detect prev/next button click
        $('.prev-next-link a').click(function (e) {

            e.preventDefault();

            // Next clicked
            if ($(this).hasClass('next') && currPos < numImages) {

                currPos++;

                $('.gallery-contents').fadeOut(300, function () {
                    $('.gallery-contents').css('top', height * (currPos - 1) * -1).fadeIn(300);
                });

                $('.btn').removeClass('prev-next-select');
                $('.btn-' + currPos).addClass('prev-next-select');

                // If at end of list, make link inactive
                if (currPos == numImages) {
                    $('.prev-next-right').addClass('inactive');
                }

                // Stop the prev link being inactive
                $('.prev-next-left').removeClass('inactive');
            }

            // Prev clicked
            else if ($(this).hasClass('prev') && currPos > 1) {

                currPos--;

                $('.gallery-contents').fadeOut(300, function () {
                    $('.gallery-contents').css('top', height * (currPos - 1) * -1).fadeIn(300);
                });

                $('.btn').removeClass('prev-next-select');
                $('.btn-' + currPos).addClass('prev-next-select');

                // If at beginning of list, make link inactive
                if (currPos == 1) {
                    $('.prev-next-left').addClass('inactive');
                }

                // Stop the prev link being inactive
                $('.prev-next-right').removeClass('inactive');
            }
        });

    }



    //---------- Expand/contract image captions -----------------------------------

    // on page-load
    /*if(getCookie('updown-status') != null) {
		
    if(getCookie('updown-status') == 'up') {
    $('.caption').removeClass('down').addClass('up');
    $('.caption .updown a').html('Hide'); }
		
    else {
    $('.caption').removeClass('up').addClass('down');
    $('.caption .updown a').html('Show caption'); }
  		
    }*/
    // uncomment to use cookies to remember up-down status between pages


    // on click
    $('.updown a').click(function (e) {

        e.preventDefault();

        if ($(this).parent().parent().hasClass('up')) {

            $('.caption .updown a').html('Show caption');
            $('.caption').removeClass('up').addClass('down');
            setCookie('updown-status', 'down');
        }

        else {
            $('.caption .updown a').html('Hide');
            $('.caption').removeClass('down').addClass('up');
            setCookie('updown-status', 'up');
        }
    });



    //---------- Thumbnails page hover effect ------------------------------------

    // handle the mouse over event
    $('.websites-project-thumb').mouseover(function () {

        var id = $(this).attr('id');

        // hide general
        $('div#websites-project-general').hide();

        // show specific hover text
        $('div.' + $(this).attr('id')).show();
    })


    // now handle mouse out event
	.mouseout(function () {

	    // hide specific text
	    $('div.' + $(this).attr('id')).hide();

	    // show general
	    $('div#websites-project-general').show();
	});




    //---------- Websites transition -----------------------------------------=====
    if ($('#websites-thumbnails-wrapper').length != 0) {


        // Detect a number navigation click and load correct image
        $('.btn').click(function (e) {

            e.preventDefault();
            var num = parseInt($(this).html());


            // Slide left/right
            if (num == 1) {

                $('.websites-thumbnails-container').animate({
                    left: '0px'
                }, 500);

                $('.prev-next-right').removeClass('inactive');
                $('.prev-next-left').addClass('inactive');
            }

            else if (num == 2) {
                $('.websites-thumbnails-container').animate({
                    left: '-608px'
                }, 500);

                $('.prev-next-left').removeClass('inactive');
                $('.prev-next-right').addClass('inactive');
            }


            // Assign the active class to correct link
            $('.btn').removeClass('prev-next-select');
            $(this).addClass('prev-next-select');
        });


        $('.prev-next-left').click(function (e) {

            e.preventDefault();

            $('.prev-next-right').removeClass('inactive');
            $('.prev-next-left').addClass('inactive');

            $('.btn').removeClass('prev-next-select');
            $('.btn1').addClass('prev-next-select');

            $('.websites-thumbnails-container').animate({
                left: '0px'
            }, 500);
        });


        $('.prev-next-right').click(function (e) {

            e.preventDefault();

            $('.prev-next-left').removeClass('inactive');
            $('.prev-next-right').addClass('inactive');

            $('.btn').removeClass('prev-next-select');
            $('.btn2').addClass('prev-next-select');

            $('.websites-thumbnails-container').animate({
                left: '-608px'
            }, 500);
        });

    }




});




/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
