(function() {
// Localize jQuery variable
var jQuery;
var host = 'ekobilet.pl'

/* loading jQuery */
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
    script_tag.onload = scriptLoadHandler;
    script_tag.onreadystatechange = function () { // IE hack
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
            scriptLoadHandler();
        }
    };
    // append jquery
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);

} else {
    jQuery = window.jQuery;
    main();
}


/* script loading callback function */
function scriptLoadHandler() {
    jQuery = window.jQuery.noConflict(true);
    main(); 
}

/* preference main function */
function main() { 
    jQuery(document).ready(function($) { 
		

		var widget_tag = document.createElement('div');
		widget_tag.setAttribute("id", "ekobilet-widget");
		(document.getElementsByTagName("body")[0] || document.documentElement).appendChild(widget_tag);
		
		var hostname = window.location.hostname.split("www.")[1] || window.location.hostname
		var location = hostname+window.location.pathname+window.location.search;
	
	    /* Load CSS */
        var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "http://"+host+"/stylesheets/widget.css" 
        });

        css_link.appendTo('head');
		
		setPosition(widget_tag);
		/* Load HTML */
		var widget_url = "http://"+host+"/widget.js?callback=?&location="+location+"&host="+hostname
		$.getJSON(widget_url, function(data) {
		  $('#ekobilet-widget').html(data.html);
		});
		
		var new_widget_url = "http://"+host+"/widget/new.js?callback=?"
		$("#ekobilet-widget .newWidget").live("click", function() {
			$.getJSON(new_widget_url, function(data) {
			  $("body").append(data.html);
			});
		    return false;
		})
		
		$("#ekobilet-content .createWidget a").live("click", function() {
			var create_widget_url = "http://"+host+"/widget/create.js?"+"callback=?"+"&event_id="+$(this).parent().attr("data-id")+"&location="+location+"&host="+hostname
			$.getJSON(create_widget_url, function(data) {
			  if (data.success) {
				  $("#ekobilet-content").html(data.flash);
				  $('#ekobilet-widget').html(data.widget);
				  window.setTimeout(function() {
					$('#ekobilet-overlay, #ekobilet-content').remove()
				  }, 5000);
			  } else {
				  $("#ekobilet-content").prepend(data.flash);
			  }
			});
			return false;
		})
		
		$("#ekobilet-widget .deleteWidget").live("click", function() {
			var delete_widget_url = "http://"+host+"/widget/delete.js?"+"callback=?"+"&widget_id="+$(this).attr("data-id")+"&host="+hostname
			$.getJSON(delete_widget_url, function(data) {
			  $('#ekobilet-widget').html(data.html);
			});
			return false;
		})
		
		$("#ekobilet-widget .buyAction").live("click", function() {
			var frame_tag = document.createElement('iframe');
	    	frame_tag.setAttribute("src",$(this).attr("href"));
	    	frame_tag.setAttribute("class","buyFrame");
		    document.getElementById("ekobilet-widget").appendChild(frame_tag);
			return false;
		})
		
		$("#ekobilet-widget .submenuAction").live("hover", function() {
			$("#ekobilet-widget .events-list").fadeIn();
			$("#ekobilet-widget .ekobilet-info-box").hide();
		})
		
		$("#ekobilet-widget .ekobilet-close a").live("click", function() {
			$("#ekobilet-widget .events-list").hide();
		})
		
		$("#ekobilet-widget .ekobilet-details").live("click", function() {
			$("#ekobilet-widget .events-list").hide();
			$("#ekobilet-widget .ekobilet-info-box").fadeIn();
		})
		
		$("#ekobilet-overlay").live("click", function() {
			$("#ekobilet-overlay, #ekobilet-content").remove();
		})
    });
}

})();
