
/** 
** Description: Base JavaScript
** Company:     Netcel - http://www.netcel.com
** Client:      Temenos
** Author:      Dominic Laurence
**
-----------------------------------------------------------------------**/


/** Functions
-----------------------------------------------------------------------**/
var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	//function to handle changing the clocks time
	
	$j('#clockNav').changeTime();
	
	//handle the focus of text input fields
	
	$j('input[type="text"]').hint();
	
	//handle the tabs section on the homepage
	
	$j('#tabs').length ? $j('#tabs').homeTabs() : null;	
	
	//homepage slider config
	
	$j('#slider').length ? $j('#slider').sliderConfig() : null;
	
	//function to match the siblings height with the div largest value
	
	$j('.matchHeights').length ? $j('.matchHeights').matchHeights() : null;	
	
	//HoverIntent plugin config (used on the main navigation)
	
	$j('#navBar').length ? $j('#navBar .nav').navHover() : null;
	
	//footer slider function
	
	$j('#footer-display').footerSlide();
	
	//language slider function

	$j('#navBar li.menu-root').langSlide();

	//location sliders

	$j('#contact-index').showHideLoc();

	loginOverlay.init('.js-login a'); 			    //Login overlay


});


/* ***************************************************************************************
Login overlay 
*/
var loginOverlay = {
    init: function () {

        //login modal light box
        $j(".js-login").find("a.hLogin").click(function (e) {
            e.preventDefault();
            var el = $j('<div id="loginFrame"/>').appendTo('body');
            var pageContainer = $j('#page');
            var pos = pageContainer.offset();
            var left = pos.left;
            var top = pos.top;
            var w = el.width();
            el.css({
                left: left + 300 + 'px',
                top: top + 120 + 'px'
            });

            //get URL of the dialog win
            var hLoginUrl = $j('.js-login input.login_url').attr('value');

            //call ajax content for the search	
            $j.ajax({
                url: hLoginUrl,
                cache: false,
                success: function (html) {
                    el.contents().remove();
                    el.append(html);
                    //el.find('input')[0].focus(); //focus first input in login form
                }
            });
            el.fadeIn();
            //return false;
        });

        //loading icon for when fetching ajax
        $j(".loginLoading").ajaxStart(function () {
            $j(this).show();
        });
        $j().ajaxComplete(function (event, request, settings) {
            $j(".loginLoading").hide();
        });

        //esc key removes dialog
        $j(document).keydown(function (e) {
            if (e.which == 27) {
                loginOverlay.closeButton();
            }
        });
    }, closeButton
	: function () {
	    $j("div#loginFrame").fadeOut();
	}
}



$j.fn.showHideLoc = function() {

    //hide all of the location divs
    $j('.location').hide();

    $j(this).find('.location').siblings().click(function() {
        //remove the href
        $j(this).css('cursor','pointer').removeAttr('href');

        //hide show the info
        $j(this).siblings().slideToggle();
    });

}

//main navigation functionality

$j.fn.navHover = function() {
	
	var firstRollOver = true;/*this is to know if the user has rolled over a navbar for the first time (to ensure the initial response is fast)*/
	var speedDown = 1000;
	var speedUp = 200;	
	
	function slideDown() {	
		$j(this).delay(firstRollOver ? 0 : speedUp).animate({ borderTopColor: '#b1b1b1', borderLeftColor: '#b1b1b1', borderRightColor: '#b1b1b1', borderBottomColor: 'transparent' }, speedUp,function() {
			$j(this).find("a:first-child").css('background-color','#fff');
			//dont slide down the "home" button
			$j(this).attr('class') == "home" ? null : $j(this).find(".subMenu").slideDown(speedDown,'easeOutExpo');

			
		});
		
		firstRollOver = false;
			
	}
	function slideUp() {
		$j(this).find(".subMenu").slideUp(speedUp,function() {
			$j(this).siblings("a").css('background-color','transparent');
			$j(this).parents('li').animate({ borderTopColor: 'transparent', borderLeftColor: 'transparent', borderRightColor: 'transparent', borderBottomColor: 'transparent' }, 100);
		});
			
	}
	
    $j("#navBar .nav li ul").css( 'display', 'none' ).css( 'left', '-1px' );
    
    var config = {
            sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
            interval: 90, // number = milliseconds for onMouseOver polling interval    
            over: slideDown, // function = onMouseOver callback (REQUIRED)    
            timeout: 400, // number = milliseconds delay before onMouseOut    
            out: slideUp // function = onMouseOut callback (REQUIRED)    
        };

        $j("#navBar .nav li").hoverIntent(config);
       
}

//handle language slider functionality

$j.fn.langSlide = function () {
	
	$j(this).find('a.language').click( function() {
		$j(this).siblings().slideDown(1000,'easeOutExpo');
	});
	
	$j(this).find('ul').mouseleave( function() {
		$j(this).slideUp(1000,'easeOutExpo');
	});
	
}

//handle footer slider functionality

$j.fn.footerSlide = function () {
	
	$j(this).addClass('closed');
	$j('#footer #expandBtn').addClass('closed').click( function() {
		$j(this).toggleClass('closed');
		$j('#footer-display').slideToggle(1000,'easeOutExpo');
	});
	
}

//initialisation of the homepage slider

$j.fn.sliderConfig = function() {
	
	$j('#prevNext .prev,#prevNext .next').css('display','block');

    var $slides = $j('#slides');
    var slideWidth = $j('#slides>div').outerWidth('true');
    var noOfSlides = $j('#slides>div').length;
    var sliderWidth = noOfSlides*slideWidth;
    var easing = 'easeOutExpo';
    var nextSpeed = 500;
    var endToEndSpeed = 1200;
    
    //default styles (will not use these if JS is disabled and will degrade nicely
    
    $j('#mask').css({height : '130px',overflow : 'hidden', position : 'relative', width : 260});
    $slides.css('width',sliderWidth).css ('position','absolute');
    $j('#slides>div').css('float','left').css('width',slideWidth).css('height','130px');
    
    //handle click events
    
    $j('#prevNext .next').click(function(){
	    
	    //prevent the user from clicking whilst the slider is still animating
	    
	    if(!$slides.is(':animated')) {
		    
		    var pos = $slides.position();
		    
		    //alert('left pos = '+pos.left+', slider width = -'+(sliderWidth-slideWidth));
		    
		    //animate the slider (...and animate back to the start if it reaches the end)
	    
		    pos.left==-(sliderWidth-slideWidth) ? $slides.animate({left: '0px'}, endToEndSpeed, easing) : $slides.animate({left: '-='+slideWidth+'px'}, nextSpeed, easing);
		    
		}
    });
    
    $j('#prevNext .prev').click(function(){
	    
	    //prevent the user from clicking whilst the slider is still animating
	    
	    if(!$slides.is(':animated')) {
		    
		    var pos = $slides.position();
		    
		    pos.left==0 ?  $slides.animate({left: '-'+(sliderWidth-slideWidth)+'px'}, endToEndSpeed, easing) : $slides.animate({left: '+='+slideWidth+'px'}, nextSpeed, easing)
		    
	    }
       
    });  
        
}

//handle the tabs section (show and hide) on the homepage

$j.fn.homeTabs = function() {
		
	//reused selectors
	
	$tabs = $j(this);
	$btn1 = $j(this).find('ul li:eq(0) a');
	$btn2 = $j(this).find('ul li:eq(1) a');
	$btn3 = $j(this).find('ul li:eq(2) a');
	
	//find out which datagroup is the tallest (so we can set the height of the tabs area)
    
    var tallest = new Array();
    tallest[0] = $j('.data0').outerHeight();
    tallest[1] = $j('.data1').outerHeight();
    tallest[2] = $j('.data2').outerHeight();
    
    var largest = Math.max.apply(Math, tallest);
	
	//set the default (call the function and pass the value 0 - tab0)
	
	showInfo(0);
	var prev;
	
	//set the height of the tabs container to accommodate the highest data group
	
	$j($tabs).css('height',''+largest+'px');
	
	//hide all the data groups except the first
	
	$j('.data').hide();$j('.data').eq(0).show();
	
	//assign click events
	
	$j($btn1).click(function() {showInfo(0);});
	$j($btn2).click(function() {showInfo(1);});
	$j($btn3).click(function() {showInfo(2);});
    
	//main function
	
	function showInfo(x) {
		//deselect the old tab and hide the data
		
		$j($tabs).find('ul li:eq('+prev+') a').removeClass('sel');
		$j($tabs).find('.data'+prev+'').hide();
		
		//select the new tab and show the data
		
		$j($tabs).find('ul li:eq('+x+') a').addClass('sel');
		$j($tabs).find('.data'+x+'').show();
		
		//set the background of the tabs to show as selected
		
		if(x==0) {
			$j($tabs).css('background-position', 'left top');
		}else if(x==1) {
			$j($tabs).css('background-position', 'center top');
		} else {
			$j($tabs).css('background-position', 'right top')
		}
		 
		//store the name of the selected tab
		
		prev = x;
    }
		
}

//function to set the height of the child div's to match the one with the largest height value

$j.fn.matchHeights = function() {
		
		var children = $j(this).children('div').length;
        var heights = new Array(children);
        
        for (i = 0; i < children; i++) {
	        
            heights[i] = parseInt($j(this).children('div').eq(i).height());
            
        }
        
        var largest = Math.max.apply(Math, heights);
        
        $j(this).children('div').css('height', largest);
    
}

$j.fn.changeTime = function() {
	
	var $timename = $j('#clockNav span');
	var count = 0;
	var timeZones = [{location: "London", zone: 1},{location: "Tokyo", zone: 9},{location: "Beijing", zone: 8},
	{location: "Berlin", zone: 2},{location: "Paris", zone: 2},{location: "Rome", zone: 2},{location: "Moscow", zone: 4}]

	$j('#clockNav a.fwd').click(function() {
		if(count>timeZones.length-2) {
			count=0;
			mytimezone=timeZones[count].zone;
			$timename.html(timeZones[count].location);
		} else {
			count++;
			mytimezone=timeZones[count].zone;
			$timename.html(timeZones[count].location);
			$timename.html(timeZones[count].location);
		}
	});
	
	$j('#clockNav a.back').click(function() {
		count--;
		if(count==-1) {
			count=timeZones.length-1;
			mytimezone=timeZones[count].zone;
			$timename.html(timeZones[count].location);
		} else {
			mytimezone=timeZones[count].zone;
			$timename.html(timeZones[count].location);
			
		}
	});
}

// Form field hints

$j.fn.hint = function () {
	
    return this.each(function () {

        var $input = $j(this),

		  value = $input.attr('value'),

		  $form = $j(this.form),
		  $win = $j(window);

        function remove() {

            if ($input.val() === value) {
                $input.val('');
            }

        }

        if (value) {

            $input.blur(function () {

                if (this.value === '') {
                    $input.val(value);
                }

            }).focus(remove).blur();

            $form.submit(remove);
            $win .unload(remove);

        }

    });

};
