$(document).ready(function() {

    var prodFirstPart = 1;
    var prodLastPart = 1;
    var prodFirstParts = getParts('win');
    var prodLastParts = getParts('last');
    $.ajax({
        url: APP_URL + APP_FILE + "?-pg=" + AJAX_PG + "&act=last&-lg=" + VLANG,
        cache: false,
        dataType: "json",
        error: function(xhr, msg, ex) {
/*            alert(msg);*/
        },
        success: function(data){
            changeProducts(data, 'last', false);
            /* force height of div */
            $("div.productbox:last div").css("height", $(this).height());
        }
    });
    $("div.productbox:last > a:first").click(function(event) {
        event.preventDefault();
        if(!checkLink($(this))) return false;
        prodLastPart = prodLastPart - 1;
        $.ajax({
            url: AJAX_URL + "&act=last&part="+prodLastPart+"&-lg=" + VLANG,
            cache: false,
            dataType: "json",
            error: function(xhr, msg, ex) {
                prodLastPart = prodLastPart + 1;
  /*              alert(msg);*/
            },
            success: function(data){
                changeProducts(data, 'last');
                if(prodLastPart <= 1) {
                    changeLink($("div.productbox:last > a:first"), 'up', 'off');
                }
                changeLink($("div.productbox:last > a:last"), 'down', 'on');
            }
        });
    });
    $("div.productbox:last > a:last").click(function(event) {
        event.preventDefault();
        if(!checkLink($(this))) return false;
        prodLastPart = prodLastPart + 1;
        $.ajax({
            url: AJAX_URL + "&act=last&part="+prodLastPart+"&-lg=" + VLANG,
            cache: false,
            dataType: "json",
            error: function(xhr, msg, ex) {
                prodLastPart = prodLastPart - 1;
/*                alert(msg);*/
            },
            success: function(data){
                changeProducts(data, 'last');
                if(prodLastPart >= prodLastParts) {
                    changeLink($("div.productbox:last > a:last"), 'down', 'off');
                }
                changeLink($("div.productbox:last > a:first"), 'up', 'on');
            }
        });
    });
    $.ajax({
        url: AJAX_URL + "&act=win"+"&-lg=" + VLANG,
        cache: false,
        dataType: "json",
        error: function(xhr, msg, ex) {
 /*           alert(msg);*/
        },
        success: function(data){
            changeProducts(data, 'first', false);
            /* force height of div */
            $("div.productbox:first div").css("height", $(this).height());
        }
    });
    $("div.productbox:first > a:first").click(function(event) {
        event.preventDefault();
        if(!checkLink($(this))) return false;
        prodFirstPart = prodFirstPart - 1;
        $.ajax({
            url: AJAX_URL + "&act=win&part="+prodFirstPart+"&-lg=" + VLANG,
            cache: false,
            dataType: "json",
            error: function(xhr, msg, ex) {
                prodFirstPart = prodFirstPart + 1;
 /*               alert(msg);*/
            },
            success: function(data){
                changeProducts(data, 'first');
                if(prodFirstPart <= 1) {
                    changeLink($("div.productbox:first > a:first"), 'up', 'off');
                }
                changeLink($("div.productbox:first > a:last"), 'down', 'on');
            }
        });
    });
    $("div.productbox:first > a:last").click(function(event) {
        event.preventDefault();
        if(!checkLink($(this))) return false;
        prodFirstPart = prodFirstPart + 1;
        $.ajax({
            url: AJAX_URL + "&act=win&part="+prodFirstPart+"&-lg=" + VLANG,
            cache: false,
            dataType: "json",
            error: function(xhr, msg, ex) {
                prodFirstPart = prodFirstPart - 1;
/*                alert(msg);*/
            },
            success: function(data){
                changeProducts(data, 'first');
                if(prodFirstPart >= prodFirstParts) {
                    changeLink($("div.productbox:first > a:last"), 'down', 'off');
                }
                changeLink($("div.productbox:first > a:first"), 'up', 'on');
            }
        });
    });
    

    function changeProducts(data, posi, fade) {
        var spa = "div.productbox:" + posi + " div div.ie-article";
		//spa = "div.productbox.ie-helper-" + (posi == 'last' ? 2 : 1) + " div div";
		if(!fade) {
			fade = 1; 
		} else {
			fade = 'slow';
		}
		var buggyhelper = true;
        $(spa).fadeOut(fade, function() {
			if(buggyhelper) {
				$(spa).remove();
				$.each(data, function(key, prod) {
					var res = '';
					if(prod != null && prod.show == 1) {
						// use of div.ie-article because of ie
						res += '<div class="ie-article" style="display: none">';
						res += '<img src="' + IMG_URL + prod.com + '.jpg" width="168" height="126" alt="" />';
						res += '<h3>' + prod.com + '</h3>';
						res += '<p>' + prod.type + '</p>';
						res += '<p>' + prod.yoctext + '</p>';
                        if(prod.price) {
                            res += '<p>' + prod.price + '</p>';
                        }
						res += '<a class="noprint" href="' + prod.link + '">' + prod.detailstext + '</a>';
						res += '</div>';
					}
					if(res) {
						$("div.productbox:" + posi + " > div").append(res);
					}
				});
				$(spa).fadeIn('slow');
			}
			buggyhelper = false;
        } );
    }

    function checkLink(a) {
        if($(a).children('img').attr('src').indexOf("arrowbox_up_da.gif") >= 0)
            return false;
        return true;
    }

    function changeLink(a, dir, status) {
        if(status == 'on') {
            $(a).children('img').attr('src', TPL_URL + "img/arrowbox_" + dir + ".gif");
        } else {
            $(a).children('img').attr('src', TPL_URL + "img/arrowbox_" + dir + "_da.gif");
        }
    }

    function getParts(mode) {
        return $.ajax({
            url: AJAX_URL + "&act=" + mode + "parts"+"&-lg=" + VLANG,
            cache: false,
            dataType: "text",
            async:false,
            error: function(xhr, msg, ex) {
/*                alert(msg);*/
            },
            success: function(data){
                return data;
            }
        }).responseText;
    }
	
});
