
/************** EVENTS **************/
    
//ask the user to do a fb auth, and then post to stream 
$('#alert_facebook_text').click(function()
{
    FB.getLoginStatus(function(response)
    {
        if (response.session)
        {
            //already logged in and connected - publish
            fb_publish();
        }
        else
        {
            //neither loggedin or connected - do it
            FB.login(function(response)
            {
                if (response.session)
                {
                    if (response.perms)
                    {
                        //user is loggedin and connected to app user is logged in and granted some permissions. - publish                      
                        fb_publish();                                                                                            
                    }
                    else
                    {
                      //user is loggedin but didn't like the site
                    }
                }
                else
                {
                    //user wouldn't / couldn't log in 
                } 
            }, {perms:'publish_stream'});            
        }
    });
    return false;
});

//publish a story to users fb stream
function fb_publish()
{
    var body = 'Vill tipsa alla om sidan http://www.familjemarknaden.se - prisjämförelser för leksaker, barnkläder, bebisprylar och mammakläder. Helt klart värt ett besök!';
    FB.api('/me/feed', 'post', { message: body }, function(response)
    {
        if (!response || response.error)
        {
            //ops...?
        }
        else
        {
            $('#alert_facebook').fadeOut('slow', function()
            {
                //animation complete, disable alert-box for user 
                $.ajax(
                {
                    async: false,
                    type: "GET",
                    url: "/services/set.disable_alert.php?type=1",
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    cache: false
                });
            });
            $('#alert_holder').html('<span>Tack för att du tipsade dina vänner om Familjemarknaden.se!</span>').css('display', 'block');
        }
    });
}

//close and disable fb notice box
$('#alert_facebook_close').click(function() {
  $('#alert_facebook').fadeOut('slow', function() {
    // Animation complete, disable for user 
    $.ajax(
    {
        async: false,
        type: "GET",
        url: "/services/set.disable_alert.php?type=1",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        cache: false
    });
  });
});

//log product click
$("a[rel*=product-link]").click(function()
{ 
    var url = this.href; 
    $.ajax(
    {
        async: false,
        type: "POST",
        url: "/services/log.product_click.php", //dynamic url to logging action
        data:
        {
            item: url
        },
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        cache: false
    });
    return true; 
});

//open external links i new window
$("a[rel*=external]").click(function()
{
    this.target = "_blank";
});

/************** FUNCTIONS **************/

function do_open_filter_item(p_item_id)
{
    var l_old_state = p_item_id.getAttribute('class');

    /* Disable category */
    var l_item = document.getElementById('filter_item_cat');
    var l_img = document.getElementById('filter_img_cat');
    var l_cont = document.getElementById('filter_cont_cat');              
    l_item.setAttribute("class", "filter_item");
    l_img.setAttribute("src", "/images/dd_right.png");  
    l_cont.setAttribute("class", "filter_item_content");  

    /* Disable company */
    var l_item = document.getElementById('filter_item_comp');
    var l_img = document.getElementById('filter_img_comp');
    var l_cont = document.getElementById('filter_cont_comp');              
    l_item.setAttribute("class", "filter_item");
    l_img.setAttribute("src", "/images/dd_right.png");  
    l_cont.setAttribute("class", "filter_item_content");  

    /* Disable price */
    var l_item = document.getElementById('filter_item_price');
    var l_img = document.getElementById('filter_img_price');
    var l_cont = document.getElementById('filter_cont_price');              
    l_item.setAttribute("class", "filter_item");
    l_img.setAttribute("src", "/images/dd_right.png");  
    l_cont.setAttribute("class", "filter_item_content");  

    /* Disable sort */
    var l_item = document.getElementById('filter_item_sort');
    var l_img = document.getElementById('filter_img_sort');
    var l_cont = document.getElementById('filter_cont_sort');              
    l_item.setAttribute("class", "filter_item");
    l_img.setAttribute("src", "/images/dd_right.png");  
    l_cont.setAttribute("class", "filter_item_content");  
      
    /* Switch current*/
    var l_name = p_item_id.id.split('_')[2];
    if(l_old_state == 'filter_item')
    {   
        /* Enable */
        var l_img = document.getElementById('filter_img_' + l_name);
        var l_cont = document.getElementById('filter_cont_' + l_name);    
        p_item_id.setAttribute("class", "filter_item filter_selected");
        l_img.setAttribute("src", "/images/dd_down.png");  
        l_cont.setAttribute("class", "filter_item_content filter_selected");
    }
    else
    {
        /* Disable */
        var l_img = document.getElementById('filter_img_' + l_name);
        var l_cont = document.getElementById('filter_cont_' + l_name);    
        p_item_id.setAttribute("class", "filter_item");
        l_img.setAttribute("src", "/images/dd_right.png");  
        l_cont.setAttribute("class", "filter_item_content");
    }          
}
