﻿var body_onLoad = "";

function AddBodyOnload(str) {
    body_onLoad += str + ";";
}

var browser_full_support = $.browser.webkit;

$(document).ready(function() {

    // page fade //
    $("body").css("display", "none");
    $("body").fadeIn(500);
    $("a.transition").click(function(event) {
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(500, redirectPage);
    });
    function redirectPage() {
        window.location = linkLocation;
    }

    // setup the cart object //
    setup_cart();

    $(".nicetable tr:odd").addClass("alt");

    if (body_onLoad != "")
        eval(body_onLoad);

    $('.slideshow').cycle({
        fx: 'fade'
    });

    // animated bouncing header //
    $("#pagetitle").animate(
        { top: "-20px" }, 250, "linear"
        , function() {
            $('#pagetitle').effect("bounce", { times: 2 }, 200);
        }
    );

    // bounce categories //
    $(".category-icons a").hover(
        function() {
            $(this).effect("bounce", { times: 1, distance: 20 }, 550);
        }
    );

    // tiny tips //
    $(".tinytip").tinyTips('title');

    // h1 and h2 adjustments //
    $(this).find('h2, h1').each(function() {
        ct = $(this).html(); if (ct != null && ct.length > 5) $(this).html('<strong>' + ct.substring(0, 1) + '</strong>' + ct.substring(1, ct.length));
    });

    // setup any galleries //
    $('#gallery a').lightBox();
//    
//    $('#gallery a').each(function() {
//        $(this).click(function() {
//            //alert('clicked ' + $(this).attr('href'));
//            $("#galleryfader").fadeIn();
//            $("#galleryholder").html("<img src=\"" + $(this).attr('href') + "\">");
//            alert($("#galleryholder").width());
//            return false;
//        });
//    });

//    $("#galleryfader").click(function() {
//        $("#galleryfader").fadeOut();
//        $("#galleryholder").fadeOut();        
//    });

//    $("body").append("<div id=\"galleryfader\">fader</div>");
//    $("body").append("<div id=\"galleryholder\"></div>");

    // alert //
    $("#alertbox").animate(
        { top: "120px" }, 180, "linear"
        , function() {
            $('#alertbox').effect("bounce", { times: 1 }, 150);
        }
    );
    $("#alertbox").click(function() {
        $("#alertbox").fadeOut("fast");
    });

    $('.altrow:odd').addClass('altbackground');

    // animated background //
    $('#balls').pan({ fps: 15, speed: 0.7, dir: 'left', depth: 1 });
    $('#balls2').pan({ fps: 15, speed: 1, dir: 'right', depth: 2 });
    $('#balls3').pan({ fps: 15, speed: 0.4, dir: 'right', depth: 3 });


    $(".hovershade").each(function() {
        $(this).hover(function() {
            // open this //
            $(this).find(".shade").animate({ opacity: 0.1 }, { queue: false, duration: 200 });
        }, function() {
            $(this).find(".shade").animate({ opacity: 1 }, { queue: false, duration: 200 });
        });
    });

    if (!$.browser.msie) {
        $(".fadesimilar").each(function() {
            $(this).hover(function() {
                $(this).find(".shade").animate({ opacity: 0.1 }, { queue: false, duration: 200 });
            }, function() {
                $(this).find(".shade").animate({ opacity: 1 }, { queue: false, duration: 200 });
            });
        });
    }


    $(".addtocart").each(function() {
        $(this).click(function(evt) {
            $("#livecart").show();
            $("#livecart").offset({
                left: evt.pageX,
                top: evt.pageY
            });
            $("#livecart").animate({
                top: '+=' + 500
            }, 1500, function() {
                $("#livecart").hide();
            });
        });
    });

});

function gotoURL(url) {
    window.location = url;
}
function isEnter(evt) {
    return (window.event) ? (window.event.keyCode == 13) : (evt.keyCode == 13);
}

var cart_state = "closed";
function position_cart() {
    topos = ($.browser.msie) ? "90" : "-100"
    var position_top = (cart_state == "opened") ? "-20" : ($(window).height() - 50);
    //if ($(this).scrollTop() > 50 && $('#cart').css('position') != 'fixed') {
    $('#cart').css({ 'position': 'fixed', 'top': position_top });
    $("#bgtransparency").css({ 'position': 'fixed', 'top': '0' });
    //}
}

function setup_cart() {
    $(window).scroll(function(e) {
        position_cart();
    });
    $(window).resize(function(e) {
        position_cart();
    });
}

var cart_pad_open = -20;
function opencart() {
    if (cart_state != "closed")
        return;

    cart_state = "opening";
    $("#cart .summary").hide();

    $("#bgtransparency").fadeIn('fast');

    $("#cart").animate({
        top: '-=' + ($(window).height() -70)
    }, 200, function() {
        cart_state = "opened";
    });
}

function closecart() {
    if (cart_state != "opened")
        return;
        
    cart_state = "closing";
    $("#cart .summary").show();

    $("#bgtransparency").fadeOut('fast');
    
    $("#cart").animate({
        top: '+=' + ($(window).height() - 50)
    }, 200, function() {
        position_cart();
        cart_state = "closed";
    });
}


$.extend({ URLEncode: function(c) {
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length) {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } else {
            if (c[x] == ' ') o += '+'; else {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();

            } x++;
        } 
    } return o;
},
    URLDecode: function(s) {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});



/*** image cycler ***/

var cycle_target = "";
var cycle_position = 0;
var cycle_images = new Array();
var cycle_started = false;
function cycleimages(t, img) {
    cycle_position = 0;
    cycle_images = img.split(',');
    cycle_target = t;
    cyclenow();
    if (!cycle_started) {
        setInterval('cyclenow()', 1000);
        cycle_started = true;
    }
}
function cyclenow() {
    if (cycle_target == "" || cycle_images == "")
        return;
    $("#" + cycle_target).attr("src", cycle_images[cycle_position]);
    cycle_position++;
    if (cycle_position >= cycle_images.length)
        cycle_position = 0;
	return;
}
function cancelcycle(t) {
    cycle_images = "";
    cycle_target = "";
    cycle_position = 0;
}

function galleryhover() {
    if (browser_full_support) {
        $(".item-list .item").each(function() {
            $(this).hover(function() {
                // open this //
                $(this).find(".shade").animate({ opacity: 0.1 }, { queue: false, duration: 200 });
            }, function() {
                $(this).find(".shade").animate({ opacity: 1 }, { queue: false, duration: 200 });
            });
        });
    }
}


function loadme(t) {
	$(t).html("<img src='/images/ajax-loader.gif'>");
}

