var my_videos = new Array();
my_videos["XONIA FEAT. DEEPCENTRAL - MY BEAUTIFUL ONE"] = "VfCyLO8Yvi8";
my_videos["MORANDI - ROCK THE WORLD"] = "818pShsaMr4";
my_videos["CATALIN JOSAN - DON'T WANNA MISS U"] = "Vj2fUdupXq8";
my_videos["EL RADU - N-AM SĂ TE CHEM"] = "r9c9qlP3L4E";

function format(str) {
    for (i = 1; i < arguments.length; i++)
        str = str.replace('{' + (i - 1) + '}', arguments[i]);
    return str;
}

function show_youtube(query) {

    for (var i in my_videos) {
        //alert('query is ' + query + ' key is: ' + i + ', value is: ' + my_videos[i]);
        if (i == query.toUpperCase()) {
            PlayVideo(my_videos[i]);
            return;
        }
    }

    query = query.replace("-", " ");
    query = encodeURI(query);
    $.getJSON("http://gdata.youtube.com/feeds/api/videos?q=" + query + "&v=2&alt=json-in-script&callback=?&max-results=1&start-index=1&format=5", function (data) {
        $.each(data.feed.entry, function (i, item) {
            var videoid = item.media$group.yt$videoid.$t;
            PlayVideo(videoid);
            return;
        });
    });
}
function popup_content(_content, _width, _height, _title) {
    if (typeof _title == "undefined")
        _title = function () { return "<img alt='' src='http://mediaforest.biz/css/images/layout/footer-logo.jpg' />"; };
        
    if (typeof _width == "undefined")
        $.colorbox({ title: _title, html: _content });
    else
        $.colorbox({ title: _title, html: _content, innerWidth: _width, innerHeight: _height });
}
function popup_url(_url, _width, _height,_title) {

    if (typeof _title == "undefined")
        _title = function () { return "<img alt='' src='http://mediaforest.biz/css/images/layout/footer-logo.jpg' />"; };

    if (typeof _width == "undefined")
        $.colorbox({ title: _title, iframe:true, href: _url });
    else
        $.colorbox({ title: _title, iframe: true, href: _url, innerWidth: _width, innerHeight: _height });
} 
function NTTTableToFusionChartScroll(caption, sub_caption, tableId, label_col, value_col, chartId, swf_base_url, width, height, ignore_last_row) {
    var header = "<chart bgColor='FFFFFF' bgAlpha='0' useRoundEdges='1' showExportDataMenuItem='1' caption='" + PrepareHebValue(caption).xmlEscape() + "' subcaption='" + PrepareHebValue(sub_caption).xmlEscape() + "' showLegend='1' showCaption='1' showPercentValues='0' pieSliceDepth='30' showBorder='0'>";
    var strXML = "";
    var count = 0;
    var total_records = $("#" + tableId + " tbody tr").length;
    var categories = "<categories>";
    var dataset = "<dataset seriesName='" + PrepareHebValue(sub_caption).xmlEscape() + "'>";

    $("#" + tableId + " tbody tr").each(function() {

        categories = categories + "<category label='" + PrepareHebValue(this.children[label_col].innerHTML).xmlEscape() + "' />";
        dataset = dataset + "<set value='" + this.children[value_col].innerHTML + "' />";
        count++;
        if (((total_records - 1) == count) && (ignore_last_row == true))
            return false;
    });

    var strXML = header + categories + "</categories>" + dataset + "</dataset></chart>";

    //var chart1 = new FusionCharts(swf_base_url + "ScrollColumn2D.swf", "fs" + chartId, width, height, "0", "1");
    //chart1.setDataXML(strXML);
    //chart1.render(chartId);

     $('#'+chartId).insertFusionCharts({
            product: 'v3',
            swfPath: swf_base_url,
            type: ScrollColumn2D,
            data: strXML,
            dataFormat: "XMLData",
            width: width,
            height: height,
            wMode: "transparent"
    });
}
function NTTTableToFusionChart(caption, sub_caption, tableId, label_col, value_col, chartId, max_records, swf_base_url, width, height, ignore_last_row) {
    var strXML = "<chart bgColor='FFFFFF' bgAlpha='0' useRoundEdges='1' showExportDataMenuItem='1' caption='" + PrepareHebValue(caption).xmlEscape() + "' subcaption='" + PrepareHebValue(sub_caption).xmlEscape() + "' showLegend='1' showCaption='1' showPercentValues='0' pieSliceDepth='30' showBorder='0'>";
    var count = 0;
    var total_records = $("#" + tableId + " tbody tr").length;

    $("#" + tableId + " tbody tr").each(function() {

        strXML = strXML + "<set label='" + PrepareHebValue(this.children[label_col].innerHTML).xmlEscape() + "' value='" + this.children[value_col].innerHTML + "' />";

        count++;
        if (((total_records - 1) == count) && (ignore_last_row == true))
            return false;
        if (max_records != null && ((count * 1) == (max_records * 1)))
            return false;
    });

    strXML = strXML + "</chart>";
    var swf_file = "Column3D.swf";
    if (max_records != null) {
        if (max_records * 1 > 10)
            swf_file = "Pie3D.swf";
    }
    else if (total_records > 10)
        swf_file = "Pie3D.swf";

    //var chart1 = new FusionCharts(swf_base_url + swf_file, "fs" + chartId, width, height, "0", "1");
    //chart1.setDataXML(strXML);
    //chart1.render(chartId);

    $('#' + chartId).insertFusionCharts({
        product: 'v3',
        swfPath: swf_base_url,
        type: swf_file.replace(".swf",""),
        data: strXML,
        dataFormat: "XMLData",
        width: width,
        height: height,
        wMode: "transparent"
    });
} 
// Remove invalid XML characters
String.prototype.xmlEscape = function() {
    if( this == null || this.length == 0 )
        return this;
    return this.replace(/&/g, "and").replace(/"/g, "``").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/'/g, "`");
//    return this.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/'/g, "&apos;");
}
String.prototype.xmlUnescape = function() {
    if (this == null || this.length == 0)
        return this;
    return this.replace(/&amp;/g, "&").replace(/&quot;/g, "\"").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&apos;/g, "'");
//    return this.replace(/&amp;/g, "&").replace(/&quot;/g, "\"").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&apos;/g, "'");
}   
function set_object_html_if_not_set(object_id, html) {
    if ($("#" + object_id).html() != html)
        $("#" + object_id).html(html);
}
function IsHebrew(str) {
    for (var i = 0; i < str.length; i++) {
        if (str.charAt(i) >= 'א' || str.charAt(i) >= 'ת')
            return true;
    }
    return false;
}
function PrepareHebValue(val) {
    if (IsHebrew(val)) {
        var result = "";
        var parts = val.split(' ');
        for (var i = parts.length - 1; i >= 0; i--) {
            if (result == "")
                result = result + parts[i];
            else
                result = result + " " + parts[i];
        }
        return result;
    }
    else
        return val;
}
function PlayVideo(videoid) {
    var _href = "http://www.youtube.com/embed/" + videoid + "?rel=0&autoplay=1";
    $.colorbox({ title: function () { return "<img alt='' src='http://mediaforest.biz/css/images/layout/footer-logo.jpg' />"; }, href: _href, iframe: true, innerWidth: 600, innerHeight: 355 });
}
function ShowYoutubeClips(div_element, query, max_results, image_width, image_height, enable_onclick) {

    $("#" + div_element).html("loading video clips please wait...");

    $.getJSON("http://gdata.youtube.com/feeds/api/videos?q=" + query + "&v=2&alt=json-in-script&callback=?&max-results=" + max_results + "&start-index=1", function (data) {

        $("#" + div_element).html("");

        $.each(data.feed.entry, function (i, item) {

            var title = item.title.$t;
            var thumb = item.media$group.media$thumbnail[0].url;
            var videoid = item.media$group.yt$videoid.$t;
            var link = "http://www.youtube.com/watch?v=" + videoid + "?autoplay=1&fs=1&color1=0x5d1719&color2=0xcd311b&border=1"; //item.link[0].href;

            var html = '<img id="youtubethumb" align="absmiddle" style="width:' + image_width + 'px;height:' + image_height + 'px;cursor:pointer;padding:1px;border:2px solid black;background-color:white;" src="' + thumb + '"  title="' + title + '" ';
            if (typeof enable_onclick == "undefined")
                html += ' onclick="PlayVideo(\'' + videoid + '\'); return false;" ';
            html += '/>';
            $("#" + div_element).append(html);
        });

    });
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function NTTHideDollarsInfo(_artist)
{
    var begin,end;
    if(_artist==null || _artist=="" )
        return "";
    begin=_artist.indexOf("$")+1;
    end=_artist.indexOf("$",begin);
    if(begin!=0 && end!=(-1))
        return _artist.replace(_artist.substring(begin-1,end+1),"");

    return _artist;
}

function NTTGetDollarsInfo(_artist) {
    var begin, end;
    if (_artist == null || _artist == "")
        return "";
    begin = _artist.indexOf("$") + 1;
    end = _artist.indexOf("$", begin);
    if (begin != 0 && end != (-1))
        return _artist.substring(begin, end);
    return "";
}

function NTTGetLabel(_artist) {

    if (_artist == null || _artist == "")
        return "";

    var meta_data = NTTGetDollarsInfo(_artist);
    if (meta_data == null || meta_data == "")
        return "";

    var s_index = -1, e_index = -1;

    s_index = meta_data.toLowerCase().indexOf("label_");
    if (s_index == -1)
        return "";
    e_index = meta_data.indexOf(',', s_index);
    if (e_index == -1)
        e_index = meta_data.length;
    return meta_data.substring(s_index + 6, e_index);
}
