$(document).ready(function(){

    $(document).ready(function(){
        //$("a[class='issuu']").prettyPhoto({social_tools: '', allow_resize: false, default_width: 950, default_height: 625});
        $("a[class='issuu'], a[rel='issuu']").colorbox({innerWidth: 950, innerHeight: 625});
    });
	
    $(document).ready(function(){
        //$("a[class='issuu']").prettyPhoto({social_tools: '', allow_resize: false, default_width: 950, default_height: 625});
        $("a[class='colorbox']").colorbox();
    });

    $('a[rel=external]').click(function(){ 
        window.open($(this).attr('href')); return false;
    });

    $('a[rel=popup]').click(function(){
        window.open($(this).attr('href'), 'popUp', 'width=300,height=200,status=0,toolbar=0'); return false;
    });

    $('input[type=image], img.hover').each( function(){ 
        // Preload
        var x = new Image();
        x.src = this.src.substring(0, this.src.length - 5) + "2.png";

        // Mouseover
        $(this).mouseover( function(){ 
            this.src = this.src.substring(0, this.src.length - 5) + "2.png"; 
        });

        // Mouseout
        $(this).mouseout( function(){
            if (this.src.substring( (this.src.length - 5) , (this.src.length) ) == "2.png" ) { 
                this.src = this.src.substring(0, this.src.length - 5) + "1.png"; 
            }
        });
        
    });
    
    // Descriptions
    $('#package .book, #series .book').each(function(){
        $(this).children('.full-description').click(function(){
            $(this).parent().children('.description').slideToggle(250);
            if ($(this).children('img').attr('alt') == 'Hide Description') {
                $(this).children('img').attr('alt', 'Show Description');
                $(this).children('img').attr('src', '/images/common/button-show-description-1.png');
            } else {
                $(this).children('img').attr('src', '/images/common/button-hide-description-1.png');
                $(this).children('img').attr('alt', 'Hide Description');
            }
        });
    });
    
    // Search
    $('#keyword').example(function() { return $(this).attr('title')});
    
    // Mailing
    $('.mailing-list input.text').example(function() { return $(this).attr('title')});

    // Site Switch
    $('#site-switch').click(function(){
        if ($('body').hasClass('home')) {
            switchSchool(false);
        } else {
            switchHome(false);
        }
    });
    
    function insert_h() {
        // Insert H
        $(".scode").each(function() {
            if ($(this).val().substring(0,1) == 'H') {
                return true;
            }
            
            $(this).val('H' + $(this).val())
            });
        $(".order-code").each(function() {
            $(this).text('H' + $(this).text())
            });
    }
    
    function remove_h() {
        // Insert H
        $(".scode").each(function() {
            if ($(this).val().substring(0,1) == 'H') {
                $(this).val($(this).val().substring(1));
            }
            else {
                return true;
            }
            });
        $(".order-code").each(function() {
            if ($(this).text().substring(0,1) == 'H') {
                $(this).text($(this).text().substring(1));
            }
            else {
                return true;
            }
            });
    }
    
    // Deal with order codes
    if ($('body').hasClass('home'))
    {
        insert_h();
    }
    else {
        remove_h();
    }
    
    function switchSchool(override) {
        if ($('body.home-only').length > 0 && $.cookie('sites') == 1 && override != true) {
            showOverlay('home', false);
        } else {
            $('body').addClass('school');
            $('body').removeClass('home');
            $('.school-based').show();
            $('.home-based').hide();
            $.cookie('sites', 2, { expires: 365, path: '/'});
            remove_h();
        }
    }
    
    function switchHome(override) {
        if ($('body.school-only').length > 0 && $.cookie('sites') == 2 && override != true) {
            showOverlay('school', false);
        } else {
            $('body').addClass('home');
            $('body').removeClass('school');
            $('.school-based').hide();
            $('.home-based').show();
            $.cookie('sites', 1, { expires: 365, path: '/'});
            insert_h();
        }
    }

    function firstLoad() {
        if (typeof $.cookie('sites') == 'object') {
            switchHome();
            $.colorbox({html: '<div id="switch" class="first"><h3>Royal Fireworks Press</h3><p class="intro">This is the first time you have visited rfwp.com. Please choose a version of our website which best suits your requirements.</p><div id="button-home">Home-based</div><div id="button-school">School-based</div><div class="clear"></div><p class="small">You will not be asked this question again. You will be able to change website easily using the buttons on the left column if you change your mind.</strong></div>', width: 600, height: 460});
            $('#button-home').live('click', function(){
                switchHome();
                $.colorbox.close();
            });
            $('#button-school').live('click', function(){
                switchSchool();
                $.colorbox.close();
            });
        }
    }
    
    function showOverlay(site, auto) {
        var alt = '';
        (site == 'school') ? alt = 'home' : alt = 'school';
        
        if (auto == true) {
            if (window.history.length > 1) {
                var backButton = 'go back';
                var backButtonImg = 'back';
            } else {
                var backButton = 'return to the ' + alt + '-based website';
                var backButtonImg = 'return-' + alt;
            }
            //$.colorbox({html: '<div id="switch"><h3 style="text-align: center">This page is only available on our <strong>' + site + '-based</strong> website</h3><br /><p style="text-align: center"><button class="switch">Switch to our ' + site + '-based website</button> or <button class="back">' + backButton + '</button></p></div>', width: 700, height: 500});
            $.colorbox({html: '<div id="switch" class="toggle"><h3 class="' + site + '">This page is only available on our ' + site + '-based website</h3><p class="intro">The page you are trying to view is only available on our ' + site + '-based website. You may switch to our ' + site + '-based website or ' + backButton + '.</p><input type="image" class="hover switch" src="/images/common/switch-switch-' + site + '-1.png" value="Switch to our ' + site + ' based website" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="image" class="hover back" src="/images/common/switch-' + backButtonImg + '-1.png" value="' + backButton + '" /></div>', width: 690, height: 350});
        } else {
            $.colorbox({html: '<div id="switch" class="toggle"><h3 class="' + site + '">This page is only available on our ' + site + '-based website</h3><p class="intro">The page you are trying to view is only available on our ' + site + '-based website. You may visit our our ' + alt + '-based home page or stay on this page.</p><input type="image" class="hover redirect" src="/images/common/switch-' + alt + '-based-1.png" value="Switch to our ' + alt + ' based website" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="image" class="hover stay" src="/images/common/switch-stay-1.png" value="Stay on this page" /></div>', width: 690, height: 350});
        }
    }
    
    $('#switch .continue').live('click', function(){
        if ($.cookie('sites') == 2) {
            switchHome(true);
            window.location.replace("http://rfwp.com/");
        } else {
            switchSchool(true);
            window.location.replace("http://rfwp.com/");
        }
    });
 
    // Switch
    $('#switch .switch').live('click', function(){
        ($.cookie('sites') == 2) ? switchHome(true) : switchSchool(true);
        $.colorbox.close();
    });

    // Back
    $('#switch .back').live('click', function(){
        if (window.history.length > 1) {
            window.history.go(-1);
        } else {
            window.location.replace('http://rfwp.com/');
        }
    });

    // Stay
    $('#switch .stay').live('click', function(){
        $.colorbox.close();
    });

    // Redirect & switch
    $('#switch .redirect').live('click',  function() {
        ($.cookie('sites') == 2) ? switchHome(true) : switchSchool(true);
        window.location.replace('http://rfwp.com/');
    });

    if ($('body.school-only').length > 0 && $.cookie('sites') == 1) {
        showOverlay('school', true);
    } else if ($('body.home-only').length > 0 && $.cookie('sites') == 2) {
        showOverlay('home', true);
    }

    if ($.cookie('rfwp-test') == 1) {
        firstLoad();
    } else {
        $('#site-switch').hide();
    }
});
