//
// ============================================== //

		// SPLIT LONG LIST //

// ============================================== //
//
initSplitUl = function(){
	var $bigList = $('ul#nav_categories'), group = $bigList.find('li:lt(12)').remove();
	
  	while(group.length){
    	$('<ul/>').append(group).appendTo('#sub_navigation');
    	group = $bigList.find('li:lt(12)').remove();
  	}
	
	$('ul#nav_categories').remove();
	var thisHtml = '<p id="sub_nav_pag"><a href="#">Next ><\/a><\/p>';
	$('#sub_navigation').append(thisHtml).find('ul').eq(1).hide().animate({opacity: 0},0.001);
	$('#sub_nav_pag a').click(function(){
		$(this).unbind('click');
		ulNext();
		return false;
	});
	
	$('#sub_navigation').css({'display' : 'block'});
};

ulNext = function(){
	$('#sub_navigation').find('ul').eq(0).animate({opacity: 0},400, function(){
		$(this).hide();
		$('#sub_navigation').find('ul').eq(1).show().animate({opacity: 1},400);
		//
		$('#sub_nav_pag a').html('< Previous').click(function() {
			$(this).unbind('click');
			ulPrev();
			return false;
		});
	});
};

ulPrev = function(){
	$('#sub_navigation').find('ul').eq(1).animate({opacity: 0},400, function(){
		$(this).hide();
		$('#sub_navigation').find('ul').eq(0).show().animate({opacity: 1},400);
		//
		$('#sub_nav_pag a').html('Next >').click(function() {
			$(this).unbind('click');
			ulNext();
			return false;
		});
	});
};


//
///////////////// INITIALISE
//

$(document).ready(function(){
	
	if ($('#photo').length) {  
		if($('#photo_nav').length){
			initSlideNav();
		}else{
			if ($('#people_viewer').length) {  
				initPeople();
			} else if ($('#photo ul li').length>1) { 
				initSlideShow();
			}
			if ($('#photo ul li').length==1) { 
				$('#photo ul').show();
			}
			
		}
	}
	//
	if ($('#message ul li').length>1) { 
		initNewsTicker();
	}
	//
	if($('#nav_categories li').length>13){
		initSplitUl();
	}else{
		$('#sub_navigation').css({'display' : 'block'});
	}
	//
	if($('#clientlist li').length>22){
		initSplitClient();
	}
	//
	$('a img.english-text').click(function(e){
		e.preventDefault();
		$('.english-text').hide();
		$('.arabic-text').show();
	});
	
	$('p.arabic-text a').click(function(e){
		e.preventDefault();
		$('.english-text').show();
		$('.arabic-text').hide();
	});
	
	$('.faq-answer').hide();
	
	$('.faq-question a').click(function(e){
		e.preventDefault();
		//
		if($(this).parent().hasClass('faq-open')){
			$('.faq-open').removeClass('faq-open').siblings('.faq-answer').hide();
		}else{
			$('.faq-open').removeClass('faq-open').siblings('.faq-answer').hide();
			$(this).parent().addClass('faq-open').siblings('.faq-answer').show();
		};
	});
});

//
///////////////// scroller
//

(function($) {

	$.fn.Scroller = function() {	
		var args = arguments[0] || {};    
		direction = args.direction; //horizontal or vertical 
		speed = args.speed; //scrolling speed, 1-10, 1: fastest
		
		
		if(!direction){
			direction = 'vertical';
		}
		if(!speed){
			speed = 5;
		}
		
		if(direction == 'vertical'){
			var up_arrow = 'http://www.futuremediasystems.co.uk/images/furniture/arrow-up.png';
			var up_arrow_hold = 'http://www.futuremediasystems.co.uk/images/furniture/arrow-up-hold.png';
			var down_arrow = 'http://www.futuremediasystems.co.uk/images/furniture/arrow-down.png';
			var down_arrow_hold = 'http://www.futuremediasystems.co.uk/images/furniture/arrow-down-hold.png';
			var scroller = 'http://www.futuremediasystems.co.uk/images/furniture/scroller-v-button.png';
			var scroller_hold = 'http://www.futuremediasystems.co.uk/images/furniture/scroller-v-button-hold.png';				
		}
		else if(direction == 'horizontal'){			
			var left_arrow = 'images/arrow-left.png';
			var left_arrow_hold = 'images/arrow-left-hold.png';
			var right_arrow = 'images/arrow-right.png';
			var right_arrow_hold = 'images/arrow-right-hold.png';
			var scroller = 'images/scroller-h-button.png';
			var scroller_hold = 'images/scroller-h-button-hold.png';			
		}
		
		speed = speed * 20;
		speed_val = speed;
		
		var this_idname = $(this).attr('id');
				
		$(this).css('overflow','hidden');
		
		var boxwidth = $(this).width();
		var boxheight = $(this).height();	

		//get content
		var boxcontent = $(this).html();	
		$(this).html('');
		

		//vertical scroller
		if(direction == 'vertical'){								
			
			
			//put content into an inside div			
			$(this).append('<div class="scroller-content">'+boxcontent+'</div><div class="scroller-control">'+
			'<div class="scroller-up"><a href="#"><img src="'+up_arrow+'" /></a></div><div class="scroller-field"></div><div class="scroller-down"><a href="#"><img src="'+down_arrow+'" /></a></div>'+
			'</div>');
			var nuwidth = boxwidth - 40;
			$('.scroller-content',this).css('width',nuwidth+'px');
			
					
			$('.scroller-control',this).click(function(event){
			event.preventDefault();
			})
					
			//set scroller field
			arrowheights = $('.scroller-up',this).height();		
			arrowheights = arrowheights + $('.scroller-down',this).height() + 20;
			nuboxheight = boxheight - arrowheights;
			$('.scroller-field',this).css('height',nuboxheight);
			
			
			
			var content_h = $('.scroller-content',this).height();
			
			
			//check if content is long enough to use scroller
			if(content_h > boxheight){			
				
				//append scroll button
				$('.scroller-field',this).append('<div class="scroller"><img src="'+scroller+'" /></div>');
				
				
				var scroller_field_h = $('.scroller-field',this).height();
				
				//define max position of scroller			
				var max = scroller_field_h - $('.scroller',this).height();			
				
				//1px move w scroller = ? px move in content  --> content height / scroller available moving field		
				var stepping = ((content_h-boxheight) / max);		
				
				
		

				
				
				var timeout = '0';
				
				//scroll up
				var scrollUp = function(this_id,hide_hold){					
					var currtop = $('#'+this_id+' .scroller-content').css('top');		
											
					currtop = parseInt(currtop);
					
						if(currtop <= 0){
							if(hide_hold != true){
								$('#'+this_id+' .scroller-up a img').attr('src',up_arrow_hold);		
							}								
							
							currtop = currtop + 10;
							if(currtop > 0){currtop = 0;}
							$('#'+this_id+' .scroller-content').css('top',currtop+'px');
						
							//set scrollbar position also										
							var scurry = Math.round(currtop / stepping) * -1;
							$('#'+this_id+' .scroller').css('top',scurry+'px');						
						}
				};
				
				var speedval = speed;
				$('.scroller-up',this).mousedown(function(){				
					timeout = setInterval(function(){scrollUp(this_idname);},speedval);
					return false;
				});
				$('.scroller-up',this).mouseout(function(){			
					if(timeout){
						clearInterval(timeout);				
						$('a img',this).attr('src',up_arrow);				
					}
					return false;
				});
				$('.scroller-up',this).mouseup(function(){			
					if(timeout){
						clearInterval(timeout);
						$('a img',this).attr('src',up_arrow);
					}
					return false;
				});

				
				//scroll down to bottom		
				var scrollDown = function(this_id,hide_hold){ 
						var inboxheight = $('#'+this_id+' .scroller-content').height() - boxheight;			
						var currtop = $('#'+this_id+' .scroller-content').css('top');
						currtop = Number(currtop.replace('px',''));
						if(inboxheight >= (currtop * -1))
						{			
							if(hide_hold != true){
								$('#'+this_id+' .scroller-down a img').attr('src',down_arrow_hold);
							}
							
							currtop = currtop - 10;
							$('#'+this_id+' .scroller-content').css('top',currtop+'px');
							
							//set scrollbar position also						
							var scurry = Math.round(currtop / stepping) * -1;
							$('#'+this_id+' .scroller').css('top',scurry+'px');						
						}	
				};
				
				
				$('.scroller-down',this).mousedown(function(){

					timeout = setInterval(function(){scrollDown(this_idname);}, speedval);
					return false;
				});
				$('.scroller-down',this).mouseout(function(){
					if(timeout){
						clearInterval(timeout);
						$('.scroller-down a img').attr('src',down_arrow);
					}
					return false;
				});
				$('.scroller-down',this).mouseup(function(){
					if(timeout){
						clearInterval(timeout);
						$('.scroller-down a img').attr('src',down_arrow);
					}
					return false;
				});
				
				
				
				
				
				//make scrollbutton draggable
				var dragIt = function(element,this_id){			
					var dragok = false;
					var y,el,ely,nuy;			
				
					function move(e){
						if (!e) e = window.event;
						if (dragok){
							nuy = ely + e.clientY - y;							
							if(nuy >= 0 && nuy <= max){
								numy = nuy;
								nuy = nuy + "px";	
								el.css('top',nuy);			

								//position content also with the stepping value
								$('#'+this_id+' .scroller-content').css('top',Math.round(stepping * numy * -1)+'px');
								
								
							}
							return false;
						}
					}

					function down(e){
						if (!e) e = window.event;			
						dragok = true;			 
						el = element;			 
						ely = parseInt(element.css('top')+0);			 
						y = e.clientY;
						$(document).mousemove(move);		
						$('img',el).attr('src',scroller_hold);
						return false;
					}
					

					function up(){
						dragok = false;				
						element.mousemove(null);
						$('img',element).attr('src',scroller);
					}

					element.mousedown(down);
					$(document).mouseup(up);						
					
				}		
				dragIt( $('.scroller',this), this_idname );
				
				
				//add mouse wheel scrolling
				moveObject = function(event){		
					if (!event) event = window.event;  

					if (event.wheelDelta || event.detail){
						var value = event.wheelDelta;
						if(!value){value = event.detail;}
						if(value < 0){
							if($.browser.mozilla){
							scrollUp(this_idname,true);
							}else{
							scrollDown(this_idname,true);							
							}
						}
						else{
							if($.browser.mozilla){							
							scrollDown(this_idname,true);
							}else{
							scrollUp(this_idname,true);
							}
						}
					}
				}		
				
				var element = document.getElementById(this_idname);
				if(window.addEventListener){
					element.addEventListener('DOMMouseScroll', moveObject, false);
				}
				element.onmousewheel = moveObject;
				
						
				
				
				
				
				
			}else{
				//set opacity of scroller
				$('.scroller-control',this).css({ opacity: 0 });
			}
					
			
		}
		else if(direction == 'horizontal'){
			
			
			//put content into an inside div			
			$(this).append('<div class="h-scroller-content">'+boxcontent+'</div><div class="h-scroller-control">'+
			'<div class="h-scroller-left"><a href="#"><img src="'+left_arrow+'" /></a></div><div class="h-scroller-field"></div><div class="h-scroller-right"><a href="#"><img src="'+right_arrow+'" /></a></div>'+
			'</div>');		
			var nuheight = boxheight - 40;
			$('.h-scroller-content',this).css('height',nuheight+'px');
			
			$('.h-scroller-control',this).click(function(event){
			event.preventDefault();
			})
			
			
			//set scroller field
			arrowwidths = $('.h-scroller-left',this).width();		
			arrowwidths = arrowwidths + $('.h-scroller-right',this).width() + 20;
			nuboxwidth = boxwidth - arrowwidths;
			$('.h-scroller-field',this).css('width',nuboxwidth);
			
			
			var content_w = $('.h-scroller-content',this).children().width();
			
			
			//check if content is long enough to use scroller
			if(content_w > boxwidth){			
				
				//append scroll button
				$('.h-scroller-field',this).append('<div class="h-scroller"><img src="'+scroller+'" /></div>');
				
				
				var scroller_field_w = $('.h-scroller-field',this).width();
				
				//define max position of scroller			
				var max = scroller_field_w - $('.h-scroller',this).width();			
				
				//1px move w scroller = ? px move in content  --> content height / scroller available moving field		
				var stepping = ((content_w-boxwidth) / max);		
					
														
				//scroll right
				var scrollRight = function(this_id,hide_hold){ 					
						var inboxwidth = $('#'+this_id+' .h-scroller-content').children().width() - boxwidth;								
						var currleft = $('#'+this_id+' .h-scroller-content').css('left');
						currleft = Number(currleft.replace('px',''));
						if(inboxwidth >= (currleft * -1))
						{			
							if(hide_hold != true){
								$('#'+this_id+' .h-scroller-right a img').attr('src',right_arrow_hold);
							}
							
							currleft = currleft - 10;
							$('#'+this_id+' .h-scroller-content').css('left',currleft+'px');
							
							//set scrollbar position also						
							var scurry = Math.round(currleft / stepping) * -1;
							$('#'+this_id+' .h-scroller').css('left',scurry+'px');						
						}	
				};
				
				
				$('.h-scroller-right',this).mousedown(function(){

					timeout = setInterval(function(){scrollRight(this_idname);}, speedval);
					return false;
				});
				$('.h-scroller-right',this).mouseout(function(){
					if(timeout){
						clearInterval(timeout);
						$('.h-scroller-right a img').attr('src',right_arrow);
					}
					return false;
				});
				$('.h-scroller-right',this).mouseup(function(){
					if(timeout){
						clearInterval(timeout);
						$('.h-scroller-right a img').attr('src',right_arrow);
					}
					return false;
				});
					
					
														
				//scroll left
				var scrollLeft = function(this_id,hide_hold){ 					
						var inboxwidth = $('#'+this_id+' .h-scroller-content').children().width() - boxwidth;								
						var currleft = $('#'+this_id+' .h-scroller-content').css('left');
						currleft = Number(currleft.replace('px',''));
						if(currleft <= 0)						
						{			
							if(hide_hold != true){
								$('#'+this_id+' .h-scroller-left a img').attr('src',left_arrow_hold);
							}
							
							currleft = currleft + 10;
							if(currleft > 0){currleft = 0;}
							$('#'+this_id+' .h-scroller-content').css('left',currleft+'px');
							
							//set scrollbar position also						
							var scurry = Math.round(currleft / stepping) * -1;
							$('#'+this_id+' .h-scroller').css('left',scurry+'px');						
						}	
												
				};
				
				
				$('.h-scroller-left',this).mousedown(function(){

					timeout = setInterval(function(){scrollLeft(this_idname);}, speedval);
					return false;					
				});
				$('.h-scroller-left',this).mouseout(function(){
					if(timeout){
						clearInterval(timeout);
						$('.h-scroller-left a img').attr('src',left_arrow);
					}
					return false;
				});
				$('.h-scroller-left',this).mouseup(function(){
					if(timeout){
						clearInterval(timeout);
						$('.h-scroller-left a img').attr('src',left_arrow);
					}
					return false;
				});					
					
					
					
				//make scrollbutton draggable
				var dragIt = function(element,this_id){			
					var dragok = false;
					var x,el,elx,nux;			
				
					function move(e){
						if (!e) e = window.event;
						if (dragok){
							nux = elx + e.clientX - x;							
							if(nux >= 0 && nux <= max){
								numx = nux;
								nux = nux + "px";	
								el.css('left',nux);			

								//position content also with the stepping value
								$('#'+this_id+' .h-scroller-content').css('left',Math.round(stepping * numx * -1)+'px');
								
								
							}
							return false;
						}
					}

					function down(e){
						if (!e) e = window.event;			
						dragok = true;			 
						el = element;			 
						elx = parseInt(element.css('left')+0);			 
						x = e.clientX;
						$(document).mousemove(move);		
						$('img',el).attr('src',scroller_hold);
						return false;
					}
					

					function up(){
						dragok = false;				
						element.mousemove(null);
						$('img',element).attr('src',scroller);
					}

					element.mousedown(down);
					$(document).mouseup(up);						
					
				}		
				dragIt( $('.h-scroller',this), this_idname );	
				
				
				
				//add mouse wheel scrolling
				moveObject = function(event){		
					if (!event) event = window.event;  

					if (event.wheelDelta || event.detail){
						var value = event.wheelDelta;
						if(!value){value = event.detail;}
						if(value < 0){
							if($.browser.mozilla){
							scrollLeft(this_idname,true);
							}else{
							scrollRight(this_idname,true);							
							}
						}
						else{
							if($.browser.mozilla){
							scrollRight(this_idname,true);
							}else{
							scrollLeft(this_idname,true);							
							}
						}
					}
				}		
				
				var element = document.getElementById(this_idname);
				if(window.addEventListener){
					element.addEventListener('DOMMouseScroll', moveObject, false);
				}
				element.onmousewheel = moveObject;
				
				
					
			}
			else{
				//set opacity of scroller
				$('.h-scroller-control',this).css({ opacity: 0 });			
			}
			
		}
	
	
	}

})(jQuery);

//
///////////////// slideshow on index
//

$(document).ready(function() {      
     
    //Execute the slideShow, set 4 seconds for each images
    slideShow(4000);
 
});
 
function slideShow(speed) {
 
 
    //append a LI item to the UL list for displaying caption
    $('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');
 
    //Set the opacity of all images to 0
    $('ul.slideshow li').css({opacity: 0.0});
     
    //Get the first image and display it (set it to full opacity)
    $('ul.slideshow li:first').css({opacity: 1.0}).addClass('show');
     
    //Get the caption of the first image from REL attribute and display it
    $('#slideshow-caption h3').html($('ul.slideshow li.show').find('img').attr('title'));
    $('#slideshow-caption p').html($('ul.slideshow li.show').find('img').attr('alt'));
         
    //Display the caption
    $('#slideshow-caption').css({opacity: 0.7, bottom:0});
     
    //Call the gallery function to run the slideshow    
    var timer = setInterval('gallery()',speed);
     
    //pause the slideshow on mouse over
    $('ul.slideshow').hover(
        function () {
            clearInterval(timer);   
        },  
        function () {
            timer = setInterval('gallery()',speed);         
        }
    );
     
}
 
function gallery() {
 
 
    //if no IMGs have the show class, grab the first image
    var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
 
    //trying to avoid speed issue
    if(current.queue('fx').length == 0) {
 
        //Get next image, if it reached the end of the slideshow, rotate it back to the first image
        var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
             
        //Get next image caption
        var title = next.find('img').attr('title'); 
        var desc = next.find('img').attr('alt');    
             
        //Set the fade in effect for the next image, show class has higher z-index
        next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
         
        //Hide the caption first, and then set and display the caption
        $('#slideshow-caption').slideToggle(300, function () { 
            $('#slideshow-caption h3').html(title); 
            $('#slideshow-caption p').html(desc); 
            $('#slideshow-caption').slideToggle(500); 
        }); 
     
        //Hide the current image
        current.animate({opacity: 0.0}, 1000).removeClass('show');
 
    }
 
}

