// some required vars
var news_slide_show_index = 0;
var news_int_slide_show_index = 0;
var news_slide_show_timeout = 5000;

function clearValue(clearInput, defaultValue)
{
    if (clearInput.value == defaultValue) clearInput.value = '';
}

$(document).ready(function(){

	/* calendar - events title tooltip */
	$('.tooltip').tooltip({
   showURL: false
})

});

	/* Only showing those sidebar block elements that contain content */
	/*
	$('.block').each(function(i){
		
		$(this).children('.inner').each(function(i){
			var inner_html         = $(this).html();
			var inner_html_trimmed = inner_html.replace(/^\s+|\s+$/g, '');
			if (inner_html_trimmed != '')
			{
				$(this).parent().show();
			}
		});

	});*/
	
	/* attaching rollover to nav elements */
	/*$('.nav_roll').hover(
	    function() {
	        //over
	        var newImage = $(this).find('img').attr('src').replace(/^(.*?)(\.(?:gif|jpg|png))$/, "$1.on$2");
	        if (newImage.indexOf('.on.on') == -1)
	        {
	           $(this).find('img').attr('src', newImage);
	        }
	    },
	    function() {
	        //out
	        var newImage = $(this).find('img').attr('src').replace('.on', '');
	        $(this).find('img').attr('src', newImage);
	    }
	);*/
	
	/* attaching rollover to branch map */
   /* if (isDefined('window','branchIds'))
	{
        $('area').click(
            function() {
                // over
                
                // find the branch code
                var branch_code = $(this).attr('href');
                if (branch_code.indexOf('#') == 0)
                {
                   branch_code = branch_code.substr(1,5);
                }
                
                // grab the branch id from the codes array
                if (branchIds[branch_code])
                {
                   // populate div with branch information
                   $('#branch_details').html(branchDetails[branch_code]);
                   
                   // change links to correspond to correct branches
                   $('#branch_link_contact').attr('href',$('#branch_link_tmp_contact').attr('value')+branchIds[branch_code]);
                   $('#branch_link_gmaps').attr('href',$('#branch_link_tmp_gmaps_a').attr('value')+branchPostCodes[branch_code]+$('#branch_link_tmp_gmaps_b').attr('value'));
                   $('#branch_link_gmaps').attr('target','_blank');
                }
            },
            function() {
               // out
            }
        );*/
        
        /* attaching region zoom to branch map click */
      /*  $('area').click(
            function() {
                
                // find the region
                var branch_region = $(this).attr('href');
                if (branch_region.indexOf('region') != -1)
                {
                   branch_region = branch_region.substr(7,20);
                   $(this).attr('href',$('#branch_link_tmp_region').attr('value')+branch_region);
                }
            }
        );*/
    //}
	
	/* starting the electrical news slideshow */
	/*if (isDefined('window','newsItems') || isDefined('window','newsIntItems'))
	{
	   setTimeout ("slideNews()", news_slide_show_timeout);
	}*/

//});

function showHide (showOrHide, elementToChange) {
    if (showOrHide == 'show')
    {
        $('#'+elementToChange).show();
    }
    else
    {
        $('#'+elementToChange).hide();
    }
}

function slideNews () {
    // NOTE : This could obviously be done better
    
    news_slide_show_index = news_slide_show_index + 1;
    news_int_slide_show_index = news_int_slide_show_index + 1;
    
    if (isDefined('window','newsItems'))
    {
        // grab the news item from the var defined above
        // check if a value exists
        if (newsItems[news_slide_show_index])
        {
           // do nothing for the time being
        }
        else
        {
           // index doesn't exist, restart
           news_slide_show_index = 0;
        }
        
        // check it again, to make sure we have an index 0
        if (newsItems[news_slide_show_index])
        {
           // populate with news item
           $('#module_news_external_item').html(newsItems[news_slide_show_index]);
        }
    }
    
    if (isDefined('window','newsIntItems'))
    {
        // internal news
        // grab the news item from the var defined above
        // check if a value exists
        if (newsIntItems[news_int_slide_show_index])
        {
           // do nothing for the time being
        }
        else
        {
           // index doesn't exist, restart
           news_int_slide_show_index = 0;
        }
        
        // check it again, to make sure we have an index 0
        if (newsIntItems[news_int_slide_show_index])
        {
           // populate with news item
           $('#module_news_internal_item').html(newsIntItems[news_int_slide_show_index]);
        }
    }
    
    // repeat
    setTimeout ("slideNews()", news_slide_show_timeout);
}

function isDefined(object, variable)
{
    /*alert(object + ']2');
    var object_eval = eval(object);
    var object_variable = object_eval[variable];
    var object_type = typeof(object_variable);
    if (object_type == 'undefined')
    {
        return false;
    }
    else
    {
        return true;
    }*/
    
    return (typeof(eval(object)[variable]) == 'undefined')? false : true;
}