﻿/*
 * Setting needed variables
 */

var blog = new Array();
var blog_current = new Array();
var product_current = '';
var category_current = '';
var brand_current = '';
var cart = {}

var pic_width = 535;
var new_pos;

/*
 * /settings
 */

function getURL(url) {
    location.href = url;
}

function loadBlogPic(b, p) {
    new_pos = -1 * p * pic_width;
    $('#blog_picture_holder_'+ b).animate({left: new_pos}, 1000, function() {
        $('#blog_link_'+ b +'_'+ blog_current[b]).css('color', '');
        blog_current[b] = p;
        $('#blog_link_'+ b +'_'+ blog_current[b]).css('color', '#000000');
    });
}

function loadBlogPicDir(b, dir) {
    new_pic = 0;
    if(blog_current[b] + dir < 0) new_pic = blog[b].length-1;
    else if(blog_current[b] + dir > blog[b].length-1) new_pic = 0;
    else new_pic = blog_current[b] + dir;
    loadBlogPic(b, new_pic);
    return;
}

function swapBrandPicture(brand, state) {
    if(brands[brand].selected) return;
    $("#brand_picture_"+ brand).attr('src', '/picture.php?id='+ (state ? brands[brand].picture : brands[brand].picture_mouseover));
}

/*
 *  set categories selection + brand selection defaults
 */

var products = {};
var categories = {};
var brands = {}

products.list = [];
categories.list = [];
brands.list = [];

brands.select = function(brand) {
    brand_current = brand;
    window.location.hash = 'brand_'+ brand;
    unselectBrandsAndCategories();
    swapBrandPicture(brand, false);
    brands[brand]['selected'] = true;
    //this[brand]['selected'] = this[brand]['selected'] ? false : true;
    shopShowProducts();
}

categories.select = function(category) {
    category_current = category;
    window.location.hash = 'category_'+ category;
    unselectBrandsAndCategories();
    categories[category]['selected'] = true;
    //this[category]['selected'] = (this[category]['selected'] ? false : true);
    shopShowProducts();
}

function showPromo() {
    closeProduct();
    unselectBrandsAndCategories();
    var i;
    for(i in products.list) {
        if(products[products.list[i]]['is_promo']) {
            $('#product_'+ products[products.list[i]]['product']).slideDown(300);
        } else {
            $('#product_'+ products[products.list[i]]['product']).slideUp(300);
        }
    }
}

function unselectBrandsAndCategories() {
    for(i in brands.list) {
        if(brands[brands.list[i]].count == 0) {
            $('#brand_picture_'+ brands.list[i]).hide();
            continue;
        }
        brands[brands.list[i]]['selected'] = false;
        swapBrandPicture(brands.list[i], true);
    }
    for(i in categories.list) {
        if(categories[categories.list[i]].count == 0) {
            $('#category_name_'+ categories.list[i]).hide();
            //$('#category_'+ categories.list[i]).hide();
            continue;
        }
        categories[categories.list[i]]['selected'] = false;
    }

    closeProduct();
    $("#shop_product_list").slideDown();
}

function initLazyLoad() {
      $(function() { $("img.lazy").lazyload({ event: "scrollstop" }); });
}

function shopShowProducts(show_all) {
    closeProduct();
    var i;
    for(i in categories.list) {
        $('#category_'+ categories.list[i]).hide();
        $("#category_name_"+ categories.list[i]).css('color', (categories[categories.list[i]]['selected'] ? '#000000' : ''));
    }

    for(i in products.list) {
        if(show_all || brands[products[products.list[i]]['brand']].selected ||
                 categories[products[products.list[i]]['category']].selected) {
            //$('#product_'+ products[products.list[i]]['product']).slideDown(0);
            $('#product_'+ products[products.list[i]]['product']).css('display', '');
        } else {
            //$('#product_'+ products[products.list[i]]['product']).slideUp(0);
            $('#product_'+ products[products.list[i]]['product']).css('display', 'none');
        }
    }
}

function openProduct(product) {
    //window.location.hash = 'product_'+ product;
    $(".banners").css('height', '320px');
    if(products[product]['info'] != null) {
        fillProduct(product);
        return;
    }
    $.get("/load.php", {'load_product': product, 'set_lang': current_lang}, function(data) {
        products[product]['info'] = data;
        fillProduct(product);
    }, "json");
}

function toggleColourChoice() {
    for(i=0; i<10; i++) {
        if(products[product_current].info.optional_colours[i] != null) {
            $("#product_colour_"+ i).html(products[product_current].info.optional_colours[i].colour);
            $("#product_colour_"+ i).show();
        } else {
            $("#product_colour_"+ i).hide();
        }
    }
    $("#dropdown_colour").show();
}

function goBackToCategory() {
    categories.select(category_current);
}

function selectSize(number) {
    openProduct(products[product_current].info.optional_sizes[number].product);
}

function selectColour(number) {
    openProduct(products[product_current].info.optional_colours[number].product);
}

function toggleSizeChoice() {
    for(i=0; i<10; i++) {
        if(products[product_current].info.optional_sizes[i] != null) {
            $("#product_size_"+ i).html(products[product_current].info.optional_sizes[i].size);
            $("#product_size_"+ i).show();
        } else {
            $("#product_size_"+ i).hide();
        }
    }
    $("#dropdown_size").show();
}

function switchSmallPicture(number) {
    for(i=1; i<5; i++) {
        $(".shop_product_picture_small #picture_small_"+ i).removeClass('active');
    }
    $("#shop_product_picture #picture_main").attr('src', '/picture.php?width=400&height=473&id='+ products[product_current].info['picture_'+ number]);
    $(".shop_product_picture_small #picture_small_"+ number).addClass('active');
}

function fillProduct(product) {
    product_current = product;
    $("#shop_product_list").css('display', 'none');
    //$("#shop_product_list").slideUp();
    //$("#shop_product").slideUp();
    //$(".suitable_products").slideUp();
    // hide dropdown boxes
    $("#dropdown_colour").hide();
    $("#dropdown_size").hide();
    //hide empty pictures
    if(products[product].info.picture_1 == '') $(".shop_product_picture_small #picture_small_1").hide();
    else $(".shop_product_picture_small #picture_small_1").show();
    if(products[product].info.picture_2 == '') $(".shop_product_picture_small #picture_small_2").hide();
    else $(".shop_product_picture_small #picture_small_2").show();
    if(products[product].info.picture_3 == '') $(".shop_product_picture_small #picture_small_3").hide();
    else $(".shop_product_picture_small #picture_small_3").show();
    if(products[product].info.picture_4 == '') $(".shop_product_picture_small #picture_small_4").hide();
    else $(".shop_product_picture_small #picture_small_4").show();

    switchSmallPicture(1)
    $(".shop_product_picture_small #picture_small_1").attr('src', '/picture.php?fit=box&id='+ products[product].info.picture_1 +'&width=50&height=50');
    $(".shop_product_picture_small #picture_small_2").attr('src', '/picture.php?fit=box&id='+ products[product].info.picture_2 +'&width=50&height=50');
    $(".shop_product_picture_small #picture_small_3").attr('src', '/picture.php?fit=box&id='+ products[product].info.picture_3 +'&width=50&height=50');
    $(".shop_product_picture_small #picture_small_4").attr('src', '/picture.php?fit=box&id='+ products[product].info.picture_4 +'&width=50&height=50');

    $("#shop_product_price").html(products[product].info.price +" "+ products[product].info.currency);
    $("#shop_product_price_original").html(products[product].info.price_original +" "+ products[product].info.currency);
    if(!products[product].info.is_promo) {
        $("#shop_product_price_original").html("");
    }

    $("#shop_product_name").html(products[product].info.name);
    $("#shop_product_description").html(products[product].info.description);
    $("#shop_product_amount").attr('value', products[product].info.in_cart);
    $("#shop_product_size").html(products[product].info.size);
    $("#shop_product_colour").html(products[product].info.colour);

    var suitable_products = "";
    for(i in products[product].info.suitable_products) {
        var link = products[product].info.suitable_products[i].product;
        var pic = products[product].info.suitable_products[i].picture;
        suitable_products += "<a href='javascript:openProduct(\""+ link +"\");'><img src='/picture.php?id="+ pic +"&amp;width=140&amp;height=150&amp;fit=fitbackground' width='140' height='150' border='0' /></a><br><br>";
    }
    $("#suitable_products").html(suitable_products);
    if(suitable_products != "") $(".suitable_products").slideDown();
    else $(".suitable_products").slideUp();
    $("#shop_product").css('display', 'block');
    //$("#shop_product").slideDown();
    //alert($("#shop_product").css('display'));
}

function insertCart() {
    products[product_current].info.in_cart = $("#shop_product_amount").attr('value');
    $.get("/load.php", {'cart': true, 'product': product_current, 'in_cart': products[product_current].info.in_cart, 'set_lang': current_lang }, function(data) {
        cart = data;
        $("#cart_total").html(cart.total_amount +" EUR");
    }, "json");
}

function closeProduct() {
    $(".banners").css('height', '');
    $("#shop_product").css('display', 'none');
    $(".suitable_products").css('display', 'none');
    $("#shop_product_list").css('display', '');
    //$("#shop_product").slideUp(1000);
    //$(".suitable_products").slideUp(1000);
    //$("#shop_product_list").slideDown(1000);
}

current_front_pic = 0;
function loopFrontPictures() {
    current_front_pic++;
    if(current_front_pic >= front_pics.length) {
        current_front_pic = 0;
    }

    $("#front_pic").fadeOut('fast', function() {
        $("#front_pic img").attr('src', '/picture.php?id='+ front_pics[current_front_pic]);
        $("#front_pic a").attr('href', front_urls[current_front_pic]);
        $("#front_pic").fadeIn();
    });
    setTimeout("loopFrontPictures()", 6000);
}

function toggle(o) {
    if($(o).css('display') == 'none') {
        $(o).slideDown();
        return;
    }
    $(o).slideUp();
}

function taust(a, change) {
    a.style.backgroundColor = change;
}


/* cart stuff */
function updatePersonalData(value, field) {
    //console.log('update: '+ value +'; '+ field);
    $.get("/load.php", { cart_personal_data: 1, field: field, value: value, 'set_lang': current_lang }, function(data) { $("#verify_"+ field).html(data); } );
}

function setDelivery(delivery, type) {
    $.get("/load.php", { cart_confirm: 1, delivery: delivery, type: type, 'set_lang': current_lang }, function(data) { $("#page_content").html(data); } );
}

function confirmCart() {
    $.get("/load.php", { cart_confirm: 1, confirm: 1, 'set_lang': current_lang }, function(data) { $("#page_content").html(data); } );
}

function cancelOrder() {
    $.get("/load.php", { cart_confirm: 1, start_new: 1, 'set_lang': current_lang }, function(data) { $("#page_content").html(data); } );
}

/* value code */
function enterValueCode() {
    $.get("/load.php", { value_code: 1, 'set_lang': current_lang }, function(data) { $("#popup_message_content").html(data); $("#popup_wrapper").slideDown() } );
}

function registerValueCode() {
    $.get("/load.php", { value_code: 1, value_code_value: $("#value_code").val(), register: 1, 'set_lang': current_lang }, function(data) {
        $("#popup_message_content").html(data);
        //$("#popup_wrapper").slideUp();

        $.get("/load.php", { cart_confirm: 1, 'set_lang': current_lang }, function(data) { $("#page_content").html(data); } );
    } );
}


