jQuery(document).ready(function() {
//Pretty popin
//http://www.no-margin-for-errors.com/projects/prettyPopin/
jQuery("a[rel^='prettyPopin']").prettyPopin({
	modal : false, /* true/false */
	width : 600, /* false/integer */
	height: false, /* false/integer */
	opacity: 0.5, /* value from 0 to 1 */
	animationSpeed: 0, /* slow/medium/fast/integer */
	followScroll: true, /* true/false */
	loader_path: 'images/prettyPopin/loader.gif', /* path to your loading image */
	callback: function(){ window.location.reload();} /* callback called when closing the popin */
});
});

function toggle_radios(item)
{
var elem = "input[name='" + item +"']:radio";
var elem_ckk = "input[name='" + item +"']:radio:checked";
var chkbox = "input[name='" + item +"']:checkbox";
  if($(chkbox+':checked').val())
    {
    $(elem_ckk).removeAttr('checked');
    $(elem).attr('disabled',true);

    }
  else
    {
    $(elem).removeAttr('disabled');
    }
}

/*
Handle the add_to_compare funtionality
If button element has compared then it is active in class
compared
id = -1 --> just check the cart status
*/
function handle_compare(site_url,id)
{
//Check if ID need to added or removed
if (id == '-1')
    {
    //Check status
    to_compare = site_url + 'compare/get_cart/';
    }
else
    {
    var link_id = '#compare_' + id;
    if(!$(link_id).hasClass('compared'))
        {
        //Add To Cart
        to_compare = site_url + 'compare/add_to_compare/'+id;
        }
    else
        {
        //Delete from the cart
        to_compare = site_url + 'compare/delete/'+id;
        }
    }

$.getJSON(to_compare, { }, function(json){
    if (json /*&& json.html.length > 1 */)
        {
        $('#quick_compare').replaceWith(json.html);

        //Check if items need to removed from the Compare
        $('a.compared').each(function(i, item) {
            var id = $(this).attr('id');
            id = id.replace('compare_','');
            if($.inArray(id, json.compare) == -1)
                {
                $(this).removeClass('compared');
                $(this).html('<i></i><span><span></span><i></i>Lisää vertailuun</span>');
                }
            });
        //Add New missing items to Compare
        $.each(json.compare, function(i,item){
               var link_id = '#compare_' + item;
               if(!$(link_id).hasClass('compared'))
                    {
                    var link_id_text = '#compare_' + item + ' span';
                    $(link_id).addClass('compared');
                    $(link_id_text).html('<span></span><i></i>Poista Vertailusta');
                    }
               });
        }
    });
 return false;
}
