

// this isn't working for IE8 and 9, even with Cufon 1.9i
if (window.Cufon) Cufon.replace('#ghome #titles, .vid_desc');
// if (window.Cufon) alert('hi');
// Cufon.replace('#ghome #titles');
// Cufon.replace('.vid_desc');
// Cufon.replace('h1');

function concatObject(obj) { //for debugging, call on obj before alert
    str='';
    for (prop in obj) {
        str += prop + " value :" + obj[prop] + "\n";
    }
    return(str);
}

function pdf_click() {
    var axel2 = Math.random() + "";
    var ax2 = axel2 * 10000000000000;
    $('body').prepend('<iframe class="dblclk" src="http://fls.doubleclick.net/activityi;src=2917166;type=kiltkids;cat=fpk_down;ord=' + ax2 + '?"></iframe>');
}

function checkSearch() {
    if (document.form2.searchbox.value == "" || document.form2.searchbox.value == "Search") {
        alert("Please enter your search terms.");
        return false;
    } else {
        return true;
    }
}

function gotoLink() {
    var pVal = document.sitelist.goto.options[document.sitelist.goto.selectedIndex].value;
    window.location.replace(pVal);
}

function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){
        document.getElementById(divid).style.display = 'block';
    } else {
        document.getElementById(divid).style.display = 'none';
    }
}

function clearHeaderSearch() {
    var els = document.getElementById('headerSearch').elements;
    for (i = 0; i < els.length; i++) {
        if (els[i].type=="select-one") els[i].options[0].selected = true;
        if (els[i].type=="text") els[i].value = "";
    }
}

function handlepaste(e) {
    alert('Sorry, paste has been disabled in this field.');
    if(!e) { //IE
        var e = window.event;
        e.returnValue = false;
    }
    e.preventDefault(); //FF
}




$(function() {
    //removed 1.8.11
    // was at end, after something broken (but no error reported?) so wouldn't run in IE6, moved up here for quick fix
    // $('#facebook').after('<div class="fb_photos"><h4><a href="http://www.facebook.com/album.php?aid=185291&id=32240386607">Kilts for Kids 2009 <span class="fb_sub">on Facebook</span></a></h4></div>');
    // $.getJSON('https://graph.facebook.com/412354321607/photos?callback=?', function(d) {
    //     var photo = '';
    //     var photos = '';
    // 
    //     $.each(d.data, function(ii, item) {
    //         $(this).each(function(jj, kk) {
    //             photo = kk.images[2].source;
    //             if (ii < 3) photos += '<div class="fb_photo_wrapper"><a href="'+kk.link+'"><img src="'+photo+'" /></a></div>';
    //         });
    //     });
    //     
    //     $('.fb_photos h4').after('<div class="fb_photos_body">'+photos+'</div>');
    // });
    
    //alert('bb');
    // $.validator.addMethod("at_least_one_checkbox", function(value, element) {
    //     var the_list_array = $("input:checked");
    //     //"input[@id=event]:checked")
    //     console.log(the_list_array.length);
    //     //return the_list_array.length > 0;
    //     return false;
    // }, "* Check at least one checkbox.");
    
    // MISC ------------------------------------------------------------------
    $('body#ghome h1').show();
    $("#confirmfield").bind('paste', function(e){handlepaste(e)});
    //$("#tabs").tabs();
    if ($('body#ghome').length) swfobject.embedSWF("common/flash/mapsml.swf", "flashcontent", "220", "120", "9.0.0", "expressInstall.swf");

    $('#k4k #breadcrumb li:first, #k4k #breadcrumb li:nth-child(2)').hide();



    // VALIDATION ------------------------------------------------------------
    if ($().validate) { // only run on pages where plugin has been loaded
        
        $('#mainContent #content form').validate({            
            invalidHandler: function(form, validator) {
                alert('Please complete the highlighted fields.');
                //$('form').jqTransform();
            },
            highlight: function(element, errorClass) {
               if ($(element).attr('type') == 'radio') {
                   var radioName = $(element).attr('name');
                   $('input[name='+radioName+']').addClass('formValidationHighlight');
               } else {
                   $(element).addClass('formValidationHighlight');
               }
            },
            unhighlight: function(element, errorClass) {
                if ($(element).attr('type') == 'radio') {
                    var radioName = $(element).attr('name');
                    $('input[name='+radioName+']').removeClass('formValidationHighlight');
                } else {
                    $(element).removeClass('formValidationHighlight');
                }
            },
            errorPlacement: function(error, element) {
                error.appendTo(null);
            }
            //$("input:checked").rules("add", "at_least_one_checkbox: true");
        });
    }
    
    
    
    // PAGINATION ------------------------------------------------------------
    if ($('#paginateNavPosition').length && $('.paginatedItem').length > 20) {
        var pager = new Pager('paginated', 20);
        pager.init();
        pager.showPageNav('pager', 'paginateNavPosition');

        $(window).bind('hashchange', function(e) { // uses bbq plugin
            var page = parseInt($.param.fragment());
            if (isNaN(page) || page < 1) page = 1;
            pager.showPage(page);
        }).trigger('hashchange');
        // console.log($('#pager').html());
        // console.log($('#paginateNavPosition').html());
    }
    
    if (('#ghome').length) {
        //$('#main_tabs li:not(:first)').hide();
        $('#titles h1:not(:first)').hide();
    }
    
    var listen = $('#title-listen').text();
    var support = $('#title-support').text();
    var action = $('#title-action').text();

    $('#map-stickers area').hover(function(e) {
        var area = $(this).attr('id');
        area = area.substr(5);
        
        if (area == 'listen') $('#title').fadeOut('100', function(){$(this).text(listen);Cufon.refresh();}).fadeIn(100);
        if (area == 'support') $('#title').fadeOut('100', function(){$(this).text(support);Cufon.refresh();}).fadeIn(100);
        if (area == 'action') $('#title').fadeOut('100', function(){$(this).text(action);Cufon.refresh();}).fadeIn(100);
    },
    function(e) {
        hovered = false; // redundant now I guess
    });
    
    //$('#main_tabs li:first a').html('CHILDREN 1<span class="sup">ST</span>');
    
    
    
    // BROWSER CSS KLUDGES (a worrying amount?!) -----------------------------
    
    // really? jq 1.4.2 still needs this to distinguish chrome from safari?
    var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
    // Is this a version of Chrome?
    if($.browser.chrome) {
        userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
        userAgent = userAgent.substring(0,userAgent.indexOf('.'));	
        version = userAgent;
        // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
        $.browser.safari = false;
    }
    if ($.browser.chrome) {
        $('body#ghome #container #mainvisual').css('bottom', '133px');
        //$('#tab1').css('margin-left', '1px');
        $('ul#mainNav li').css('width', '111px');
        $('#logo-logo').css('margin-left', '-3px');
    }
    
    if ($.browser.msie && $.browser.version < 7) $('html').supersleight(); 

    // FF < 3.6 different layout, 2px too low. However Mac/Windows different too. Fubar...
    if ($.browser.mozilla) {
        v = $.browser.version;
        v = v.split('.');
        v = v.join('');
        
        //Gecko '1.9.2' is FF 3.6 I guess  - 1.8.11 this was a bad way to work with the FF version, so done awqay with
        //if (v < 1920) $('body#ghome #container #mainvisual').css('bottom', '138px');
        // see above
        if (navigator.platform == 'Win32') {
            $('body#ghome #container #mainvisual').css('bottom', '138px');
            //$('#tab1').css('margin-left', '1px');
        } else {
            $('body#ghome #container #mainvisual').css('bottom', '132px');
        }
    }
    
    /* Actually asked by Eileen for support for Opera 9.52 bizarrely, hence: */
    if ($.browser.opera && $.browser.version == '9.52') {
        $('#tab1').css('margin-left', '1px');
        $('body#ghome #container #mainvisual').css('bottom', '138px');
    }

    if ($.browser.safari) {
        $('body#ghome #container #mainvisual').css('bottom', '133px');
    }
    
    //alert(concatObject($.browser));
    if (($.browser.safari || $.browser.chrome) && navigator.platform == 'Win32') {
        $('body#ghome #container #mainvisual').css('bottom', '136px');
    }
    
    
    

    // GA --------------------------------------------------------------------
    if (location.hostname == 'www.children1st.org.uk') {
        $.geekGaTrackPage('UA-402580-2');
        
        $("a[href*=.pdf]").click(function() {
            //console.log($(this).attr('href'));
            $.geekGaTrackEvent('pdf', 'download', $(this).attr('href'));
        });
    }
    
    
    
    
    // K4K -------------------------------------------------------------------
    var orig_src, hover_src;
    $('#features img, .k4k-sidebar img').hover(function() {
        orig_src = $(this).attr('src');
        hover_src = orig_src.replace('.png', '-hover.png');
        $(this).attr('src', hover_src);
    },
    function(){
        $(this).attr('src', orig_src);
    });
    
    // js only way to control this I guess
    // if ($('#facebook').length) {
    //     var h = $('#facebook').html();
    //     if (($.browser.msie && $.browser.version <= 8) || ($.browser.mozilla && navigator.userAgent.indexOf('Windows') > -1)) {
    //         h = h.replace('height="555"', 'height="545"');
    //         $('#facebook').html(h);
    //     }
    // 
    //     if ($.browser.chrome || $.browser.safari) {
    //         h = h.replace('height="555"', 'height="550"');
    //         $('#facebook').html(h);
    //     }
    // }
    
    
    
    // Richard's doubleclick code
    // tried moving size attrs etc into CSS but didn't work
    // click code like so to as closely mimic given code as possible, given inability to test
    
    var axel = Math.random() + "";
    var ax = axel * 10000000000000;
    if (location.href == 'http://' + location.hostname + '/') {
        $('body').prepend('<iframe class="dblclk" src="http://fls.doubleclick.net/activityi;src=2917166;type=generic;cat=homepage;ord=' + ax + '?"></iframe>');
    }
    if (location.href.indexOf('http://' + location.hostname + '/136') === 0) {
        $('body').prepend('<iframe class="dblclk" src="http://fls.doubleclick.net/activityi;src=2917166;type=kiltkids;cat=kilt_kid;ord=' + ax + '?"></iframe>');
    }
    if (location.href.indexOf('http://' + location.hostname + '/244') === 0) {
        $('body').prepend('<iframe class="dblclk" src="http://fls.doubleclick.net/activityi;src=2917166;type=kiltkids;cat=fund_pac;ord=' + ax + '?"></iframe>');
        $('a[href*=.pdf]').click(function() {
            $('body').prepend('<script type="text/javascript" id="DoubleClickFloodlightTag409098">pdf_click()</script>');
        });
    }
    
    // misc fix
    if ($('#breadcrumb li:last').find('a').length) $('#breadcrumb li a:last').css('background', 'none');
    
    
    
    // DOM INSERTION (ELSE TMCE WILL STRIP)
    // $('#granny-wrapper').prepend('<div id="granny-header"></div>');
    // $('#granny-col2').prepend('<div id="granny-point"></div>');
    // $('#granny-counter').after('<div id="granny-map"></div>');
    

});



