//TODO: Improve the examples scripts -- too hardcoded
//TODO: Remove hardcoded references to variable myVizabi

//url params
function randomSize(id) {
    var width = Math.floor(Math.random() * 800) + 300;
    var height = Math.floor(Math.random() * 500) + 300;
    var container = document.getElementById(id);
    
    setSize(id, width, height);

    forceResizeEvt();
}

function phoneSize(id, mode) {
    var container = $("#" + id);
    if (mode === 'landscape') {
        container.width(568);
        container.height(320);
    } else {
        container.width(320);
        container.height(568);
    }
    container.removeClass('fullscreen');
    forceResizeEvt();
}

function fullSize(id) {
    var container = $("#" + id);
    container.width("auto");
    container.height("auto");
    container.addClass('fullscreen');
    forceResizeEvt();
}

function setSize(id, width, height, fullscreen, block_url) {
    var container = $("#" + id);

    container.width(width);
    container.height(height);
    if (fullscreen) {
        container.addClass('fullscreen');
    } else {
        container.removeClass('fullscreen');
    }
    forceResizeEvt(block_url);
}

function forceResizeEvt(block_url) {
    //force resize
    event = document.createEvent("HTMLEvents");
    event.initEvent("resize", true, true);
    event.eventName = "resize";
    window.dispatchEvent(event);

    if(!block_url) updateURL();
}

function setLanguage(id, language) {
    var newOption = {
        language: language
    };
    myVizabi.setOptions("#" + id, newOption);
}

function setCurrentState(id, idState) {
    if (!idState) {
        idState = "state";
    }
    var state = document.getElementById(idState).innerHTML;
    state = JSON.parse(state);
    forceState(id, state);
}

function forceState(id, state) {
    var newOption = {
        state: state
    };
    myVizabi.setOptions("#" + id, newOption);
}

function showState(state, id, block_url) {
    if (!id) {
        id = "state";
    }
    var container = document.getElementById(id);
    var str = JSON.stringify(state, null, 2);
    container.innerHTML = str;

    if(!block_url) updateURL();
}

function showQuery(query) {
    var container = document.getElementById("query");
    var str = JSON.stringify(query, null, 2);
    container.innerHTML = str;
}

function goToExample(example) {
    location.href = '../' + example + '.html';
    return;
}

function updateURL() {
    var url = {
        state: $("#state").text().replace(/(\s|\r\n|\n|\r)/gm, ""),
        width: $(".placeholder").width(),
        height: $(".placeholder").height(),
        fullscreen: $(".placeholder").hasClass('fullscreen')
    };
    url_string = JSON.stringify(url);
    location.href = "#" + url_string;
}

var url = {};

function parseURL(block_url) {
    var hash = window.location.hash;

    if (hash) {
        options = JSON.parse(hash.replace("#", ""));

        var placeholder = $(".placeholder").attr("id");
        var state = JSON.parse(options.state);

        url.state = state;
        url.lang = options.lang;

        setSize(placeholder, options.width, options.height, options.fullscreen, block_url);

    }
}

function shareLink() {
    updateURL();
    var address = "https://api-ssl.bitly.com/v3/shorten",
        params = {
            access_token: "8765eb3be5b975830e72af4e0949022cb53d9596",
            longUrl: document.URL
        };
    $.getJSON(address, params, function(response) {
        if (response.status_code == "200") {
            prompt("Copy the following link: ", response.data.url);
        } else {
            console.log(response);
            alert("Copy the link from the browser");
        }
    });

}

/*dropdown*/
function DropDown(el) {
    this.dd = el;
    this.placeholder = this.dd.children('span');
    this.opts = this.dd.find('ul.dropdown > li');
    this.val = '';
    this.index = -1;
    this.initEvents();
}

DropDown.prototype = {
    initEvents: function() {
        var obj = this;

        obj.dd.on('click', function(event) {
            $(this).toggleClass('active');
            return false;
        });

        obj.opts.on('click', function() {
            var opt = $(this);
            obj.val = opt.text();
            obj.index = opt.index();
            obj.placeholder.text(obj.val);
        });
    },
    getValue: function() {
        return this.val;
    },
    getIndex: function() {
        return this.index;
    }
}

function hashHandler(whenChanged){
    this.oldHash = window.location.hash;
    this.Check;

    var that = this;
    var detect = function(){
        if(that.oldHash!=window.location.hash){
            that.oldHash = window.location.hash;
            if(typeof whenChanged === 'function') whenChanged(that.oldHash);
        }
    };
    this.Check = setInterval(function(){ detect() }, 100);
}

parseURL();
var hashDetection = new hashHandler(function(newhash) {
    console.log("changed: "+newhash);
    //parseURL blocking the url update
    parseURL(false);
    console.log(url.state);
    var id = $('.container .placeholder').attr('id');
    //force new state
    //TODO: Really bad way of looking for id here
    forceState(id, url.state);
});

$(function() {

    var menu = new DropDown($('#dropdown-menu')),
        size = new DropDown($('#dropdown-size')),
        language = new DropDown($('#dropdown-language')),
        options = new DropDown($('#dropdown-options'));

    $('.wrapper-dropdown').click(function() {
        $('.wrapper-dropdown').not(this).each(function() {
            $(this).removeClass('active');
        });
    });

    $(document).click(function() {
        // all dropdowns
        $('.wrapper-dropdown').removeClass('active');
    });

});
@@ -895,27 +895,16 @@
 llscreen
-, block_url
 ) %7B%0A    
@@ -1138,25 +1138,16 @@
 sizeEvt(
-block_url
 );%0A%7D%0A%0Afu
@@ -1168,25 +1168,16 @@
 sizeEvt(
-block_url
 ) %7B%0A    
@@ -1347,39 +1347,24 @@
 event);%0A%0A   
- if(!block_url)
  updateURL()
@@ -1884,27 +1884,16 @@
 tate, id
-, block_url
 ) %7B%0A    
@@ -2065,23 +2065,8 @@
 %0A   
- if(!block_url)
  upd
@@ -2707,25 +2707,16 @@
 arseURL(
-block_url
 ) %7B%0A    
@@ -3066,19 +3066,8 @@
 reen
-, block_url
 );%0A%0A
@@ -4355,756 +4355,17 @@
 %0A%7D%0A%0A
-function hashHandler(whenChanged)%7B%0A    this.oldHash = window.location.hash;%0A    this.Check;%0A%0A    var that = this;%0A    var detect = function()%7B%0A        if(that.oldHash!=window.location.hash)%7B%0A            that.oldHash = window.location.hash;%0A            if(typeof whenChanged === 'function') whenChanged(that.oldHash);%0A        %7D%0A    %7D;%0A    this.Check = setInterval(function()%7B detect() %7D, 100);%0A%7D%0A%0AparseURL();%0Avar hashDetection = new hashHandler(function(newhash) %7B%0A    console.log(%22changed: %22+newhash);%0A    //parseURL blocking the url update%0A    parseURL(false);%0A    console.log(url.state);%0A    var id = $('.container .placeholder').attr('id');%0A    //force new state%0A    //TODO: Really bad way of looking for id here%0A    forceState(id, url.state);%0A%7D
+parseURL(
 );%0A%0A

Remove buggy :bug: history control