/*jslint browser: true*/
/*global $, jQuery*/
var disqus_shortname = ''; // Required - Replace example with your forum shortname
var disqus_identifier = ''; //a unique identifier for each page where Disqus is present';
var disqus_title = ''; //a unique title for each page where Disqus is present';
var disqus_url = ''; //a unique URL for each page where Disqus is present';
$(document).ready(function(){
    function GetURLForDisqus(area_title){
        var prefix = 'http://www.oneit.com.au/prototypes/'+disqus_shortname+'/';
        var fileNameStartIndex = window.location.href.lastIndexOf('/') + 1;
        var htmlFilename =  window.location.href.substr(fileNameStartIndex,window.location.href.length - fileNameStartIndex).trim();
        htmlFilename = htmlFilename.charAt(htmlFilename.length - 1) == '#' ? htmlFilename.substr(0,htmlFilename.length-1) : htmlFilename;
        return prefix + htmlFilename + '#!' + area_title.replace(/ /g,'');
    }
    
    var disqusPublicKey = "AabKM2qQNAhUFYDpOioBbIszh2eSiOXAAsvWYpyCetaiEZDnO0rAKa3BWlaOfKD5";
    var disqusShortname = '';
    
    disqusShortname = $('.disqus-comment-button').attr('data-disqus-shortname');
    
    if(disqusShortname != null && disqusShortname != ''){
        disqus_shortname = disqusShortname;
        var identArray = [];
        $('.disqus-comment-button').each(function () {
            var ident = $(this).attr('data-disqus-identifier');
            identArray.push('ident:' + ident);

        });
        
        $.ajax({
            type:'GET',
            url:"https://disqus.com/api/3.0/threads/set.jsonp",
            data: { api_key: disqusPublicKey, forum : disqusShortname, thread : identArray },
            cache: false,
            dataType: 'jsonp',
            success: function (result) {
                for(var i in result.response){
                    var count = result.response[i].posts;
                    
                     if (count > 0) {
                         $('a[data-disqus-identifier="' + result.response[i].identifiers[0] + '"]').addClass('boldtext');
                     }
                     $('a[data-disqus-identifier="' + result.response[i].identifiers[0] + '"]').text('Show Comments (' + count +')');
                }
            }
        });
        
        $('.disqus-comment-button').click(function(){
            var button = $(this);
            var identifier = button.attr('data-disqus-identifier');
            var title = button.attr('data-disqus-areaName');
            var url = GetURLForDisqus(title);
            if(window.DISQUS){
                $('#disqus_thread').insertAfter(button); //append the HTML after the link
                
                //if Disqus exists, call it's reset method with new parameters
                DISQUS.reset({
                  reload: true,
                  config: function () {
                      this.page.identifier = identifier;
                      this.page.url = url;
                      this.page.title = title;
                  }
                });
                
            }else{
                //insert a wrapper in HTML after the relevant "show comments" link
                $('<div id="disqus_thread"></div>').insertAfter(button);
                disqus_identifier = identifier;
                disqus_title = title;
                disqus_url = url;
                
                
                //append the Disqus embed script to HTML
                var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
                $('head').append(dsq);
            }
            
        });
    }
    
});