// Common.js
// ---------
// 
// For EP UDC (Urban Design Compendium)
// Requires jQuery 1.1.4 or higher
// Initial code, Martin Paton, Developer, Amaze
// 13/08/2007

// Modified for HCA Build, Paul Tilsed, Developer, Amaze
// 01/12/08

var intervalId = 0;

var inputButtonRolloverClass = "buttonRollover";

$(document).ready(function(){

    prepareTooltips();
    addTableColours();
    addFontSize();
    
    $("a.helper").focus(function() {
        $(this).addClass("focus");    
    });

    $("a.helper").blur(function() {
        $(this).removeClass("focus");    
    });

    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch(err) {}
        
    $("a", "#linkstable").click(function() {
    
        if ($(this).attr("href") == "#skipnav") return true;
        return popURL($(this).attr("href"), 'LINK');
    });
    
    $("a", "li.externallink").click(function() {
        return popURL($(this).attr("href"), 'LINK');
    });   
    
    
    // Initiate Google check
    intervalId = setInterval("googleCheck()", 500);           
 });
  function doClear(theText) 
{
     if (theText.value == theText.defaultValue)
 {
         theText.value = ""
     }
 }

//----------------------------
//
// Table Colors
//
//----------------------------
 function addTableColours()
 {
    $("table.hl-rows tr:nth-child(even)").addClass("h-light");
 }
//----------------------------
//
//Tootltip Functionality
//
//----------------------------
function prepareTooltips()
{
    $("ul#socialbookmarks li a.tipme").tooltip({
     
    bodyHandler: function() { 
        return $(this).html(); 
    }, 
    showURL: false 
});
}
//----------------------------
//
//Google Analytics
//
//----------------------------
function googleCheck()
{
    if (urchinTracker)
    {       
        clearInterval(intervalId);
        
        $("a").click(function() {
                var url = $(this).attr("href");
                categoriseType(url);
        });       
        
        $("#searchsite").click(function() {    
                callTracker('/internal search/' + $("#searchterm").attr("value"));
        });   
        
        $("#gobody").click(function() {
                callTracker('/internal search/' + $("#searchtermbody").attr("value"));
        });    
    }
}
 // Actually instigate tracking call
function callTracker(pageview) 
{
       urchinTracker(pageview);
} 
function categoriseType(s) {
    var c = '';
    if (s.indexOf('http://') > -1) {
        c = '/url clicked/';
    } else if (s.indexOf('public/documents/') > -1) {
        c = '/document viewed/';
    }  
    if (c != '') {
        callTracker(c + adjustPath(s));
    }
}
function adjustPath(s) {
    
    if (s.indexOf('public/documents/') > -1) {
        s = s.replace('public/documents/','');
    }
    return s;
}
function popURL (href, type) {      
    var url = href;      
    var w = window.open(url, type ,'');     
    w.focus();
           
    return false;
}
function resize() {
     if (navigator.appVersion.match(/MSIE [0-6]\./)) {
        var currentSize = document.body.clientWidth;
                
        if (currentSize <= 830) 
        {            
            $("#sitecontainer").css("width", "700px");
        } else if ((currentSize > 830) && (currentSize <= 950)) {
            $("#sitecontainer").css("width", "85%");
        } else if (currentSize > 950) {
            $("#sitecontainer").css("width", "72%");
        }               
    }
 }
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
// ------------------------------------------------------------------------------
//
// Validation Functionality.
//
// ------------------------------------------------------------------------------
//*************************************//
// Contact Us                          //
//*************************************//
 function checkFirstName(source, args)
 {
    checkValue("#txtFirstName", args)
 }
  function checkSurname(source, args)
 {
    checkValue("#txtSurname", args)
 }
  function checkEmailAddress(source, args)
 {
    var email = $("#txtEmailAddress").val();
    $("#txtEmailAddress").css("border", "#999999 solid 1px");
    if (echeck(email)){
        args.IsValid = false;
        $("#txtEmailAddress").css("border", "red solid 2px");
    }
 }
 function checkOrganisation(source, args)
 {
    checkValue("#txtOrganisation", args)
 }
 function checkPhoneNumber(source, args)
 {
    checkValue("#txtPhone", args)
 }
 function checkReason(source, args)
 {
     args.IsValid = false;
     if (($("#cblReasonList_0").attr("checked")))
     {args.IsValid = true;}
     if (($("#cblReasonList_1").attr("checked")))
     {args.IsValid = true;}
     if (($("#cblReasonList_2").attr("checked")))
     {args.IsValid = true;}
     if (($("#cblReasonList_3").attr("checked")))
     {args.IsValid = true;}
     if (($("#cblReasonList_4").attr("checked")))
     {args.IsValid = true;}
     if (($("#cblReasonList_5").attr("checked")))
     {args.IsValid = true;}
     $("#txtReason").css("border", "#999999 solid 1px");
     if($("#chbOther").attr("checked")){             
         checkValue("#txtReason", args)
     }
 }
 
 function checkBrand(source, args)
 {
     args.IsValid = false;
     if (($("#cblWhichBrand_0").attr("checked")))
     {args.IsValid = true;}
     if (($("#cblWhichBrand_1").attr("checked")))
     {args.IsValid = true;}
     if (($("#cblWhichBrand_2").attr("checked")))
     {args.IsValid = true;}
 }
  function checkSubject(source, args)
 {
    checkValue("#txtSubject", args)
 }
  function checkEnquiry(source, args)
 {
    checkValue("#txtEnquiry", args)
 }
 function checkValue(control, args)
 {
    var enquiry = $(control).val();
    $(control).css("border", "#999999 solid 1px");
    if (enquiry == ""){
        args.IsValid = false;
        $(control).css("border", "red solid 2px");
    }else
    {
    args.IsValid = true;
    }
 }
 function checkConditions(source, args)
 {
    args.IsValid = (($("#chConditions").attr("checked")));    
 }
 function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return true
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return true
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return true
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return true
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return true
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return true
		 }
		 if (str.indexOf(" ")!=-1){
		    return true
		 }
 		 return false					
	}
// ------------------------------------------------------------------------------
//
// Custom Style Sheets
//
// ------------------------------------------------------------------------------
//* Font size functions *//
function addFontSize()
{
   var size = readCookie("fontsize");
   var title = size ? size : "62.5%";
   switchStylestyle(title);
}
function increaseFont(){
   var size = readCookie("fontsize");
   switch (size) {
      case '42.25%' :
         switchStylestyle('52%');
         break;
      case '52%' :
         switchStylestyle('62.5%');
         break;
      case '62.5%' :
         switchStylestyle('75%');
         break;
      case '75%' :
         switchStylestyle('85%');
         break;
      case '85%' :
         break;
      default :
         switchStylestyle('85%');
         break;
   }
}
function decreaseFont(){
	
	   var size = readCookie("fontsize");
   switch (size) {
      case '85%' :
         switchStylestyle('75%');
         break;
      case '75%' :
         switchStylestyle('62.5%');
         break;
      case '62.5%' :
         switchStylestyle('52%');
         break;
      case '52%' :
         switchStylestyle('42.25%');
         break;
      case '42.25%' :
         break;
      default :
         switchStylestyle('42.25%');
         break;
   }
}
function switchStylestyle(fontsize)
{
   $("body").css({'font-size' : fontsize});
    var highcontrast = readCookie("contrast");
    if (highcontrast == 'contrast') {
        switchcontraststyle('contrast')
    }

   createCookie('fontsize', fontsize, 365);
}
//* Cookie managment *//
//Create Cookie persist for a year
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
        document.cookie = name+"="+value + expires +"; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    
    return null;
}
//* Contrast Functions *//
function switchcontrast(){
   var size = readCookie("contrast");
   switch (size) {
      case 'contrast' :
         switchcontraststyle('screen');
         break;
      default :
         switchcontraststyle('contrast');
         break;
         }
}
function switchcontraststyle(styleName)
{
   $('link[rel*=style][title]').each(function(i)
   {
          if (this.getAttribute('title') == 'screen')
          {
           this.disabled = true;
          }
          if (this.getAttribute('title') == 'contrast')
          {
           this.disabled = true;
          }  
   });
      $('link[rel*=style][title]').each(function(i)
   {
   if (this.getAttribute('title') == styleName) {
            this.disabled = false;        
            }   
   });
   createCookie('contrast', styleName, 365);
}

// ------------------------------------------------------------------------------
//
// Image Map
//
// ------------------------------------------------------------------------------
function showImg(divId)
	{
		document.getElementById(divId).style.visibility="visible";

	}
function hideImg(divId)
	{
		document.getElementById(divId).style.visibility="hidden";
	
	}
	
	
// ------------------------------------------------------------------------------
//
// Google Map
//
// ------------------------------------------------------------------------------
function showmap(lat,lon,pointer)
{
      pointer = "map-" + pointer;
      pointer = document.getElementById(pointer);
      $(pointer).height(250);
      $(pointer).css("display", "block");
      if (GBrowserIsCompatible()) 
      {
        map = new GMap2(pointer);
	    //new map UI
	    map.addControl(new GMenuMapTypeControl());
	    map.addControl(new GSmallZoomControl3D());
        map.setCenter(new GLatLng(lat,lon), 16);
        var hcaicon = new GIcon(); 
        hcaicon.image = "/img/site-icons/hca-marker.png";
        hcaicon.iconSize = new GSize(20, 34); 
        hcaicon.shadow = "/img/site-icons/hcashadow.png"; 
        hcaicon.shadowSize = new GSize(37, 34);
        hcaicon.iconAnchor = new GPoint(10, 33);
        hcaicon.infoWindowAnchor = new GPoint(10, 33);
        
        var point = new GLatLng(lat,lon);
        var marker = new GMarker(point, hcaicon);
        map.addOverlay(marker);
        map.savePosition()
        }
}

function showmaponpage(lat,lon)
{
      pointer = document.getElementById("venue-map");
      $(pointer).height(250);
      $(pointer).css("display", "block");
      if (GBrowserIsCompatible()) 
      {
        map = new GMap2(pointer);
	    //new map UI
	    map.addControl(new GMenuMapTypeControl());
	    map.addControl(new GSmallZoomControl3D());
        map.setCenter(new GLatLng(lat,lon), 16);
        var hcaicon = new GIcon(); 
        hcaicon.image = "/img/site-icons/hca-marker.png";
        hcaicon.iconSize = new GSize(20, 34); 
        hcaicon.shadow = "/img/site-icons/hcashadow.png"; 
        hcaicon.shadowSize = new GSize(37, 34);
        hcaicon.iconAnchor = new GPoint(10, 33);
        hcaicon.infoWindowAnchor = new GPoint(10, 33);
        
        var point = new GLatLng(lat,lon);
        var marker = new GMarker(point, hcaicon);
        map.addOverlay(marker);
        map.savePosition()
        }
}

// ------------------------------------------------------------------------------
//
// Bind Events Listings
//
// ------------------------------------------------------------------------------
function bindEventLinks()
{
$.expr[':'].external = function(obj)
{    return obj.hostname != location.hostname;};
     var mediaLinks = $("div.event-intro a");
     for(var i=0;i<mediaLinks.length;i++){
            if (mediaLinks[i].getAttribute('rel') != null) {
            
                    mediaLinks[i].setAttribute('href', '#e');
                    
                    mediaLinks[i].onclick=function() { 
                    $("div.event-intro").show();
                    $("div.event-details-index").hide();
                    var pointerID = this.getAttribute('rel')
                    $(this.parentNode).hide('blind', 'normal', function(){
                            $("div#" + pointerID).show('blind','slow');
                    });

                }
                
            }
        }  
}



