/**
 * Copyright(c) 2008-2009,gamegoldfast.com Inc. Some rights reserved.
 */
$(document).ready(function(){

    // show quick buy gold pacakge form
    function quick_gold_form(productid, pacakge, server, price){
        $.ajax({
            url: "../add_cart.php?productid=" + productid + "&pacakge=" + pacakge + "&price=" + price + "&server=" + server,
            type: 'GET',
            dataType: 'html',
            timeout: 20000,
            
            error: function(){
                alert('Failed to connect to server');
            },
            success: function(msg){
                $.blockUI({
                    fadeIn: 0,
                    message: $('#login').html(msg)
                });
            }
        });        
    }
    
    // get category quick buy gold full list
    function get_gold_package(){
        var serverid = $('#sel_gold_server').val();
        var jspath = 'js/' + serverid + '_gold_service_package.js?t=' + new Date().getTime() + '.js';
        $.getJSON(jspath, function(data){
            // empty gold list's tag
            $('.selected_gold_list').empty();
            
            // loaded gold list layout
            var html = '<table cellpadding="0" cellspacing="0" id="quick_gold_tbl">';
            html += '<tr><th>Package</th><th>Price(USD)</th><th>Add to cart</th><th>Buy now</th></tr>';
            $.each(data, function(entryIndex, entry){
                if (entry['server_id'] == serverid) {
                    html += '<tr>';
                    html += '<td class="package">' + entry['package'] + '</td>';
                    html += '<td class="price">' + entry['price'] + '</td><td>';
                    html +='<span id="'+entry['productid']+'" class="g_cart">Add to cart</span></td>';
                    html += '<td><span id="'+ entry['productid'] +'" class="g_buy">';
                    html +='<img src="../images/check.jpg" /></span></td>';
                    html += '</tr>';
                }
            });
            html += '</table>';
            $('.selected_gold_list').append(html);
            
            // show add to cart form layout
            $('#quick_gold_tbl .g_cart').each(function(){
                $(this).click(function(){
                    server = $('#sel_gold_server option:selected').text();
                    price = $(this).parents().find('.price').html();
                    package = $(this).parents().find('.package').html();
                    productid = $(this).attr('id');
                    // show add to cart form
                    quick_gold_form(productid, package, server, price);
                });
            });
            
            // checkout quick buy
            $('#quick_gold_tbl .g_buy').each(function(){
            	$(this).click(function(){
                   	serverid = $('#sel_gold_server').val();
                    productid = $(this).attr('id'); 
                    //url ="../add_cart_quick.php?productid="+productid+"&serid="+serverid+"&date=t";  
                    url ="../add_cart_quick.php?productid="+productid+"&serid="+serverid+"&date=t";               
                    window.location.href = url;         		
            	});            	
            });            
        });
    };
    
    // loaded gold list by default open page
    get_gold_package();
    
    // load gold full list by selected different server
    $('#sel_gold_server').change(function(){
        get_gold_package();
    });
})
