/**
 * how.much.is Publisher Widget
 * 
 * Copyright 2010 how.much.is
 *
 * CDN version 1.0.020510-001
 */

var hmiAppHost = "how.much.is";
var hmiCdnHost = "cdn-how.much.is";

if (typeof(hmiIncludes) == 'undefined') var hmiIncludes = new Object();
var jqueryPath = "http://" + hmiCdnHost + "/javascripts/jquery-1.3.2.min.js";
var jqueryUiPath = "http://" + hmiCdnHost + "/javascripts/jquery-ui-1.7.2.custom.min.js";
var jqueryUiCssPath = "http://" + hmiAppHost + "/stylesheets/custom-theme/jquery-ui-1.7.2.custom.css";
var googleAnalyticsPath = "http://www.google-analytics.com/ga.js";

function lazyLoadJQuery(hmiPublisherWidget) {
	if (typeof window.jQuery !== "function") {
        var script = document.createElement("script");
        script.setAttribute("src", jqueryPath);
        script.setAttribute("type", "text/javascript");
        document.getElementsByTagName("head")[0].appendChild(script);
        script.addEventListener("load", function(){
            lazyLoadJQueryUI(hmiPublisherWidget);
        }, true);
    } else {
        lazyLoadJQueryUI(hmiPublisherWidget);
    }
}

function lazyLoadJQueryUI(hmiPublisherWidget) {
    hmiInclude(jqueryUiCssPath, "css");
    if (typeof jQuery("html").dialog !== "function") {
        var script = document.createElement("script");
        script.setAttribute("src", jqueryUiPath);
        script.setAttribute("type", "text/javascript");
        document.getElementsByTagName("head")[0].appendChild(script);
        script.addEventListener("load", function(){
            hmiPublisherWidget.loadWidgetData();
        }, true);
    } else {
        hmiPublisherWidget.loadWidgetData();
    }
}

function lazyLoadGoogleAnalytics() {
	if (!(window.gat_ && window.gat_.getTracker_)) {
		hmiInclude(googleAnalyticsPath, "js");
	}
}

function hmiPublisherWidget(widgetData) {
	var widgetElement = '#hmi-publisher-widget';
    
    this.apiToken = widgetData.apiToken;
	this.productId = widgetData.productId;
    this.admin = widgetData.admin;
    this.manufacturerName = widgetData.manufacturerName;
    this.productName = widgetData.productName;
	
    this.fetchContent = function(){
    	// lazy load jQuery and GA
    	lazyLoadJQuery(this);
    	lazyLoadGoogleAnalytics();
    };
    
    this.loadWidgetData = function(){
        // make ajax call using ivars
        var url = "http://" + hmiAppHost + "/publisher/widget_data?" + jsonToQueryString(this.queryElements());
        hmiInclude(url, "js");
    }
    
    this.queryElements = function(){
        return ({api_token: this.apiToken,
                site_product_id: this.productId,
                msrp: this.msrp,
                currency: this.msrpCurrency,
                manufacturer_name: this.manufacturerName,
                product_name: this.productName,
                system_product_id: this.systemProductId,
                description: this.description,
                admin: this.admin});
    };

    return true;
}

function hmiInclude(jsFile, type) {
	if (hmiIncludes[jsFile] != null) return;
	if (type == "js") {
        var scriptElement = document.createElement('script');
	    scriptElement.type = 'text/javascript';
	    scriptElement.src = jsFile;
        document.getElementsByTagName('head')[0].appendChild(scriptElement);
    } else if (type == "css") {
        var scriptElement = document.createElement('link');
        scriptElement.type = 'text/css';
        scriptElement.rel = 'stylesheet';
        scriptElement.href = jsFile;
        document.getElementsByTagName('head')[0].appendChild(scriptElement);
    }
	hmiIncludes[jsFile] = jsFile;
}

function jsonToQueryString(json) {
    // convert simple hash-style JSON object to query string;
    var queryString = "";
    for (var item in json) {
        if (json[item] !== undefined) {
            queryString += (item + '=' + encodeURIComponent(json[item]) + '&');
        }
	}
    return (queryString.length > 0) ? queryString.substr(0, (queryString.length - 1)) : queryString;
}

function Querystring(qs) {
	this.params = {};
	
	if (qs.length == 0 || qs == null) return;

	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&');
	
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

