﻿var _category = "";

$.preloadCssImages(); 
$.ajaxSetup({ cache: false });

$(document).ready( function() 
{
    
    ResizeContainers();

    $(window).unbind().resize( function() { ResizeContainers(); });
    $(document).unbind("ajaxStart");
    $(document).unbind("ajaxStop");
    $(document).ajaxStart( function() { $('#ajax-loader').delay(100).fadeIn("fast"); } );
    $(document).ajaxStop( function() { $('#ajax-loader').stop(true,true).delay(300).fadeOut("fast"); } );

    UpdateMenu( _view, _category );

    $('.scale-picture').scale( true, true, 0, 13 ); 
    $('.scale-cart').scale( true, true, 4, 0 ); 
    $( '#thumbnailContainer' ).css("visibility","visible").fadeTo( 1, 1.0 );

});

function ResizeContainers()
{
    ResizeColumnContainer('li.family','.thumb-container', 320 );
    ResizeColumnContainer('li.thumb','.thumb-container', 160 );
    ResizeColumnContainer('#cloud a','#cloud', 160, 960);
}

function getCookie( key ) 
{
    
    if( document.cookie.length > 0 )
    {
        index = document.cookie.indexOf( key + "=" );
        if( index != -1 )
        {
            index = index + key.length + 1;
            endIndex = document.cookie.indexOf( ";", index );
            if( endIndex == -1 ) endIndex = document.cookie.length;
            return( document.cookie.substring( index, endIndex ) );
        }
    }
    
    return( '' );
    
}  

function setCookie( key, value ) 
{
    var cookieDate = new Date();
    cookieDate.setDate( cookieDate.getDate() + 1000 );
    document.cookie = key + "=" + value  + ";expires=" + cookieDate.toGMTString() 
}

function ResizeColumnContainer( selector, container, itemWidth, max )
{                       
        
    var windowWidth = $(window).width() - itemWidth;

    $(container).each( function( index )
    {
        var items = $(this).children(selector).length;
        if( items != undefined && items > 0 )
        {
            var columns = windowWidth / itemWidth;
            if( items >= columns )
            {
                var newWidth = ( Math.round( windowWidth / itemWidth ) * itemWidth );
                $(this).width( newWidth + 'px' ).css('visibility','visible');
            }
        }
    });


}

function DeserializeResults( results )
{
    var deserialize = eval( "var result = " + results + ";" );
    return( result );
}

function OnError( results ) 
{ 
    alert( results ); 
} 
  
  
function RefreshPictureList( tag, category, family, showCart )
{
    var element = $( '#thumbnailContainer' );
    element.height( element.outerHeight() );
    $( '#thumbnailMask' ).css('opacity','0.0').css('visibility','visible').fadeTo( "slow", 1.0, function() 
    { 
        GetPictures( tag, category, family, showCart ); 
    });
}  
  
function RefreshTestimonialList()
{
    var element = $( '#thumbnailContainer' );
    element.height( element.outerHeight() );
    $( '#thumbnailMask' ).css('opacity',0.0).css('visibility','visible').fadeTo( 750, 1.0, function() 
    { 
        GetTestimonials(); 
    });
}  

function GetPictures( tag, category, family, showCart ) 
{

    var url = "thumbnails";
    
    if( showCart )
    {
        url = "gallery";
    }
    
    else
    {

        if( tag != null && tag != undefined && tag != "" )
        {
            url = "tag/" + tag;
        }
    
        else if( category != null && category != undefined && category != "" )
        {
            url = "category/" + category;
        }

        else if( family != null && family != undefined && family != "" )
        {
            url = "family/" + family;
        }

    }

    $.ajax
    ({
        url: url,
        error: OnError,
        type: "POST",
        cache: false,
        success: function( data ) { ReplacePictures( data, showCart ) } 
    });

}

function GetTestimonials() 
{

    $.ajax
    ({
        url: "../testimonials/",
        error: OnError,
        type: "POST",
        cache: false,
        success: function( data ) { ReplacePictures( data ) } 
    });

}

function ReplacePictures( data, showCart )
{

    $( '#thumbnailContainer' ).replaceWith( data );

    $( '#cloud' ).html( $( '.ajaxCloud' ).html() );
    $( '.ajaxCloud' ).remove();

    ResizeContainers();

    $( '#thumbnails' ).children().css( 'opacity', 0.0 );
    $( '#thumbnails' ).children().addClass( 'hidden' );

    $( '#thumbnailMask' ).fadeTo( "fast", 0.0 ).css('visibility','hidden');

    AttachSlider();
    ShowHiddenThumbnails();
    
}

function ShowHiddenThumbnails() 
{

    var thumbnail = $( '#thumbnails' ).children( ".hidden:first" );
    
    if( thumbnail != null && thumbnail.length > 0 )
    {
        thumbnail.removeClass( "hidden" );
        thumbnail.fadeTo( 100, 1.0, function(){ ShowHiddenThumbnails(); } );   
    }

}

function UpdateMenu( element )
{

    if( element == undefined ) return;
    
    $( ".menu" ).children().removeClass( "selected" );
    $( '#menu-' + element ).addClass( "selected" );   

}

function AddToCart( productId, skuId )
{

    $('.image-frame').fadeTo( "slow", 0.0 ); 
    $('.slide-img').fadeTo( "slow", 0.0 ); 
    $('#picture').fadeTo( "slow", 0.0, function() 
    { 
        $.ajax
        ({
            url: "/cart/" + productId + "/" + skuId,
            error: OnError,
            type: "POST",
            cache: false,
            success: function( data ) 
            { 
                ShowCart( data ) 
                $('.menuCart').removeClass( 'empty' );
            } 
        });
    });

}

function RemoveFromCart( element, productId, skuId, itemsRemaining )
{

    $(element).parent().parent().fadeOut('slow', function()
    {
        $('#cart-container').load
        (
            "/cart/remove/" + productId + "/" + skuId + " #cart-content", 
            null, 
            function() 
            { 
                if( itemsRemaining == 0 )
                {
                    $('.menuCart').addClass( 'empty' );
                }
                $('.scale-cart').scale( true, true, 4, 0 );
            } 
        );
    });

}

function CartChangeQuantity( element, productId, skuId )
{
    $('#cart-container').load
    (
        "/cart/quantity/" + productId + "/" + skuId + "/" + $(element).val() + " #cart-content", 
        null, 
        function() { $('.scale-cart').scale( true, true, 4, 0 ); } 
    );
}

function EmptyCart()
{

    $('.cart-table').children().fadeOut('slow', function()
    {
        $('#cart-container').load( "/cart/empty #cart-content");
    });

    $('.menuCart').addClass( 'empty' );

}

function Checkout()
{
    $('#atc-form').submit();
}

function SubmitPictureRequest()
{

    var form = $('#request-form');

    form.validate(
    {

        rules: { message: { required: true } },
        messages: { message: { required: "" } },

        submitHandler: function( form ) 
        {
            $.post( "/request", { message: $('#message').val() } );
            alert( "Thank you for your suggestion!\n\nWhy not come back in a little while and check out my latest designs." );
            $('#message').val('');
        }
           
    });

    if( form.valid() ) form.submit();

}

function SubmitContact()
{

    var form = $('#contact-form');

    form.validate(
    {

        rules: 
        {
            name: { required: true },
            message: { required: true },
            email: { required: true, email: true }
        },

        messages: 
        {
            name: { required: "Please tell me your name" },
            message: { required: "Please tell me why you are contacting me" },
            email: { required: "Please provide your email address", email: "You must provide a valid email address" }
        },

        submitHandler: function( form ) 
        {

            var name = $('#name').val();
            var email = $('#email').val();
            var message = $('#message').val();
    
            if( message.length == 0 ) return;

            $.post( "/contact-request", { name: name, email: email, message: message } );
    
            alert( "Thank you for contacting me.  I will get back to you shortly!" );
            $('#name,#email,#message').val('');

        }
           
    });

    if( form.valid() ) form.submit();

}

function TogglePickup()
{

    var pickup = $('#pickup').attr('checked');

    $('#cart-container').load
    (
        "/cart/pickup/" + pickup + " #cart-content", 
        null, 
        function() 
        { 
            if( pickup == true )
            {
                $(".pickup-container").css({ backgroundColor: '#d6d6d6' }).animate( { backgroundColor: '#b4dbd8' }, 1000 );
            }
            else
            {
                $(".pickup-container").css({ backgroundColor: '#b4dbd8' }).animate( { backgroundColor: '#d6d6d6' }, 1000 );
            }
            $('.scale-cart').scale( true, true, 4, 0 ); 
        } 
    );


}

