﻿$(function () {
    $('div.swf').each(
		function (index) {
		    var w = $(this).width();
		    var h = $(this).height();
		    var flashvars = {};
		    var params = { wmode: "transparent" };
		    var attributes = {};
		    attributes.id = $(this).attr('title');
		    swfobject.embedSWF($(this).attr('title') + '.swf', $(this).attr('id'), w, h, '9', false, flashvars, params, attributes);
		});

    $('div.youtube').each(
		function (index) {
		    var flashvars = {};
		    var params = {};
		    params.allowScriptAccess = 'Always';
		    params.allowFullScreen = 'True';
		    params.movie = 'http://www.youtube.com/v/' + $(this).attr('title')
		    var attributes = {};
		    attributes.id = $(this).attr('title');
		    swfobject.embedSWF('http://www.youtube.com/v/' + $(this).attr('title'), $(this).attr('id'), '300', '250', '8', false, flashvars, params, attributes);
		});
});

function set_marquee(className) {
    $('div.' + className).marquee('pointer').mouseover(function () {
        $(this).trigger('stop');
    }).mouseout(function () {
        $(this).trigger('start');
    }).mousemove(function (event) {
        if ($(this).data('drag') == true) {
            this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
        }
    }).mousedown(function (event) {
        $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
    }).mouseup(function () {
        $(this).data('drag', false);
    });
}


function select_pac(num, id) {
    $('#p-' + id + ' div').each(function (i) {
        var el = $(this);
        if (el.attr('id') == 'Pack' + num) {
            el.children('.pac-img-on').show();

            if (id == 'box') {
                $('#PackageID').val(num);
                $('#image-preview').empty();
                var img = el.children('.prod-pic').clone();
                if (img.is('*')) {
                    img.appendTo('#image-preview');
                    img.show();
                }
                GetChildPackages();
            }
            else {
                $('#ChildPackID').val(num);
                CalculateCart();
            }
        }
        else {
            el.children('.pac-img-on').hide();
        }
    });
}

function SetCart(obj) {
    $(obj).hide();
    $.post('/shoppingcart/addnew', $('#ProductForm').serialize(), function (val) {
        window.location = "/shoppingcart";
    });
}

function GetChildPackages() {
    var PackID = $('#PackageID').val();
    $.get('/home/getchildpackages?PackID=' + PackID, function (data) {
        var dv = $('#p-pac')
        dv.html(data);
        dv.find('.pac-img-on:first').show();
        dv.find('.pac-item:first').click();
        CalculateCart();
    });
}

function CalculateCart() {
    if ($('#PackageID').val() == 18) {
        SetProdQuantity(3000);
    }
    $.get('/home/cart', $('#ProductForm').serialize(), function (data) {
        $('#cart-place').html(data);
        $('.cart-row').fadeIn('slow');
    });
}

function SetProdQuantity(MinVal) {
    var num = $("#PriceID option:selected").text();
    var quan = FloatToInt(num);

    if (quan < MinVal) {
        $('#PriceID > option').each(function () {
            var currVal = FloatToInt($(this).text());
            if (currVal == MinVal)
                this.selected = true;
        });
    }
}

function FloatToInt(val) {
    return parseInt(val.replace(",", ""));
}

function ShowDetails() {
    var id = $('#ProductID').val();
    popUp("/home/details?pID=" + id, 640, 500);
}

function clear_form(id) {
    $(':input', '#' + id)
     .not(':button, :submit, :reset, :hidden')
     .val('')
     .removeAttr('checked')
     .removeAttr('selected');
}

function popUp(URL, w, h) {
    var left = (screen.width / 2) - (w / 2);
    var top = (screen.height / 2) - (h / 2);

    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('" + URL + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h + ",left=" + left + ",top=" + top + "');");
}
