/*
    Created on : Jul 2010
    Author     : IanDundas.co.uk

TODO: Separate out JS that will only be used on one action into it's own file
TODO: tidy up this file.
*/


var symbolsHash = [

    /* index page*/ {
        selector: 'body#index-index img#mi6techvault2-logo',
        left: 200,
        top: -520,
        img: "hiddenicons_skel.png"
    },
    /* index page #2 */ {
        selector: 'body#index-index a#newbook3-logo-link',
        left: 615,
        top: -315,
        img: "hiddenicons_scorp.png"
    },
    /* alex gadgets */ {
        selector: 'body#content-page div#wrapper',
        left: 236,
        top: -395,
        img: "hiddenicons_point.png",
        dependson: 'img#alexgadgetgallery-center'
    },
    /* alex gadgets 2*/ {
        selector: 'body#content-page div#wrapper',
        left: 850,
        top: -205,
        img: "hiddenicons_ark.png",
        dependson: 'img#alexgadgetgallery-center'
    },
    /* anthony horowitz talks*/ {
        selector: 'body#content-page div#wrapper',
        left: 750,
        top: -705,
        img: "hiddenicons_snake.png",
        dependson: 'img#placeholder368-logo'
    },
    /* competition 1*/ {
        selector: 'body#index-competitionentry div#wrapper',
        left: 0,
        top: -640,
        img: "hiddenicons_storm.png"
    },
    /* competition 1*/ {
        selector: 'body#index-competitionentry div#wrapper',
        left: 905,
        top: -915,
        img: "hiddenicons_eagle.png"
    },
    /* competition 1*/ {
        selector: 'body#index-mi6techvault div#wrapper',
        left: 250,
        top: -700,
        img: "hiddenicons_croc.png"
    },

]

function alignSymbol(hash)
{

    $(hash.selector).each(function(){

        if(hash.dependson!=null){
            if($(hash.dependson).size()==0)return;
        }

        offset = $(this).offset();
        position = $(this).position();

        abs_pos_left = position.left+hash.left;
        abs_pos_top = position.top+hash.top;

        symbol = $('<img src="/images/hidden_icons/'+hash.img+'" />');
        $(symbol)

            .css('left',hash.left+'px')
            .css('top',hash.top+'px')
            .addClass('logo-addrollover')
            .addClass('alexhiddensymbol')
            .attr("rolloversrc","/images/hidden_icons_hover/"+hash.img)


        $('div#wrapper').append("<div class=\"normalise\"></div>")
        $('div#wrapper .normalise').append(symbol)


        jQuery.preLoadImages(hash.img, "/images/hidden_icons_hover/"+hash.img);

    })

}


$(document).ready(function(){

for (i in symbolsHash){

    alignSymbol(symbolsHash[i])

}


});

















/* Thanks to http://www.switchonthecode.com/tutorials/using-jquery-slider-to-scroll-a-div for this */
function  handleSliderChange(e, ui){
    var maxScroll = $("#content-scroll").attr("scrollWidth") -
    $("#content-scroll").width();
    $("#content-scroll").animate({
        scrollLeft: ui.value * (maxScroll / 100)
    }, 1000);
}
/* Thanks to http://www.switchonthecode.com/tutorials/using-jquery-slider-to-scroll-a-div for this */
function handleSliderSlide(e, ui){
    var maxScroll = $("#content-scroll").attr("scrollWidth") -
    $("#content-scroll").width();
    $("#content-scroll").attr({
        scrollLeft: ui.value * (maxScroll / 100)
    });
}

//char array from http://code.google.com/p/jquery-matrix
var chars = "0123456789";

var rand = function (max) {
  return max? Math.round (Math.random () * max) : 0;
};

function domatrixeffect(element){
    if($(element).hasClass('clicked'))return;
    char = rand(chars.length-1)
    $(element).val(chars.charAt(char))
    window.setTimeout (function () {domatrixeffect(element);}, 80);
}


function verifysupersecret(){
    supersecretkey='61723584'; // SHH NO PEEKING!
    givenkey='';

    $('div#supersecretnumbers input.mi6techvault-number').each(function(){
        givenkey+=$(this).val();
    })
    return givenkey==supersecretkey;
}

$(function(){


    //TODO does this need to be AJAX'd? probably now
    $('form#supersecretnumbers').submit(function(){
        if (!verifysupersecret()){
            $.fancybox({
                    'padding'		: 0,
                    'href'		: '/images/AlexRider_Tech_denied.jpg',
                    'title'   		: 'ACCESS DENIED',
                    'transitionIn'	: 'elastic',
                    'transitionOut'	: 'elastic'
            });
        }
        else{
            window.location='/topsecret/accessgranted';
        }
        return false;
    })


    $('div#supersecretnumbers input.mi6techvault-number')
    .keyup(function(key){

        if (verifysupersecret())
            $('div#supersecretnumbers input.mi6techvault-number').addClass('verified');
        else
            $('div#supersecretnumbers input.mi6techvault-number').removeClass('verified');

        if( key['keyCode']!=8 && key['keyCode']!=13 && key['keyCode']!=9)
            $(this).next('.mi6techvault-number').select()
    })
    .each(function(){
        domatrixeffect(this);
    })
    .mouseup(function(){
        $(this).select()
    })
    .select(function(){
        if(!$(this).hasClass('clicked')){
            $(this).val('').addClass('clicked');
        }
    })


    //the .each syntax is used here as shorthand so that we don't get an error from e.g.  .fancybox()  when fancybox isn't included. TODO: separate this out into separate files
    $('.js-width').each(function(){
        $(this).css('width',$(this).attr('necessary-width')+'px');
    })

    $('.js-height').each(function(){
        $(this).css('height',$(this).attr('necessary-height')+'px');
    })

    $('a.fancybox').each(function(){
        $(this).fancybox({
                'titleShow'		: false,
                'transitionIn'	: 'elastic',
                'transitionOut'	: 'elastic'
        });
    })

    $("#content-slider").slider({
        animate: true,
        change: handleSliderChange,
        slide: handleSliderSlide
    });


    $('div#header').css('cursor','pointer').click(function(){window.location='/'}).hover(function(){window.status='http://alexrider.iandundas.co.uk'});

    $('#gadgetentrantage').each(function(){
        if(!$.browser.msie)
        {
            $(this).csb({
                style: 'custom-dropdown',
                mode: 'select',
                callback: function(arg){
                    $('.hide').hide();
                    $("#moodul_"+selected).show();
                }
            })
        }
    });

    $('img.logo-addrollover').hover(
        function(){
            if ($(this).attr('rolloversrc'))
                $(this).attr('tempsrc',$(this).attr('src')).attr('src',$(this).attr('rolloversrc'))

        },function(){
            $(this).attr('src',$(this).attr('tempsrc'))
    })

    $('div.flashmessage').effect("highlight",{}, 'fast').effect("highlight",{}, 'medium').effect("highlight",{}, 'slow');

    $('div.custom-scroll-content').each(function(){
        $(this).jScrollPane({scrollbarWidth:15, scrollbarMargin:10});
    });


    $("input[type=file]").each(function(){
        $(this).filestyle({
            image: "/images/filebrowse-icon.png",
            imageheight : 19,
            imagewidth : 56,
            width : 250
        })
    })
    $("input.file").css('width','272px');

    $('input[type="checkbox"]').each(function(){
        $(this).ezMark();
    });

    //TODO:
//    $('input.file').attr('disabled','disabled').val($('input#gadgetupload').attr('placeholder'));

    $('.make-hyperlink')
        .click(function(){
            if ($(this).attr('href')){
                window.location = $(this).attr('href');
            }
        })
        .css('cursor','pointer')
        .hover(function(){
            $(this).find('a').each(function(){
                $(this).addClass('hover');
            })
        },function(){
            $(this).find('a').each(function(){
                $(this).removeClass('hover');
            })
        })

    $('form#competitionentry').submit(function(){

        $return = true;
        $return = $return && $("form#competitionentry").validate().form();
        if ($('input#gadgetupload').val()==''){
            //please upload your gadget
            $('input.file.competitionentry').addClass('error')
        }
        if(!$('input#gadgettermsandconditions').attr('checked')){
            $('input#gadgettermsandconditions').parent().addClass('checkerror');
        }

        if ( $('input#gadgetentrantnewsletter').attr('checked')==true ){
              if ($('input#gadgetentrantoverthirteen').attr('checked')==false && $('input#gadgetentrantparentalconcent').attr('checked')==false){
                  //please provide concent
                  //            $('input#gadgetentrantoverthirteen').parent().css('background-image','url(/images/checkbox-both-error2.png)');
                  //            $('input#gadgetentrantparentalconcent').parent().css('background-image','url(/images/checkbox-both-error2.png)');
                  $('input#gadgetentrantoverthirteen').parent().addClass('checkerror');
                  $('input#gadgetentrantparentalconcent').parent().addClass('checkerror');
                  $return = false;
              }
        }

        $('input#gadgetentrantoverthirteen').click(function(){
            $('.checkerror').removeClass('checkerror')
        })
        $('input#gadgetentrantparentalconcent').click(function(){
            $('.checkerror').removeClass('checkerror')
        })
        $('input#gadgettermsandconditions').click(function(){
            $(this).parent().removeClass('checkerror')
        })

        return $return;
    });
})
