/* 
 * @file Shop Info
 */

var ShopInfoDialogId = 'shop_info_dialog';
var ShopInfoUrl = 'ajax/shop/info.php';
var ShopInfoData = null;

function LoadInfoDialog(){

    $('body').append('<div id="'+ShopInfoDialogId+'"></div>');

    dialog = $('#'+ShopInfoDialogId);
    //dialog.hide();
    dialog.addClass('pop_up');


    container = $('<div id="pop_up_container"></div>');
    content   = $('<div id="pop_up_content"></div>');
    content.append('<div id="pop_up_content_top"></div>');
    content.append('<div id="pop_up_content_bottom"></div>');
    content.append('<div id="close_it_box"></div>');
    container.append(content);

    dialog.append(container);

    $('#close_it_box').click(HideInfoDialog);

    //Load data
    $.post(ShopInfoUrl,null,DataLoaded,"json");

    dialog.hide();

    $('a.help_link').click(function(){

        ShowInfoDialog($(this).attr('href'));

        return false;
    })

}

function DataLoaded(data){

    ShopInfoData = new Array();

    box_container = $('#pop_up_content_bottom');

    for(i in data){
        title = data[i]['Title'];
        text  = data[i]['Text'];

        box = $('<div></div>');
        box.addClass('pop_boxes');
        box.addClass('ppb_inactive');
        box.html('<a href=""><h3>'+title+'</h3></a>');
        box.attr('id','popbox_'+i);
        box_container.append(box);

        ShopInfoData[i]=text;
    }

    //Define Action for boxes
    $('div.pop_boxes a').click(function(){

        $('[id^=popbox_]').each(function(){
            if( $(this).hasClass('ppb_active') ){
                $(this).removeClass('ppb_active');
                $(this).addClass('ppb_inactive');
            }
        });

        if( $(this).parent().hasClass('ppb_inactive') ){
            $(this).parent().removeClass('ppb_inactive');
        }

        if( !$(this).parent().hasClass('ppb_active') ){
            $(this).parent().addClass('ppb_active');
        }
            

        id = $(this).parent().attr('id').replace('popbox_','');
        $('#pop_up_content_top').html(ShopInfoData[id]);
     
        return false;
    });
	
	try {
		if(callPage!='')
			ShowInfoDialog(callPage);
	} catch (exception) { 
    
	}

	
}

function ShowInfoDialog(param){
    $('#pop_up_content_top').html(ShopInfoData[param]);

    $('[id^=popbox_]').each(function(){
            if( $(this).hasClass('ppb_active') ){
                $(this).removeClass('ppb_active');
                $(this).addClass('ppb_inactive');
            }
        });

    cont = $('#popbox_'+param);

    if( cont.hasClass('ppb_inactive') )
        cont.removeClass('ppb_inactive');

    if( !cont.hasClass('ppb_active') )
        cont.addClass('ppb_active');
    
    $('html, body').animate({scrollTop:0}, 'slow', function(){
        $('#'+ShopInfoDialogId).fadeIn();
    });
    
}

function HideInfoDialog(){
    dialog = $('#'+ShopInfoDialogId);
    dialog.fadeOut();
}


function LoadPopulars(){

   var params = {type:'nav,acc',limit:3,split:1};
   
    $.post(
        'ajax/shop/products.php?retrieve=populars',
        {data: JSON.stringify(params)},
        function(data){
           var pop = '#shop_populars_';
           var accPrefixLink = (products_type=='aero')?'0504':'0504';
           var navPrefixLink = (products_type=='aero')?'0507':'0506';
           for(t in data){
               pdata = data[t];
                for(i in pdata){
                   p = pdata[i];
                   n = parseInt(i)+1;
                   counter = $('<div></div>').html(n);
                   var link = $('<a></a>');

                   if(p['ProductType'].toLowerCase().indexOf('farm') > -1)
                       {
                            var accPrefixLink = '0515';
                            var navPrefixLink = '0516';
                       }else if(p['ProductType'].toLowerCase().indexOf('aero') > -1){
                            var accPrefixLink = '0508';
                            var navPrefixLink = '0507';
                       }else{
                            var accPrefixLink = '0504';
                            var navPrefixLink = '0506';
                       }
                   if(p['ProductCode'].toLowerCase().indexOf('nav') == 0)
                    link = $('<a></a>').attr('href',"index.php?swt="+navPrefixLink+"&ipr="+p['ProductId']);
                   else if(p['ProductCode'].toLowerCase().indexOf('acc') == 0)
                    link = $('<a></a>').attr('href',"index.php?swt="+accPrefixLink+"&com=-1&acc="+p['ProductId']);


                   link.html(p['ProductName']);

                   litem = $('<li></li>').append(counter).append(link);

                   $(pop+t).append(litem);
               }
           }

        },
        'json'
        );

}
