$(document).ready( function () {
    
    market.ini();
           
});


var market = {

    artColDiv : 'artCol',
    
    currentArticleDiv : null,
    
    ini : function () {
 
        // load the flash 
        $('#marketCol').flash( {
            swf: '/media/specials/instoreadvertising/show_pano.swf',
            width: 620,
            height: 330,
            play: true,
            allowFullScreen: true,
            // these arguments will be passed into the flash document
            flashvars: {
                movie: '/media/specials/instoreadvertising/pano.swf',
                xml_file: '/media/specials/instoreadvertising/' + conf.countryCode + '/eingang.xml',
                redirect: window.location
            }
        });
        
        $('#artCol').prepend('<div id="mLoading" style="display: none;"><img src="/img/misc/artgrp-loader.gif" alt="" title="" /></div>');
        
    },
    
    loadArticlesCol : function (artArray) {
        
        $('#mLoading').show();
        
        articleIds = artArray.articles; 
        // hide default image to display article
        $('#mDefaultImage').hide();
        
        // hide all divs in the section
        $('.mArticleRow').hide();
        $('.mArticleListRow').hide();
    
        if (artArray.articles.length > 1) {
            
            currentGrpDiv = 'articleList' + artArray.articles[0];

            if ($('#' + currentGrpDiv).length > 0) { 
                $('#' + currentGrpDiv).show(); 
                $('#mLoading').hide(); 
            } else {
                
                $('#artCol').append('<div id="' + currentGrpDiv + '" class="mArticleListRow"></div>');
                
                // get all articles
                $.each(articleIds, function (i, value) {
                    // get the article via json 
                    $.getJSON('/xhr/art/json.php', {
                        art : value,
                        loc : conf.loc,
                        grouped : true
                    },
                    function(data, textStatus) {
                        if (textStatus == 'success') {   
                            // display article info   
                            market.buildArticle(data, 'list', currentGrpDiv);   
                        }
                    });
                });
                
            }
            
        } else {
        
            $.each(articleIds, function (i, value) {
                
                currentDiv = 'article' + value;

                // check if there is a div with currentDiv´s name
                if ($('#' + currentDiv).length > 0) { 
                    $('#' + currentDiv).show(); 
                    $('#mLoading').hide(); 
                } else {       
                    // get the article via json 
                    $.getJSON('/xhr/art/json.php', {
                        art : value,
                        loc : conf.loc
                    },
                    function(data, textStatus) {
                        if (textStatus == 'success') { 
                            // display article info   
                            market.buildArticle(data, 'art', false);   
                        }
                    });
                }
            });
        }
        
    },
    
    buildArticle : function (artObj, type, div) {
      
        if (type == 'art') { 
        
            currentDiv = 'article' + artObj.id;
            
            // wrapper
            $('#' + market.artColDiv).append('<div id="' + currentDiv + '" class="mArticleRow"></div>');
        
        } else {
            currentDiv = div;
        }
                
        if (type == 'art') { 
            // art title
            $('#' + currentDiv).append('<h3>' + artObj.title + '</h3>');
            $('#' + currentDiv).append('<div class="p">' + artObj.description + '</div>');
            $('#' + currentDiv).append(artObj.artLink);
        } else {
            $('#' + currentDiv).append('<div class="articleListItem" id="articleListItem' + artObj.id + '"></div>');
            $('#articleListItem' + artObj.id).append('<h3>' + artObj.title + '</h3>');
            $('#articleListItem' + artObj.id).append(artObj.artLink);
        }
        
        if (type == 'art') { 
            // get all images an build a image slideshow 
            images = artObj.images;
            if (images.length > 0) {
                $('#' + currentDiv).append('<div id="artImageSlideshow' + currentDiv + '" style="display: none;" class="mArtImageRow"></div>'); 
                
                // insert the images 
                imagePreload = new Array();
                
                market.imageLoader(artObj, 0); 
            }
        }
        
        // if everything is loaded, hide loader image
        $('#mLoading').hide();
        
        market.currentArticleDiv = currentDiv;   
        
    },
    
    imageLoader : function (artObj, i) {
        
        currentDiv = 'article' + artObj.id;
    
        // preload images 
        curImg = artObj.images[i];  
                  
        if (curImg.thumbImg) {
            imagePreload[i] = new Image();         
            imagePreload[i].src = artObj.imageHost + curImg.thumbImg;
        }
        
        imagePreload[i].onload = function () {
            $('#artImageSlideshow' + currentDiv).append('<div class="mArtImageContainer"><img src="' + imagePreload[i].src + '" title="" /></div>');
            
            z = i+1;
            end = artObj.images.length-1;
            
            if (end >= z) {            
                market.imageLoader(artObj, z);
            } else {
                $('#artImageSlideshow' + currentDiv).fadeIn();    
            } 
        } 
    }
    
}

