function show_youtube(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);
        });
    });
} 
function popup_content(_content, _width, _height) {
    if (typeof _width == "undefined")
        $.fn.nyroModalManual({ content: _content });
    else
        $.fn.nyroModalManual({ content: _content, width: _width, height: _height });
}
function popup_url(_url, _width, _height) {
    if (typeof _width == "undefined")
        $.fn.nyroModalManual({ url: _url });
    else
        $.fn.nyroModalManual({ url: _url, width: _width, height: _height });
} 
function NTTTableToFusionChartScroll(caption, sub_caption, tableId, label_col, value_col, chartId, swf_base_url, width, height, ignore_last_row) {
    var header = "<chart 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);
}
function NTTTableToFusionChart(caption, sub_caption, tableId, label_col, value_col, chartId, max_records, swf_base_url, width, height, ignore_last_row) {
    var strXML = "<chart 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);
} 
// 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) {

    $.fn.nyroModalManual(
            {
                type: 'swf',
                height: 355,
                width: 600,
                url: "http://www.youtube.com/v/" + videoid + "?autoplay=1&fs=1&color1=0x5d1719&color2=0xcd311b&border=1"
            });
}
function ShowYoutubeClips(div_element, query, max_results,image_width,image_height) {

    $("#" + 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 + '" onclick="PlayVideo(\'' + videoid + '\'); return false;" />';
            $("#" + div_element).append(html);
        });
        
    });
}

function loadImage(imgObj, imgUrl, imgEmptyUrl) 
{
    var http = false;
    // Mozilla/Safari
    if(imgObj==null || imgObj=="" || imgUrl==null || imgUrl=="" )
   	{
   	    document.getElementById(imgObj).style.display = "none";
   	    return;
   	}

    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    http.open('GET', imgUrl, true);
    http.onreadystatechange = function() 
    {
        if (http.readyState == 4 && http.status == 200)
        {
	        document.getElementById(imgObj).style.display = "block";
	        document.getElementById(imgObj).src = imgUrl;
            http.onreadystatechange = null;
            http = null;     
      }
        else
        {
		    //document.getElementById(imgObj).style.display = "none";
	        document.getElementById(imgObj).src = imgEmptyUrl;
        }
    }
    http.send(null);
}

function NTTAjax(strURL,divid) 
{
    var request = false;
    var self = this;

    displayMessage(divid,"loading...");
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.request = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.request.open('GET', strURL, true);
    self.request.onreadystatechange = function() 
    {
        if (self.request.readyState == 4) 
        {
            displayMessage(divid,self.request.responseText);
            self.request.onreadystatechange = null;
            self.request = null;     
        }
        else
        {
            displayMessage(divid,"");
        }
    }
    self.request.send(null); 
}

/****************************************************
     Author: Eric King
     Url: http://redrival.com/eak/index.shtml
     This script is free to use as long as this info is left in
     Featured on Dynamic Drive script library (http://www.dynamicdrive.com)
****************************************************/
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos)
{
    if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
    if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
    else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
    settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
    win=window.open(mypage,myname,settings);
}
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 createXMLHttpRequest() {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
    try { return new XMLHttpRequest(); } catch(e) {}
    alert("Ajax not supported");
    return null;
}

function displayImage(objId,img_url)
{
    if(document.getElementById(objId))
        document.getElementById(objId).src=img_url;
}
function displayParentImage(objId,img_url)
{
    if(window.parent.document.getElementById(objId))
        window.parent.document.getElementById(objId).src=img_url;
}
function displayMessage(objId,msg)
{
    if(document.getElementById(objId) && msg)
    {
       if(document.getElementById(objId).innerHTML!=msg);
        document.getElementById(objId).innerHTML=msg;
    }
}
function displayParentMessage(objId,msg)
{
    if(window.parent.document.getElementById(objId))
        window.parent.document.getElementById(objId).innerHTML=msg;
}
function showDiv(objId)
{
    if(document.getElementById(objId))
        document.getElementById(objId).style.display="block";
}
function showParentDiv(objId)
{
    if(window.parent.document.getElementById(objId))
        window.parent.document.getElementById(objId).style.display="block";
}
function hideDiv(objId)
{
    if(document.getElementById(objId))
        document.getElementById(objId).style.display="none";
}
function hideParentDiv(objId)
{
    if(window.parent.document.getElementById(objId))
        window.parent.document.getElementById(objId).style.display="none";
}
