// general functions
function addLoadEvent(func) {	
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function CheckBoxValidatorDisableButton(chkId, mustBeChecked, btnId) {
    var button = document.getElementById(btnId);
    var chkbox = document.getElementById(chkId);

    if (button && chkbox) {
        button.disabled = (chkbox.checked != mustBeChecked);
    }
}

function CheckBoxValidatorEvaluateIsValid(val) {
    var control = document.getElementById(val.controltovalidate);
    var mustBeChecked = Boolean(val.mustBeChecked == 'true');

    return control.checked == mustBeChecked;
}

function CheckBoxListValidatorEvaluateIsValid(val) {
    var control = document.getElementById(val.controltovalidate);
    var minimumNumberOfSelectedCheckBoxes = parseInt(val.minimumNumberOfSelectedCheckBoxes);

    var selectedItemCount = 0;
    var liIndex = 0;
    var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    while (currentListItem != null) {
        if (currentListItem.checked) selectedItemCount++;
        liIndex++;
        currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    }

    return selectedItemCount >= minimumNumberOfSelectedCheckBoxes;
}

function pageLoad(sender, args) {
      if(args.get_isPartialLoad()){   
         //tb_init('a.thickbox');   
    }   
 }
 
function getElementByClass(classname){ 
     var messages=new Array();
     var inc=0; 
     var alltags=document.all? document.all : document.getElementsByTagName("*"); 

     for (i=0; i<alltags.length; i++){
        if (alltags[i].className.match(classname)){ 
         messages[inc++]=alltags[i]; 
         }
     }
     if(messages != null){return messages;}
     else {return null;}
} 

function getAbsX(elt) {return (elt.x) ? elt.x : getAbsPos(elt,"Left");}
function getAbsY(elt) {return (elt.y) ? elt.y : getAbsPos(elt,"Top");}
function getAbsPos(elt,which) {
    var iPos = 0;
    while (elt != null) {
     iPos += elt["offset" + which];
     elt = elt.offsetParent;
    }
    return iPos;
}

function enlargeImageThumb(x) {
    var eleMainImage = document.getElementById('ctl00_cph_content_main_img');  
    var eleMainLnk = document.getElementById('ctl00_cph_content_main_lnk');
    var eleMainZoomLnk = document.getElementById('ctl00_cph_content_main_zoomlnk');
    var eleThumbImage = getElementByClass('thumb'); 
    
    if (eleMainLnk!=null){ 
        var s = x.href.split("?hi-res=");
        //alert(s[1]);
        eleMainZoomLnk.href = s[1];      
        eleMainLnk.href = s[1];
        eleMainImage.src = x.href;        
    }
    return false;    
}

function applyEventsToImages() {
    var eleThumbLnk = getElementByClass('thumblnk');
    if (eleThumbLnk!=null){
            for (var intCounter = 0; intCounter < eleThumbLnk.length; intCounter++) {
            eleThumbLnk[intCounter].onmouseover = function ()
	                {enlargeImageThumb(this);
	                return false;}  
	        eleThumbLnk[intCounter].onclick = function ()
	                {return false;}                  			
                }
            }
} 

function validatePostCode(value, strDefault)
{
    var postcodeFilter=/^[A-Za-z]{1,2}[0-9A-Za-z]{1,2}[ ]?[0-9]{0,1}[A-Za-z]{2}$/;
    
    if (!postcodeFilter.test(value) && value == '' || value == strDefault) 
    {
        return true;
    }
    else
    {
        return false;
    }	
}

var timeout = 6500;
var duration = 800;
var fadeout = 1800;
var counter = 0;

function nextArticle() {
    var msg = $('#newsMsg p')
    var lnk = $('#newsMsg a')
        
    if (typeof (arrNewsTitle) !== 'undefined') {
        if (arrNewsTitle != null) {
            var nummsgs = arrNewsTitle.length;
            if (counter == nummsgs) { counter = 0; }
            var title = arrNewsTitle[counter];
            var id = arrNewsId[counter];
            var info = arrNewsDescription[counter];
            
            msg.html(info).text();
            lnk.html(title).text();
            lnk.attr('href', '/news/article.aspx?id=' + id);

            counter += 1;
            showArticle();
        }
    }
}

function showArticle() {
    var msg = $('#newsMsg')
    setTimeout(function () { msg.animate({ opacity: ['toggle'] }, { duration: duration, specialEasing: { opacity: 'linear' }, complete: function () { nextArticle() } }) }, timeout);
    msg.fadeIn();
}

var offercounter = 0;
function nextOffer() {
    var msg = $('#offerMsg p')
    var lnk = $('#offerMsg a')

    if (typeof (arrOfferTitle) !== 'undefined') {
        if (arrOfferTitle != null) {
            var nummsgs = arrOfferTitle.length;
            if (offercounter == nummsgs) { offercounter = 0; }
            var title = arrOfferTitle[offercounter];
            var id = arrOfferId[offercounter];
            var info = arrOfferSynopsis[offercounter];

            msg.html(info).text();
            lnk.html(title).text();
            lnk.attr('href', '/offers/details.aspx?id=' + id);

            offercounter += 1;
            showOffer();
        }
    }
}

function showOffer() {
    var msg = $('#offerMsg')
    setTimeout(function () { msg.animate({ opacity: ['toggle'] }, { duration: duration, specialEasing: { opacity: 'swing' }, complete: function () { nextOffer() } }) }, timeout);
    msg.fadeIn();
}



function ismaxlength(obj) {
    var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length > mlength)
        obj.value = obj.value.substring(0, mlength)
}


function TreeViewSetup() {
    // setup the drop-down media selector
    var e = $('.AspNet-TreeView-ClickableNonLink');
    var c = $('.AspNet-TreeView-Collapse');

    e.click(function() {
        $("#content-wrap").css("height", "auto");
        $("#content-left").css({ 'height': 'auto', 'min-height': '800px' });
        $("#content-right").css({ 'height': 'auto', 'min-height': '400px' });
        $('#content-wrap').equalHeights();
    });

    c.click(function() {
        $("#content-wrap").css("height", "auto");
        $("#content-left").css({ 'height': 'auto', 'min-height': '800px' });
        $("#content-right").css({'height': 'auto', 'min-height': '400px' });
        $('#content-wrap').equalHeights();
    });
}

function AccordionSetup() {
    // setup the drop-down media selector
    var linkh = $('.accordionHeader');
    var links = $('.accordionHeaderSelected');

    linkh.click(function() {
        Cufon.replace('.accordionHeader', { fontFamily: 'HelveticaNeue-CondensedBold', color: '#363636' });
        Cufon.replace('.accordionHeaderSelected', { fontFamily: 'HelveticaNeue-CondensedBold', color: '#ffffff' });
    });

//    linkh.mouseover(function() {
//        Cufon.replace($(this), { fontFamily: 'HelveticaNeue-CondensedBold', color: '#902a3e' });
//        //$(this).toggleClass('accordionHeaderSelected');
//    });

//    linkh.mouseout(function() {
//        Cufon.replace($(this), { fontFamily: 'HelveticaNeue-CondensedBold', color: '#363636' });
//        //$(this).toggleClass('accordionHeader');
//    });

    links.click(function() {
        Cufon.replace('.accordionHeader', { fontFamily: 'HelveticaNeue-CondensedBold', color: '#363636' });
        Cufon.replace('.accordionHeaderSelected', { fontFamily: 'HelveticaNeue-CondensedBold', color: '#ffffff' });
    });

}

function siteInit() {
    //cookieSetup();
    //mediaSetup();
    //AccordionSetup();
    //tooltipSetup();
    //TreeViewSetup();
}

$(document).ready(function() {
    siteInit();
});

