$(document).ready(function(){

    $('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).live('mouseover', function(){ 
            this.src = this.src.substring(0, this.src.length - 5) + "2.png"; 
        });

        // Mouseout
        $(this).live('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"; 
            }
        });
    });
    
    // Manual switch
    $('li.switch a').click(function(){
        if ($(this).hasClass('home')) {
            $.cookie('site', 1, { expires: 365, path: '/'});
        } else if ($(this).hasClass('business')) {
            $.cookie('site', 0, { expires: 365, path: '/'});
        }
    });

    // Switch buttons
    $('#switch .left').live('click', function() {
        $.colorbox.close();
        $.cookie('site', 0, { expires: 365, path: '/'});
        window.location.replace('http://1026262120.3087932.test.prositehosting.co.uk/');
    });
    
    $('#switch .right').live('click', function() {
        $.colorbox.close();
        $.cookie('site', 1, { expires: 365, path: '/'});
        window.location.replace('http://1026262120.3087932.test.prositehosting.co.uk/home');
    });

    // Show overlay
    if ($.cookie('cookie-support') == '1' && $('#index').length > 0 && $.cookie('site') == null) {
        $.colorbox({html: '<div id="switch"><img src="/wp-content/themes/cloudfree/images/common/cloudfree-it.png" alt="Cloudfree IT"><p class="intro">This is the first time you have visited our website, would you like to view our <strong>business</strong> or <strong>home</strong> website?</p><input type="image" class="left" src="/wp-content/themes/cloudfree/images/common/switch-business-1.png" value="Business Customer" /><input class="right" type="image" src="/wp-content/themes/cloudfree/images/common/switch-home-1.png" value="Home Customer" /></div>', width: 600, height: 410});
    }

    function doMap(lat, lng, title, content) {
        var mapCenter = new google.maps.LatLng(lat,lng);
        
        var infowindow = new google.maps.InfoWindow( { content: '', size: new google.maps.Size(400,200)});

        var mapOptions = {zoom: 14,
                          navigationControl: true,
                          mapTypeControl: true,
                          mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.HORIZONTAL},
                          scaleControl: true,
                          navigationControl: true,
                          navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                          center: mapCenter,
                          mapTypeId: google.maps.MapTypeId.ROADMAP};

        var map = new google.maps.Map(document.getElementById('map'), mapOptions);

        var marker = new google.maps.Marker({position: mapCenter,
                                             map: map,
                                             title: title});
                                             
        var infoWindow1 = new google.maps.InfoWindow({ content: content });
        var marker1 = new google.maps.Marker({
            position: mapCenter, 
            map: map, 
            title:"Cloudfree IT"
        });
        google.maps.event.addListener(marker1, 'click', function() {
            infoWindow1.open(map,marker1);
        });
    }

    if ($('#map').length > 0) {
        doMap(53.8594566, -1.9103204, 'Cloudfree IT', '');
    }

});
