/* 
  This file is licensed for use by Bulldog Martial Arts for use on one site: www.bulldogmartialarts.com 
  Copyright © Julian Nicholls 2008-2010, All Rights Reserved. 
*/ 

//--------------------------------------------------------------------------
// $URL: file:///svn/repos/Bulldog/shopsite.js $
// $Rev: 404 $
// $Date: 2009-03-04 16:57:01 +0000 (Wed, 04 Mar 2009) $
//--------------------------------------------------------------------------

$(document).ready(function() {
    objSlide  = document.getElementById('prodslide');
    
    $("#blurb,div#trolley").corner();
    
    $("#bbimg").hover( function() {
        $(this).attr( "src", "images/bulldogbutton.jpg" );
    }, function() {
        $(this).attr( "src", "images/bb_emboss.jpg" );
    });        

    $("#timg").hover( function() {
        $(this).attr( "src", "images/ticket2.jpg" );
    }, function() {
        $(this).attr( "src", "images/t_emboss.jpg" );
    });        
    
    $("#trolleydiv").load( "/filltrolley.php" );

    $("#showblog").click( function() {
        $("#middle").append( '<div id="blogwait"><img src="images/ajax-loader.gif" alt="Wait" /> Loading...</div>' );
    });
    
    $("a.faded").hover( function() {
        var $img = $(this).find( "img" );
        var src  = $img.attr( "src" ).replace( /fade/, "" );
        
        $img.attr( "src", src );
    }, function() {
        var $img = $(this).find( "img" );
        var src  = $img.attr( "src" ).replace( /\./, "fade." );
        
        $img.attr( "src", src );
    });
    
    setupInfo();
});


//--------------------------------------------------------------------------
// setupInfo
//
// Set up for displaying the three info panels.
//
//--------------------------------------------------------------------------

function setupInfo()
{
    $(".openinfo").click( function() {
        $("body").append('<div id="infoframe"><div id="infoclose"><a href="#" id="btnclose"><img src="images/close.png" /></a></div><div id="infobox"></div></div>');

        var header = "Terms &amp; Conditions";
        
        switch( this.id )
        {
            case "opentc":
                $("#infobox").css( "overflow", "scroll" );
                break;

            case "openprod":
                header = "Product Information";
                break;

            case "opencont":
                header = "Contact Information";
                break;
        }
                
        $("#infoclose").append( header );

        $("#infobox").load( $(this).attr( "href" ) );
        
        $("#infoframe")
            .css( "left", (findLivePageWidth() - 830) / 2 )
            .fadeIn( 400 );

        $("#btnclose").click( function() {
            $("#infoframe").fadeOut( 400, function() {
                $(this).remove();
            });
        });

        return false;
    });
}


//--------------------------------------------------------------------------
// showBrand
//
// Show a brand's products
//
//--------------------------------------------------------------------------

function showBrand( value )
{
    window.location = '/index.php?brid=' + value;
}


//--------------------------------------------------------------------------
// setMiddle
//
// Fill in the background image for a category.
//
//--------------------------------------------------------------------------

function setMiddle( midurl )
{
    $("#middle").css( "background", 'url( images/' + midurl + ') no-repeat' );
}


//--------------------------------------------------------------------------
// findLivePageWidth
//
// Return the inside dimensions of the browser window.
//
//--------------------------------------------------------------------------

function findLivePageWidth() 
{
    if( window.innerWidth )
        return window.innerWidth;

    if( document.body.clientWidth )
        return document.body.clientWidth;

    return  null;
}


//--------------------------------------------------------------------------
// showSlide
//
// Show a product slide, creating the photo div in the process.
//
// Entry:   evt     URL to product picture
//          id      Product ID
//
//--------------------------------------------------------------------------

function showSlide( evt, id )
{
    $.post( 'showproduct.php', { sendData: id }, fillSlide );
}


//--------------------------------------------------------------------------
// fillSlide
//
// Callback function from fetchdata that fills the product slide.
//
//--------------------------------------------------------------------------

function fillSlide( data )
{
    var ps = $("#prodslide");
    
    ps.html( data ).width( Math.max( $("#largePhoto").width() + 200, 550 ) );

    var livePageWidth   = findLivePageWidth();
    var newLeft         = ((livePageWidth/2)-8) - (ps.width()/2);
    
    ps.css( "left", newLeft ).show("slow" ).corner();
    
    $(".prodoption").change( function() {
        var inc      = 0.0,     // Price increment
            disabled = false;   // Enable / disable of add to basket button
        
        $("select.prodoption option:selected").each( function() {
            var val = $(this).val();
            var idx = val.indexOf( "#" );
    
            if( idx > 0 )
                inc += parseFloat( val.substr( idx + 1 ) );
                
            if( val == 'Select' )   // Unselected
                disabled = true;
        });
        
        var tot = parseFloat( $("#prodadd #price").val() ) + inc;

        $("#prodadd .price").html( "Price: &pound;" + tot.toFixed( 2 ) );
        
        if( disabled )
            $("#BasketAdd").attr( "disabled", "disabled" );
        else            
            $("#BasketAdd").removeAttr( "disabled" );
    });
    
    $("div.swatch").click( function() {
        $("div.swatch").css( "border-style", "outset" );    // Unpress others
        $(this).css( "border-style", "inset" );             // Press this one
        
        $(".optcolour option:selected").attr( "selected", "false" );
        $(".optcolour option:eq(" + (parseInt( this.id.substr(9), 10) + 1) + ")").attr( "selected", "true" );
        $(".optcolour").change();
    });

    $("a.athumb").click( function() {
        var img = $(this).find( "img" ).attr( "src" );
        
        if( $("#largePhoto").attr( "src" ) != img )
        {
            $("#largePhoto").animate( {opacity: 0}, 600, "linear", function() {
                $("#largePhoto").css( 'opacity', 1 ).attr( "src", img );    
            });
        }
        
        return  false;
    });
}


//--------------------------------------------------------------------------
// addToBasket
//
// Add a product to the basket and highlight the fact
//
//--------------------------------------------------------------------------

var count = 6;

function addToBasket()
{
    $.post( "/filltrolley.php", $("#prodadd").serialize(), function( data ) {
        $("#trolleydiv").html( data );

        count = 6;
        startHighlight();
    }, "html" );

    hideSlide();
}


//--------------------------------------------------------------------------
// startHighlight
//
// Highlight the latest item added to the trolley by flashing a reddish 
// background three times on it.
//
//--------------------------------------------------------------------------

function startHighlight()
{
    --count;
    
    if( count & 1 )
        $("#latest").addClass( "pinken" );
    else
        $("#latest").removeClass( "pinken" );
        
    if( count )
        setTimeout( "startHighlight()", 330 );
}


//--------------------------------------------------------------------------
// hideSlide
//
// Close the product slide.
//
//--------------------------------------------------------------------------

function hideSlide()
{
    $("#prodslide").fadeOut( 1000 );
}


//--------------------------------------------------------------------------
// doUpdate
//
// Update the trolley based on the entered quantities
//
//--------------------------------------------------------------------------

function doUpdate( func )
{
    var r = "/updatetrolley.php?u=1";

    $( '#cartform input' ).each(function(){
        var n = $(this).attr( 'name' );
        
        if( n == "quants[]" )
            r += "&" + n + "=" + $(this).val();
    });
    
    window.location = r;
}
