


var Vistair = function(){ }
Vistair.sitemap = {
    

    cache:'',
    opened:false,
    animating:false,
    open:function(){
        if(Vistair.sitemap.cache == ''){
            $.ajax({ type: "POST", url: "/sitemap/?ajax=true",
                success:function(response){
                    Vistair.sitemap.cache = response;
                    $('#map').html(Vistair.sitemap.cache);
		    
                    Vistair.sitemap.post_open()
                }
            })
        } 
        else Vistair.sitemap.post_open();
    },
    post_open:function(){
        Vistair.sitemap.animating = true;         
        $('#map').fadeIn('fast');
        var targetOffset = $('#footer').offset().top
        $('html,body').animate({scrollTop: targetOffset}, 1000, function(){
            Vistair.sitemap.animating = false;         
        });
        Vistair.sitemap.opened = true;
        $('#openmap').removeClass('mapdown')
        $('#openmap').addClass('mapup')
        
    },
    close:function() {
        if(Vistair.sitemap.opened){
            var targetOffset = $('#chassis').offset().top
            $('html,body').animate({scrollTop: targetOffset}, 1000, function(){
                $('#map').fadeOut('fast');
            });
            Vistair.sitemap.opened = false;
            $('#openmap').addClass('mapdown')
            $('#openmap').removeClass('mapup')
        }
    }
    
}





$(document).ready(function() {

  /* make all home feature boxes the same height */
  if($('#homefeatures').length>0)
  {
      // ADDED IN BY ROB CHARLWOOD TO FIX THE ISSUE OF FOOTER MERGING INTO THE FEATURE BOXES ON THE HOMEPAGE
      // GRAB HEIGHTS OF EACH CHEEKY LITTLE BOX
      newsHeight = $('#home_news').height();
      aboutHeight = $('#home_about').height();
      testimonialHeight = $('#home_testimonials').height();
      
      // CREATE ARRAY TO STORE THE VALUES
      var heights = [newsHeight, aboutHeight, testimonialHeight];
      
      // WORK OUT WHICH IS GREATEST
      function calcHeight(heights)
      {
          // ASSIGN LOCAL METHOD VALUE
          var myheights = heights;
          
          // DEFINE ANSWER VALUE
          //var ans = Math.max(myheights[0], heights[1]);
          var ans = 0

          // LOOP THROUGH THAT NAUGHTY ARRAY AND HOLLA WHICH ONE IS BIGGEST
          for (var i = 0; i < myheights.length; i++ ) 
          {
              // CHECK THE MAX HEIGHT
              ans = Math.max(ans,myheights[i]);
          }
          
          // RETURN THE HEIGHT HEIGHT IN NUMBER FORMAT
	  // minus the vertical padding
          return ans;
      }
     
    // GRAB THE GREATEST HEIGHT USING THE ABOVE METHOD AND THEN APPEND A 'PX' TO IT FOR CSS PURPOSES
    greatestHeight = calcHeight(heights) + 'px';
        
    // APPLY THE GREATEST HEIGHT TO ALL THE BOXES
    $('#home_news').css('height', greatestHeight);
    $('#home_about').css('height', greatestHeight);
    $('#home_testimonials').css('height', greatestHeight);
  }
    //  need to check for the highest feature box, then use that to resize!

  /* style lists within page trunk */
  if($('#copy').length>0)
  {
    var bullet_colour = $('#copy').attr('class')
    $('#copy ul li').each(function() {
      $(this).addClass(bullet_colour)
      var text = $(this).html()
      $(this).html('<li><span class="normal">'+text+'</span></li>')
      //$(this).html('<li>'+text+'</li>')
    });
  }
  
  
  
  /* style asterixes on forms */
  $('label').each(function() {
    var text = $(this).text()
    text = text.replace(' *', '<em>*</em>')
    $(this).html(text)
  });

  /* for external links */
   $('a.new-window').each(function(){
     $(this).click(function(){
       window.open(this.href);
       return false;
     });
   });
   
   /* replace file upload image */
  if($("input[type=file]").length>0)
   {
    $("input[type=file]").filestyle({ 
      image: "/site_media/images/choose_file.jpg",
      imageheight : 25,
      imagewidth : 89,
      width: 400
    });
   }
   
   /* upgrade Django defaut CSS */
   var h2_css = {
	'font-family' : 'Georgia, "Trebuchet MS", Helvetica, Arial, Verdana, san-serif',
	'font-size' : '16px',
	'line-height' : '20px',
	'color' : '#0e344e',
	'position' : 'relative',
	'display' : 'block',
	'margin-bottom' : '10px',
	'font-weight' : 'bold'
   }
     
   $("label[for=id_cv]").css(h2_css)
   
   



    // Embed YouTube video
    embeddedYouTube = 
    '<object width="510" height="388">' +
    '<param name="movie" value="http://www.youtube.com/v/Wjsr6wo2cRg"></param>' +
    '<param name="allowFullScreen" value="true"></param>' +
    '<param name="allowscriptaccess" value="always"></param>' +
    '<embed ' +
    'src="http://www.youtube.com/v/Wjsr6wo2cRg" ' +
    'type="application/x-shockwave-flash" ' +
    'allowscriptaccess="always" ' +
    'allowfullscreen="true" ' +
    'width="510" ' +
    'height="388">' +
    '</embed>' +
    '</object>';
    
    $('a[href^="http://www.youtube.com/watch?v="]').each(function() {
        a = $(this);
        matched = a.attr('href').match(/^http:\/\/www.youtube.com\/watch\?v=([A-Za-z0-9_-]*)$/)
        if(matched) {
            a.after(embeddedYouTube.replace(/Wjsr6wo2cRg/g, matched[1]))
            a.remove()
        }
    });






    /* live search */
    var term = new String('')
  
    if($('#flashz').length>0)
        var myFlashMovie = document.getElementById("flashz");  // added 'z' in swfobject.js - ie bug fix
    

    $('#livesearch input[type="text"]').focus(function(event){
	$(this).val('');
    });
    
    
    
    $("#livesearch input[type='text']").keyup(function(event){
      
	term = $(this).val().toString()
	
        if($('#flashz').length>0)
	    myFlashMovie.myFunctionId(false);
	
	
	if(term!='' && term.length>=2)
	{
	    
		$.ajax({
		  type: "POST",
		  url: '/search/',
		  data: 'x=0&y=0&q='+term+'&models=solutions.solution',
		  
		      success: function(response){

			  $('#searchresults').hide()
			  $('#searchresults').html(response)
			  $('#searchresults').show()
			  
			  $('#close_search').click(function(){
			      $(this).parent().slideUp('fast')
			      
			      if($('#flashz').length>0)
				  myFlashMovie.myFunctionId(true)
			      
			})		  
		    }
		})
	}
    });
    
    
    
  
    










  /* newsletter signup */
  $("#signup input[type='text']").focus(function(event){
    $(this).val('')
  });

  if($("#signupform").length>0)
  {
    
    $("#signupform input[type='text']").focus(function(event){
	$(this).val('')
    }); 
    
    
    $("#signupform").submit( function () {

    email = $("#signupform input[type='text']").val()
	    $.ajax({
			type: "GET",
			url: '/signup/',
			data: "email="+email,
			success: function(response){
			   if(response == 'true')
			   {
			      $('.newsletterContent form').hide()
			      $('.newsletterContent div').html('<p>Thank you for registering for our newsletter</p>')
			   }
			   else
			   {
			      $("#signupform input[type='text']").val('Valid Email Required')
			   }
			}
	    });
	
	
      return false
    
    
    } );
  }






 
 






  /* site map */
	$('.sitemap').click(function() {
        if(!Vistair.sitemap.opened)
            Vistair.sitemap.open();
        else
            Vistair.sitemap.close();
        return false;
    });



});

// CREATE METHOD TO OPEN SEND TO FRIEND PAGE
function popup()
{
    //  NEW METHOD TO CHANGE CATEGORIES- ADDED BY ROB CHARLWOOD 05/05/2009
    var a = document.URL.split("//");
    a = (a[1] ? a[1] : a[0]).split("/");
        
    // UPDATE THE NEW PAGE
    a[1] = '/send-to-friend/';
        
    // REJOIN THE URL AND FORWARD THE USER ON
    b = "http://" + a[0] + a[1];
    // END OF FIX
    
    // OPEN THE POPUP
    window.open(b, 'sendtofriend', 'width=390,height=500');
}

function fixIE()
{
    if(document.all)
    {
        referrer = window.opener.location;
        document.getElementById('referrer_header').innerHTML = referrer;
        document.getElementById('id_referred_to').value = referrer;
    }
    else
    {
        return false;
    }
}


