﻿var $j = jQuery.noConflict();
 $j(document).ready(function() {

if ($j("#frontpageNews a").length) {}
   else{$j("#frontpageNews").hide()}

if ($j("#frontpageEvents .event").length) {}
   else{$j(".frontpageBanners .banner.last").hide()}
    
    factNav();
    contactNav();
    setEqualHeight($j(".story"));
    

 $j("#call_me_container").hide();
   
    var contactshown=false;

$j("#AskUs").mousedown(function(){
			if (!contactshown)
				{
				$j("#call_me_container").show('blind','',500);
				$j("#AskUs").addClass('expanded',0);
				contactshown=true;
				}
			else
				{
				$j("#call_me_container").hide('blind','',500);
				$j("#AskUs").removeClass('expanded',0);
				contactshown=false;
				}
			return false;
		});


 showlocation();
    showCluster();

$j(".story").each(function(){
   var readmore ="";
   readmore = $j(this).find(".contactDetailsLink").attr("href");
   if(readmore!="" && readmore!=undefined)
    {
       $j(this).find("img").wrap("<a href='"+readmore+"'/>");
    }
});

   
    
});



function SubmitContactForm()
{


var ExtendedFormName = document.getElementById("ExtendedFormName" );
var ExtendedFormEmail= document.getElementById("ExtendedFormEmail" );
var ExtendedFormPhone= document.getElementById("ExtendedFormPhone" );
var ExtendedFormCompany = document.getElementById("ExtendedFormCompany" );
var ExtendedFormMessage= document.getElementById("ExtendedFormMessage" );
var theExtendedFormPage= document.getElementById("ExtendedFormPage" );


var msg = "Required fields:\n\n";
var alertflag = false;

if(ExtendedFormName.value == "")
{
msg = msg + "- Name\n";
alertflag = true;
}
if(ExtendedFormCompany.value == "")
{
msg = msg + "- Company\n";
alertflag = true;
}

if(ExtendedFormEmail.value == "")
{
msg = msg + "- E-mail\n";
alertflag = true;
}

if(ExtendedFormPhone.value == "")
{
msg = msg + "- Phone\n";
alertflag = true;
}

if(ExtendedFormMessage.value == "")
{
msg = msg + "- Message\n";
alertflag = true;
}



if(alertflag)
{alert(msg );}
else
{



    jQuery.post("/components/document/ContactForm.ashx", {page: theExtendedFormPage.value, name: ExtendedFormName.value, message: ExtendedFormMessage.value, email: ExtendedFormEmail.value, phone: ExtendedFormPhone.value, company: ExtendedFormCompany.value}, // Ajax call script, sending the letter as a param
	function(data)
	{
	
            alert("Thank you for your enquiry. One of our experts will contact you soon.");
            $j("#call_me_container").hide('blind','',500);
				            $j("#AskUs").removeClass('expanded',0);
				            contactshown=false;

_gaq.push(['_trackPageview', '/goal/contact-me']);


	});





}

}




function frontpage()
{
    //Front page news ticker
    newsTicker();
    //Front page top entries
    frontEntries();
}

//Attach click events to the front page left 'menu'
function frontEntries()
{
    $j(".slideshow").hide();
/*
    $j(".entryLink").click(function() {
         var show = $j(this).attr("href");
          if(show!=null && show!='')
		 {
		    resetSlideShow(show,this,false);
		 }
		 return false;
    });    
*/
    $j("img.clickable").click(function(){
        var text = $j(this).next("span.text");
        if($j(text).find("a").length)
        {
            var url = $j(text).find("a").attr("href");
            window.location = url;
        }
    });

    pickRandomShow();

}
/* Initial random slideshow */
function pickRandomShow()
{
    // Count the number of items
    var countShows = $j('.slideshow').size();     

    // Find a random number between 0 and the number of items 
    var randomizeIt = Math.floor(Math.random()*(countShows));    

    // Click the chosen item
    //$('#randomiza_wrapper').tabs({ selected: randomizeIt });
    //var link = $j(".entryLink").eq(randomizeIt-1);
        var link = $j(".clusterLink").eq(randomizeIt-1);    
        //var show = $j(link).attr("href");

        var show = $j(link).attr("rel");
        if(show!=null && show!='')
	{
		resetSlideShow(show,link,true);

	}
}

//Front page 'slideshow'
function slideshow(show,rotate,link)
{  
    var slidepace = 5000;
    setTimeout(function() {changeSlides(show,rotate,link);}, slidepace);
}
function changeSlides(show,rotate,link)
{
    var slidepace = 5000;
    var elements = $j(show +" .slide");
    var index = 0;
    
    

    var doNext = null;
    doNext = function() 
    {    
        if($j(show).css("display")=="block")
        {
            var element = elements.eq(index);
            var prev;
            if(index-1>=0)
            {
                prev = elements.eq(index-1);
            }
            else
            {   
                //first
                prev = elements.eq(0);                
            }
        
            $j(prev).find("img").fadeIn();
            $j(element).find("img").fadeOut();
        
            /* if there are more slides, go to next slide */
            if (index < elements.length) 
            {
                index++;
                setTimeout(doNext, slidepace);
            }
            //No more slides within - rotate to next slideshow?
            else if(rotate)
            {
                var nextshow = $j(show).next().attr("id");// Find next show
                var links = $j('.clusterLink'); //All links
                var thisIndex = links.index(link); //This link
                var nextlink = links.eq(thisIndex+1); //Next link
                
                //If there is a next show
                if(nextshow!=null && nextshow!=undefined && nextshow!='')
                {
                    resetSlideShow(nextshow,nextlink,true);

                }
                //else go to first slideshow 
                else
                {
                    var firstshow = $j('.slideshow').first().attr("id");
                    
                    var firstlink = links.eq(0); //Next link
                    resetSlideShow(firstshow,firstlink,true);
                }
            }
            else
            {
                index=0;
                setTimeout(doNext, 0);
            }
            
        }
        else
        {
            resetSlides(show);
        }
    }
    doNext.call();
}

function resetSlideShow(show,link,rotate)
{

    $j(".clusterLink").parent().removeClass("selected");
    $j(".slideshow").hide();
    $j(link).parent().addClass("selected");		            
    $j("#"+show).show();
    slideshow("#"+show,rotate,link); 
    
}

function resetSlides(show)
{
    $j(show +" .slide").each(function(){
        $j(this).find("img").fadeIn();
    });
}




function newsTicker()
{
    $j('#frontpageNews').cycle({      
        timeout: 5000,
        speed:  500
    }); 

var h = setEqualHeight($j('#frontpageEvents').find("div.event"));
 $j('#frontpageEvents').css("height",h+"px");


 $j('#frontpageEvents').cycle({
        timeout: 5000,
        speed: 500
        
    });

   
}

//Attach click events to the facts page left 'menu'
function factNav()
{  
     $j("#factNav").find("a").click(function() {
         var fact =  $j(this).attr("href");
		 if(fact!=undefined && fact!='')
		 {
		     $j("#factNav a").parent().removeClass("selected");
		     $j(this).parent().addClass("selected");		    
		    
		    getFacts(fact);
		 }
		 return false;
    });    
    
     $j("#factNav").find("li").find("a").first().click();
}
function getFacts(url)
{
    $j.get(url, function(data) {
    
        clearFact();
    
        //Title
        var h1 = $j(data).find('h1').html();
        $j("#factContainer h2").html(h1);
        
        
         
        //Country content
        var noCountry = true;
        var countryImg;
        if($j(data).find('#countryImage').length)
        {
            countryImg = $j(data).find('#countryImage img').attr("src"); 
            if(countryImg!=undefined && countryImg!='')
            {
                $j("#factContainer #country").html("<img src='"+countryImg+"'/>");
                noCountry = false;
            }
        }
        var countryText = $j(data).find('#countryText').html(); 
        if(countryText.length > 0)
        {
            $j("#factContainer #country").append(countryText);
            if(countryImg!=undefined && countryImg!='')
            {
                $j("#factContainer #country").append("<a class='chart' href='"+countryImg+"'>Download chart</a>");
            }
            noCountry = false;
        }
        
        
        //City content
        var noCity = true;
        var cityImg;
        if($j(data).find('#cityImage').length)
        {
            cityImg = $j(data).find('#cityImage img').attr("src"); 
            if(cityImg!=undefined && cityImg!='')
            {
                $j("#factContainer #city").html("<img src='"+cityImg+"'/>");
                noCity=false;
            }
        }
        var cityText = $j(data).find('#cityText').html(); 
        if(cityText.length)
        {
            $j("#factContainer #city").append(cityText);
            if(cityImg!=undefined && cityImg!='')
            {
                $j("#factContainer #city").append("<a class='chart' href='"+cityImg+"'>Download chart</a>");
            }
            noCity=false;
        }
        
        //Hide or show switcher
        if(noCountry || noCity)
        {
            $j("#switcher").hide()
        }
        else
        {
            $j("#factContainer #city").hide();
            $j("#switcher #countryLink").addClass("selected");
            switcherNav();
        }       
         
            
      }); 
}
function switcherNav()
{
     $j("#switcher a").click(function() {
         var fact = $j(this).attr("href");
		 if(fact!=null && fact!='')
		 {
		    $j("#factContainer #city").hide();
		    $j("#factContainer #country").hide();
		    $j("#switcher a").removeClass("selected");
		    $j(this).addClass("selected");		    
		    $j(fact).show();
		 }
		 return false;
		 });
}
function clearFact()
{
    $j("#factContainer #city").empty();
    $j("#factContainer #country").empty();
    $j("#factContainer h2").empty();
    $j("#switcher").show()
}
function areaChange(area, solution)
{
    var areafilter = $j(area).val();    
    //var solutionfilter = $j(solution+" option:selected").attr("value");
    
    $j("#stories").find(".story,.storyRight").removeClass("hideArea");
    $j("#stories").find(".story,.storyRight").addClass("showArea");
    
    if(areafilter!='0')
    {    
        $j("#stories").find(".story,.storyRight").each(function(){               
            if ($j(this).attr("rel").search(new RegExp(areafilter, "i")) < 0) {        
                    $j(this).addClass("hideArea");
                    $j(this).removeClass("showArea");
                    
                } else {
                    $j(this).removeClass("hideArea");
                    $j(this).addClass("showArea");
                }
    })
    }
    else
    {
        $j("#stories").find(".story,.storyRight").removeClass("hideArea");
        $j("#stories").find(".story,.storyRight").addClass("showArea");
    }
}
function solutionChange(area, solution)
{
    //var areafilter = $j(area+" option:selected").attr("value");    
    var solutionfilter = $j(solution).val();
    
    $j("#stories").find(".story,.storyRight").removeClass("hideSolution");
    $j("#stories").find(".story,.storyRight").addClass("showSolution");
    
    if(solutionfilter!=0)
    {    
        $j("#stories").find(".story,.storyRight").each(function(){
            if ($j(this).attr("rel").search(new RegExp(solutionfilter, "i")) < 0) {        
                    $j(this).addClass("hideSolution");
                    $j(this).removeClass("showSolution");
                    
                } else {
                    $j(this).removeClass("hideSolution");
                    $j(this).addClass("showSolution");
                }
        })
    }
    else
    {
        
        $j("#stories").find(".story,.storyRight").removeClass("hideSolution");
        $j("#stories").find(".story,.storyRight").addClass("showSolution");
    }
}

function locationChange2(location) {
    var solutionfilter = $j(location + " option:selected").val();

    $j("#stories").find(".story,.storyRight").removeClass("hideLocation");
    $j("#stories").find(".story,.storyRight").addClass("showLocation");

    if (solutionfilter != 0) {
        $j("#stories").find(".story,.storyRight").each(function () {
            if ($j(this).attr("rel").search(new RegExp(solutionfilter, "i")) < 0) {
                $j(this).addClass("hideLocation");
                $j(this).removeClass("showLocation");

            } else {
                $j(this).removeClass("hideLocation");
                $j(this).addClass("showLocation");
            }
        })
    }
    else {

        $j("#stories").find(".story,.storyRight").removeClass("hideLocation");
        $j("#stories").find(".story,.storyRight").addClass("showLocation");
    }

}

function periodChange(period) {
    var periodfilter = $j(period + " option:selected").val();

    var today = new Date();

    var filterTime = new Date();    
    filterTime.setDate(filterTime.getDate() + parseInt(periodfilter));

    $j("#stories").find(".story,.storyRight").removeClass("hideDate");
    $j("#stories").find(".story,.storyRight").addClass("showDate");

    if (periodfilter != 0) {
        $j("#stories").find(".story,.storyRight").each(function () {

            var date = new Date($j(this).find("input[name='date']").val());
//console.log(date +" < " +today);
//console.log(date < today);

if(date < today)
{
 $j(this).addClass("hideDate");
 $j(this).removeClass("showDate");
}

            else if (date > filterTime) {

                $j(this).addClass("hideDate");
                $j(this).removeClass("showDate");

            } else {
                $j(this).removeClass("hideDate");
                $j(this).addClass("showDate");
            }
        })
    }
    else {

        $j("#stories").find(".story,.storyRight").removeClass("hideDate");
        $j("#stories").find(".story,.storyRight").addClass("showDate");
    }

    

}


function resetFilters(area, solution,date)
{
/*
    $j(area).val(0);
    $j(solution).val(0);
    $j(date).val(0);
    $j("#stories").find(".story,.storyRight").removeClass("hideSolution");
    $j("#stories").find(".story,.storyRight").removeClass("hideArea");

    $j(".office").show();
    $j(".story,.storyRight").removeClass("hideArea");
    $j(".story,.storyRight").removeClass("hideLocation");
*/

    var baseurl = "";
    if (window.location.href.indexOf("?") != -1) {
        baseurl = window.location.href.slice(0, window.location.href.indexOf('?'));
    }
    else {
        baseurl = window.location.href;
    }
    window.location.href = baseurl + "?area=0&location=0";

}

function locationChange(location)
{
     var baseurl = "";
    if (window.location.href.indexOf("?") != -1) {
        baseurl = window.location.href.slice(0, window.location.href.indexOf('?'));
    }
    else {
        baseurl = window.location.href;
    }
    window.location.href = baseurl + "?area=" + $j("#areaFilter option:selected").val() + "&location=" + $j(location).val();    
}

function showlocation() {
    var location = getUrlVars()["location"];   

    if (location != undefined && location != '') {
        //var locationfilter = $j(location).val();
        var locationfilter = location;
        
        $j('#locationFilter option[value=' + location + ']').attr('selected', 'selected');

        $j(".office").show();
        $j(".contactList").find(".story,.storyRight").removeClass("hideLocation");
        $j(".contactList").find(".story,.storyRight").addClass("showLocation");

        if (locationfilter != '0') {
            $j(".contactList").find(".story,.storyRight").each(function () {

                if ($j(this).attr("rel").search(new RegExp(locationfilter, "i")) < 0) {
                    $j(this).addClass("hideLocation");
                    $j(this).removeClass("showLocation");

                } else {
                    $j(this).removeClass("hideLocation");
                    $j(this).addClass("showLocation");
                }
            })
        }
        else {
            $j(".contactList").find(".story,.storyRight").removeClass("hideLocation");
            $j(".contactList").find(".story,.storyRight").addClass("showLocation");
        }
        $j(".office").each(function () {
            if ($j(this).find("div.story:visible").length == 0) {
                $j(this).hide();
            }

        });
    }
}

function expertiseChange(expertise)
{
    var areafilter = $j(expertise).val();
    
    $j(".contactList").find(".story,.storyRight").removeClass("hideArea");
    $j(".contactList").find(".story,.storyRight").addClass("showArea");
    
    if(areafilter!='0')
    {    
        $j(".contactList").find(".story,.storyRight").each(function(){               
            if ($j(this).attr("rel").search(new RegExp(areafilter, "i")) < 0) {        
                    $j(this).addClass("hideArea");
                    $j(this).removeClass("showArea");
                    
                } else {
                    $j(this).removeClass("hideArea");
                    $j(this).addClass("showArea");
                }
        })        
    }
    else
    {
        $j(".contactList").find(".story,.storyRight").removeClass("hideArea");
        $j(".contactList").find(".story,.storyRight").addClass("showArea");
    }
    
}

function clusterChange(area)
{
    var baseurl = "";
    if (window.location.href.indexOf("?") != -1) {
        baseurl = window.location.href.slice(0, window.location.href.indexOf('?'));
    }
    else {
        baseurl = window.location.href;
    }
    window.location.href = baseurl + "?area=" + $j(area).val() + "&location=" + $j("#locationFilter option:selected").val();      
}

function showCluster() {

var area = getUrlVars()["area"];
if (area != undefined && area != '') {

    var areafilter = area;
    $j('#areaFilter option[value=' + area + ']').attr('selected', 'selected');

    //var areafilter = $j(area).val();

    $j(".office").show();
    $j(".contactList").find(".story,.storyRight").removeClass("hideArea");
    $j(".contactList").find(".story,.storyRight").addClass("showArea");

    if (areafilter != '0') {
        $j(".contactList").find(".story,.storyRight").each(function () {
            if ($j(this).attr("rel").search(new RegExp(areafilter, "i")) < 0) {
                $j(this).addClass("hideArea");
                $j(this).removeClass("showArea");

            } else {
                $j(this).removeClass("hideArea");
                $j(this).addClass("showArea");
            }
        })
    }
    else {
        $j(".contactList").find(".story,.storyRight").removeClass("hideArea");
        $j(".contactList").find(".story,.storyRight").addClass("showArea");
    }
    $j(".office").each(function () {
        if ($j(this).find("div.story:visible").length == 0) {
            $j(this).hide();
        }

    });
}

}


function dateChange()
{
    /*alert("Not implemented yet!");*/
}


function changeSection(select)
{
    var filter = $j(select).val();    
    
    $j(".section").removeClass("sectionHidden");
    
    $j(".section").each(function(){
        if($j(this).attr("rel").search(new RegExp(filter, "i")) < 0)
        {
            $j(this).addClass("sectionHidden");
        }
    })
    
    
}

function contactNav()
{
    if($j(".contact .contactInfo").length>1)
    {
        /*  Hide unnecessary region links */
        $j(".regionLink").each(function(){
            var region = $j(this).attr("id");
            if($j(".contact #contact"+region).length==0)
            {
                $j(this).hide();
            }
            else
            {               
                $j(this).css({borderLeft: 'solid 1px #ccc',paddingLeft:'5px'});                
            }
            
            
        });
        
        $j(".contact .contactInfo").css({borderBottom: 'none', paddingBottom: '0',marginBottom:'0'});

        $j(".contact .contactInfo").hide();
        $j(".contact .contactInfo").first().show();
        $j(".regionLink:visible").first().addClass("selected");
        $j(".regionLink:visible").first().css({borderLeft: 'none',paddingLeft:'0'});     
        
        $j(".regionLink").click(function(){
            $j(".regionLink").removeClass("selected");
            var region = $j(this).attr("id");
            $j(this).addClass("selected");
            $j(".contact .contactInfo").hide();            
            $j(".contact #contact"+region).show();
            return false;
        });
    }
    else
    {
        $j("#regionNav").hide();
    }
}


function setEqualHeight(columns)
{
    var tallestcolumn = 0;
    columns.each(
       function()
       {
          currentHeight = $j(this).height();
          if(currentHeight > tallestcolumn)
          {
              tallestcolumn  = currentHeight;
          }
       }
    );
    columns.height(tallestcolumn);
}
function search(word) {

    $j('input#search-site').keypress(function (e) {
        if (e.keyCode == '13') {
            e.preventDefault();
            var val = $j('input#search-site').val();
            window.location = "/SearchResult?search=" + val;
        }
    });
}



// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

