/* Author: 

*/
$('document').ready(function(){
    // Tabbed feature for landing pages
    $('#tabs').tabisha();

    // Tag li's with class first for browser compatibility
    $('#navsec li:first-child').addClass('first-child');
    $('#navsec>ul>li>ul>li:last-child').addClass('last-child');

    // Placeholder functionality simulation
    $('#SearchForm input').each(function(count,element){
	search_text(element);
    });
    
    accordian();
    rotating_products();
    
    
    // Show login window dropdown
    $('#login a').click(function( event ){
	if( this.expanded != true ){
	    $(this).parent().append( $('#login_form').hide().slideDown() ).end()
		.addClass('active');

	    this.expanded = true;
	}else{
	    $(this).removeClass('active');
	    $('#login_form').slideUp();
	    this.expanded = false;
	}
	event.preventDefault();
	
    });
    
    // Make child columns of .sameheight the same height as each other
    var sameheight = $('.sameheight');
    
    var current_highest = 0;
    sameheight.find('.inner').each( function(){
	var next_height = $(this).height();
	if( current_highest < next_height ){
	    current_highest = next_height;
	}
    } );
    sameheight.find('.inner').height( current_highest );
    
    

    
});


/**
 * Emulate placeholder text support
 */
function search_text(e)
{

    // Check if an object is passed directly (like from an each loop), or by an ID reference
    var input = false;
    if( typeof(e) == 'object' ){
	// whole object passed
	input = e;
    }else{
	// reference to object passed
	input = document.getElementById(e);
    }

    if( input ){

	var search_advice_text = $(input).siblings('label').text();

	if( $(input).val().length == 0 ){
	    $(input).val( search_advice_text ).css({'color':'#d6eadf'});
	}
	$(input).focus(function(){
	    if( $(this).val().length == 0 || $(this).val() == search_advice_text ){
		$(this).val( '' ).css({'color':'#fff'});
	    }
	}).blur(function(){
	    if( $(this).val().length == 0 ){
		$(this).val( search_advice_text ).css({'color':'#d6eadf'});
	    }
	});
    }

}


/**
 * Homepage accordian function 
 */
var accordian = function()
{
    if( !document.getElementById('accordian') )
	return false;
	
    var items = [], annimationtime = 200;
    
    
    var mousein = function(event)
    {
	
	function makeActive()
	{
	    $(this).addClass('active').unbind(event).find('p,.cta').slideDown( annimationtime, 'easeOutBounce' );
	    this.expanded = true;
	} 
	
	function unActive()
	{
	    $(this).removeClass('active').hoverIntent(mousein,mouseout);
	    this.expanded = false;
	}
	
	// Check panel is not the initially active panel, we dont want to animate that one, as its already expanded.
	if( this.expanded != true && !$(this).hasClass('active') ){
	    
	    // Animate stuff. Apperance is set also by the active class, so dont set that till the callback is called
	    // Collapse current panel
	    $('.panel.active').animate({
		width: '25%'
		}, annimationtime,'easeOutQuad', unActive
	    ).find('p,.cta').slideUp( annimationtime ).end().find('em').hide(); // this sidedown is triggered before the class is applied by the CB function
	    
	    // Expand new panel
	    $(this).animate({
		width: '50%'
		}, annimationtime,'easeOutQuad', makeActive
	    ).find('em').show(); // this sidedown is triggered before the class is applied by the CB function
	    
	}   
    }
    
    var mouseout = function()
    {
    }
    
    // bind functions to hoverIntent on panels for the first time
    $('.panel').hoverIntent( mousein,mouseout );
    $('.panel:not(.active)').find('p,.cta').hide();
   
    
}



/**
 * Rotating product feature
 */
var rotating_products = function()
{
    if( !document.getElementById('product_feature') )
	return false;
    
    var items = [];
    var currentItem = 0;
    
    /**
     * Definition of a method for an element to make itself active.
     * Its applied to the dom elements in the each loop below
     */
    function makeActive()
    {
	$(this).find('p').show().end()
	    .children('.png').fadeIn().end()
	    .children('.item').addClass('activeitem');
	    
	this.active = true;
	currentItem = this.number; // set current item to this item
	try{
	    $(this).siblings().each(function(i,element){
		element.unActivate();
	    });
	}catch(err){}
    }
    
    /**
     * Function to deactivate an item. the element  object is passed to the function.
     */
    function unActivate()
    {
	if( this.active ){
	    $(this).find('p').hide().end()
		.children('.png').fadeOut().end()
		.children('.item').removeClass('activeitem');
	}
	this.active = false;
    }
    
    
    var mouseout = function()
    {
	
    }
    
    $('#product_feature li').each( function(i,element){
	// add method to element to make itself active
	this.makeActive = makeActive;
	this.unActivate = unActivate;
	
	// remove css styling so elements are just hidden
	$(this).children('p,.png').css({
	    'display'	: 'block',
	    'opacity'	: 1
	}).hide();
	
	if( 0 == i ){
	    this.makeActive();
	}
	
	// Bind functions to hoverIntent event
	$(this).hoverIntent(this.makeActive,mouseout);
	
	this.number = i;
	
	// store object
	items.push(this);
	
	
    } );
    
    
    
    // loop through items
    
    window.setInterval(function(){
	try{
	    items[currentItem].makeActive = makeActive;

	    items[currentItem].makeActive();
	    if( currentItem > 1 ){
		currentItem = 0;
	    }else{
		currentItem ++;
	    }
	}catch(err){};

    },3000);
    
    
    
}






















