(function(){

	var boxHeight = null;

	var sendDialog = null;

    var base_uri = '';  

	var initSendToAFriend = function() {
		
		if ($('#send-to-a-friend').length > 0) {
			
			$.post(base_uri + '/send', null, function(response) {
																		
				$('#main-block').append(response);
				
				$('#send-dialog form').bind('submit', sendSubmit);
																		
				var options = {
					
					autoOpen : false,
					bgiframe : true,
					dialogClass : 'send-to-a-friend',
					id : 'send-to-a-friend',
					width: 420,
					minHeight : 0,
					draggable : true,
					modal : true,
					title : 'Send To A Friend'
				};
			
				sendDialog = $('#send-dialog').dialog(options);
					
				$('#send-to-a-friend').show();
				
				$('#send-to-a-friend').live('click', function(){

					$('.errors', sendDialog).html('');
	
					$('#name', sendDialog).val('Name');

					$('#email', sendDialog).val('Email address');
				
					$('#message', sendDialog).val('Enter your message');
	
					$('#name', sendDialog).addClass('default');
	
					$('#email', sendDialog).addClass('default');
				
					$('#message', sendDialog).addClass('default');
	
					$('form', sendDialog).show();
	
					sendDialog.dialog('open');
																											
				});
	
				$('.CmsForm li input.default,.CmsForm li textarea.default').bind('focus', removeDefault);
			
			}); // end ajax
			
		} // end if
		
	} // end sentToAFriend

	var removeDefault = function() { 
				
		$(this).val(''); 
		
		$(this).removeClass('default');
		
		$(this).unbind('focus', removeDefault);
																																				
	} // end removeDefault

	var sendSubmit = function(event) {
		
		event.preventDefault();
		
		$('.errors', sendDialog).html('');
		
		var form = this;
		
		var data = {
			
			name : $('#name', this).hasClass('default') ? '' : $('#name', this).val(),

			email : $('#email', this).hasClass('default') ? '' : $('#email', this).val(),
			
			url : $('#page-url').val(),

			message : $('#message', this).hasClass('default') ? '' : $('#message', this).val()

		} // end if

		$.post(base_uri + '/send/post', data, function(response) {

			if (response.split("|")[0] != "0") {

				$(form).hide();
				
				$('.errors', sendDialog).html(response);

				$('.close-dialog').one('click', function() {

					sendDialog.dialog('close');
																					
				});

			} else {

				$('.errors', sendDialog).html(response.split("|")[1]);

			} // end if

		});

	} // end sendSubmit

   /*	var initBookmarks = function() {
		
		$('.bookmark-container').show();

		$('.bookmark-container').live('click', showBookmarks);

		var bookmark_url = $('#page-url').val();

		$.post(base_uri + '/bookmark', { 'url' : bookmark_url }, function(data) {

			$('#main-block').append(data);

			boxHeight = $('#bookmarks-box').height() + 20;

		});
		
	} // end initBookmarks    */

	var showBookmarks = function() {

		var loc = $('img', this).position();
		
		var top = loc.top - boxHeight - 30;

		var left = loc.left + 5;

		$('#bookmarks-box').css('top', top);

		$('#bookmarks-box').css('left', left);
		
		$('#bookmarks-box').fadeIn(800, function(){
																						 
			$(document).bind('click', hideBookmarks);
			
		});

	} // end showBookmarks

	var hideBookmarks = function() {

		$('#bookmarks-box').fadeOut(800);
														 
		$(document).unbind('click', hideBookmarks);
		
	};
	
	var sendSignup = function(event) {
		
		event.preventDefault();
		
		var form = this;
		
		var data = {
			
			email : $('#MERGE0', this).hasClass('default') ? '' : $('#MERGE0', this).val()

		} // end if

		$.post(base_uri + '/signup/post', data, function(response) {
				
			if ($(response).find('.errorText').length > 0) {
				
				if ($('.errors', form).length == 0) {
					
					$(form).prepend('<ul class="errors"></ul>');
					
				} // end if
				
				$('.errors', form).html('');
				
				var errors = $('.errors', form);
				
				$(response).find('.errorText').appendTo(errors);
				
		 } else {
			 
			 $('#newsletter-signup').html(response);
			 
		 } // end if

		});

	} // end sendSubmit

	var initEnquiryForm = function() {

          /*$('#enq-form').html5form({
                async: false,
                method: 'POST',
                colorOn: '#FFFFFF',
                colorOff: '#999',
                allBrowsers: true

          });*/

          $('#enq-form input').placeholder();

          $('#enq-form textarea').placeholder();
		
		$('label[for="name"]', this).html('Name: <span>*</span>');

		if ($('#type', this).val() == 'email') {
			
			$('label[for="contact_details"]', this).html('Email: <span>*</span>');

		} else {
			
			$('label[for="contact_details"]', this).html('Phone: <span>*</span>');

		} // end if
		
		$('#type-email', this).click(function() {
			
			if (this.checked) {

				$('label[for="contact_details"]').html('Email: <span>*</span>');

			} // end if
																					 
		});

		$('#type-phone', this).click(function() {
				
			if (this.checked) {
				
				$('label[for="contact_details"]').html('Phone: <span>*</span>');
				
			} // end if
			
		});
			
	}


	var row = 0;

	var col = 0;

	// the lower it is the faster the fade
	
	var fadeSpeed = 1000;
	
	// the lower it is the fashter the transaition
	
	var pageSpeed = 5000;

	var nextClient = function() {

		// get the current client

		row = $('#list-' + row).length ? row : 0;

		col = $('#list-' + row + ' .nth-child-' + col).length ? col : 0;

		var current = $('#list-' + row + ' .nth-child-' + col + ' img');
		
		var show = $('#list-' + (row + 1) + ' .nth-child-' + col).length ? $('#list-' + (row + 1) + ' .nth-child-' + col + ' img') : $('#list-0 .nth-child-' + col + ' img');
		
		showClient(current, show);
	
		col++;

		if ($('#list-' + row + ' .nth-child-' + col).length == 0) {

			row++;
			
			col = 0;
			
		} // end if

	} // end nextPage

	var showClient = function(current, show) {

		current.fadeOut(fadeSpeed);
		
		show.fadeIn(fadeSpeed);
		
	} // end loadPage




	var initClientList = function() {
		
		interval = setInterval(nextClient, pageSpeed);

	} // end initClientList

	var init = function() {

		//initBookmarks();
		
		initSendToAFriend();

        $("input[name=name], input[name=contact_details], textarea[name=message]").focus(function(){

            var defaultText = $(this).val();

            if(defaultText === "Name" || defaultText === "Email" || defaultText === "Message" || defaultText === "Telephone"){

                 $(this).select();

            }

        });
        $("input[name=name], input[name=contact_details], textarea[name=message]").blur(function(){
            var element = $(this);

            if (element.val() == ''){

                if (element.attr('id') == 'name'){

                    element.val('Name');

                }
                if (element.attr('id') == 'telephone-number' && ($('#type-email').is(':checked')) ){

                    element.val('Email');

                }
                if (element.attr('id') == 'telephone-number' && ($('#type-phone').is(':checked')) ){

                    element.val('Telephone');

                }

                if (element.attr('id') == 'message' ){

                    element.val('Message');

                }


            }
        })

        if ($('#telephone-number')){

            $('#type-phone').click(function(){

                    $('#telephone-number').attr('value','Phone');

            });

              $('#type-email').click(function(){

                      $('#telephone-number').attr('value','Email');

            });


        }
        function fadeTo(opacityVal) {

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

        }

        $('#play-video').click(function(){

            video = document.getElementById("html-video");
            if (video){
                video.play();
                 fadeTo(1);

            } else {
              movieName = "work-player-flash";
              if (navigator.appName.indexOf("Microsoft") != -1) {
                    video = window[movieName];
              }
              else {
              if(document[movieName].length != undefined){
                    video = document[movieName][1];
              }
                video =  document[movieName];
              }


                video.playMovie();
            }
                $('#play-video').toggle();

                $('#ipad-play-icon').toggle();
                $('#pause-video').toggle();

        });
           $('#ipad-play-icon').click(function(){

            video = document.getElementById("html-video");
            if (video){
                fadeTo(1);
                video.play();

            } else {
              movieName = "work-player-flash";
              if (navigator.appName.indexOf("Microsoft") != -1) {
                    video = window[movieName];
              }
              else {
              if(document[movieName].length != undefined){
                    video = document[movieName][1];
              }
                video =  document[movieName];
              }


                video.playMovie();
            }
                $('#play-video').toggle();
                $('#ipad-play-icon').toggle();
                $('#pause-video').toggle();

        });

        $('#pause-video').click(function(){
          video = document.getElementById("html-video");

          if (video){
                fadeTo(0.5);
               video.pause();

            } else {
              movieName = "work-player-flash";
              if (navigator.appName.indexOf("Microsoft") != -1) {
                    video = window[movieName];
              }
              else {
              if(document[movieName].length != undefined){
                    video = document[movieName][1];
              }
                video =  document[movieName];
              }
               // video = document.getElementById("work-player-flash");



                video.pauseMovie();
            }


             $('#play-video').toggle();
             $('#ipad-play-icon').toggle();
             $('#pause-video').toggle();

        });

        $('#html-video').bind('ended',function(){

            playCarousel();

            changePageFlash();

        })




		$('#enq-form').each(initEnquiryForm);

		$('#home-introduction .client-list').each(initClientList);

		$('#newsletter-signup form').bind('submit', sendSignup);

		$('input.default,textarea.default').bind('focus', removeDefault);
		
		/*$('#our-work').each(function(){

			swfobject.embedSWF('/~fiabdev/flash/departureboard.swf', "our-work-flash", "291", "244", "9.0.0", null, null, null);

		});*/

		   /*	$('#showcase-reel').each(function(){

					swfobject.embedSWF('../flash/testBanner.swf', "showcase-reel-flash", "973", "313", "10.0.0", null, null, null);

		    }); */

            $('#work-player').each(function(){

           

                var height = $('#vid-height').val();

                var width = $('#vid-width').val();

                var flashvars = { video : $('#vid-file').val(),
                                   height : $('#vid-height').val(),
                                   width : $('#vid-width').val()
                }

                var params = { allowscriptaccess  : 'always'}

              

				swfobject.embedSWF(base_uri + '/flash/vidplayer.swf', "work-player-flash", '664', height , "10.0.0", null, flashvars, params);

            });








	} // end init

	$(document).ready(init);
					
})();
