(function(){
	
	var interval = null;

    var base_uri = '';

    var flv,video,page,nextPage,next,current,previous, workid,projectdetails,client_url,project_url,work_url,client,project,flash,params, work,url   = null;

	var fadeSpeed = 500000;

    var size = null;

	var pageSpeed = 10000;

    var fadeTo = function (opacityVal) {

            document.getElementById('html-video').style.opacity=opacityVal;

    }

    var startshowreel = function(){

        if ($('#front-page-carousel-items li.selected').length){

            flv =  $('#front-page-carousel-items li.selected').attr('id').split('-')[1];

            video = document.getElementById('flvpage-' + flv);

            if (video.runFlash){

                video.runFlash();

            }

            flv = null;
         }
    }

	var changePage = function(event) {

		event.preventDefault();

		pauseCarousel();

        $('#next-swf').unbind('click');

        $('#previous-swf').unbind('click');

		page = $(this).attr('class').split('-')[1];

		loadPage(page);

	} // end changePage

     var changePageFlash = function(){

	    page = $('#current-page').val();

        page = Number(page) + Number(1);

        if (page > size)

            page = 1;

		loadPage(page);

    }

     window.changePageFlash = changePageFlash;

	 nextPage = function(event) {

		next = $('#carousel-paginator li a.selected').parent().next();

		if (!next.length) {
			
			next = $('#carousel-paginator li.button:first');

		} // end if

		page = $('a', next).text();

		loadPage(page);


	} // end nextPage

	var loadPage = function(page) {

		current = $('#front-page-carousel-items li.selected').attr('id').split('-')[1];

		$('#front-page-carousel-items li').removeClass('selected');

		$('#front-page-carousel-items li#page-' + page  ).addClass('selected');

        next = Number(page) + Number(1);

        size = $('#carousel-size').val();

        if (next > size)

            next = 1;

        previous = Number(page) - Number(1);

        if (previous < 1)

            previous = size;

        $('#next-swf').removeClass();

        $('#next-swf').addClass('page-'+next);

        $('#previous-swf').removeClass();

        $('#previous-swf').removeClass();

        $('#previous-swf').addClass('page-'+previous);

        $('#current-page').val(page);

         $('#front-page-carousel-items li#page-' + current).css('display','none');

		 //$('#front-page-carousel-items li#page-' + current).fadeOut(fadeSpeed);


         //workid = $('#work-id-'+page).val();

         window.clientdetails = {};

         projectdetails ='0';

         client_url = $('#work-client-url-' + page).val();

         project_url = $('#work-project-url-' + page).val();

         work_url =  $('#work-client-url-' + page).val();

         client = $('#work-client-' + page).val();

         project = $('#work-project-' + page).val();

         $('span.client-details').html('<a href="'  + client_url + '">' + client + ' </a>');

         $('span.project-details').html('<a href="' + project_url + '">' + project + ' </a>');

         $('a#front-details').attr('href',work_url);

		 $('#front-page-carousel-items li#page-' + page).fadeIn('slow', function() {

            video = document.getElementById('flvpage-' + page);

            if (video.runFlash){

                video.runFlash();

                 $('#next-swf').click(changePage);

                 $('#previous-swf').click(changePage);
            }

		 });

         client_url = null;

         workid = null;

         work_url = null;

         client = null;

         project = null;

        // page = null;

	} // end loadPage



	var pauseCarousel = function() {

		$('#carousel-play img').attr('src', base_uri + '/images/icons/carousel-play.png');

		$('#carousel-play img').attr('alt', 'Play');

		$('#carousel-play').unbind('click', pauseCarousel);

		$('#carousel-play').bind('click', playCarousel);

		clearInterval(interval);

	} // end pauseCarousel

	var playCarousel = function() {

		interval = setInterval(nextPage, pageSpeed);
		
	} // end pauseCarousel

    var stopSpin = function(){

       $('#loading-div').hide();

        startshowreel();

    }

	var initShowreel = function() {

         params = { allowscriptaccess  : 'always',
                                    bgcolor: "#000000",
                                    wmode: "opaque",
                                    menu: false };

        $('#front-page-carousel-items li.front-banner').each(function(){

                    flv = $(this).attr('id').split('-')[1];

                    flash = $('#front-page-carousel-items li.front-banner #banner-' + flv).val();

					swfobject.embedSWF(flash, "flvpage-"+flv, "973", "313", "10.0.0", null, null, params);

                    flv = null;

                    flash = null;


		});

          $('#front-page-carousel-items li.front-banner').hover(function(){
                $(this).css("cursor","pointer");

          })
          $('#front-page-carousel-items li.front-banner').click(function(){

                workid = $(this).attr('id').split('-')[1];

                work = $('#work-id-'+workid).val();

                url = $('#work-project-url-' + workid).val();

                window.location.href = url;

          });


        $('#next-swf').click(changePage);

        $('#previous-swf').click(changePage);

	   	setTimeout(function(){ stopSpin()  },1500);

	} // end init

	$(document).ready(initShowreel);

})();
