/*
	Tablecloth by Alen Grakalic
*/

this.tablecloth = function(){

	var highlightCols = false;
	var highlightRows = false;
	var selectable = true;

	var tableover = false;
	this.start = function(){
		var tables = document.getElementsByTagName("table");
		for (var i=0;i<tables.length;i++){
			tables[i].onmouseover = function(){tableover = true};
			tables[i].onmouseout = function(){tableover = false};
			rows(tables[i]);
		};
	};

	this.rows = function(table){
		var css = "";
		var tr = table.getElementsByTagName("tr");
		for (var i=0;i<tr.length;i++){
			css = (css == "odd") ? "even" : "odd";
			tr[i].className = css;
			var arr = new Array();
			for(var j=0;j<tr[i].childNodes.length;j++){
				if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
			};
			for (var j=0;j<arr.length;j++){
				arr[j].row = i;
				arr[j].col = j;
				if(arr[j].innerHTML == "&nbsp;" || arr[j].innerHTML == "") arr[j].className += " empty";
				arr[j].css = arr[j].className;
				arr[j].onmouseover = function(){
					over(table,this,this.row,this.col);
				};
				arr[j].onmouseout = function(){
					out(table,this,this.row,this.col);
				};
				arr[j].onmousedown = function(){
					down(table,this,this.row,this.col);
				};
				arr[j].onmouseup = function(){
					up(table,this,this.row,this.col);
				};
				arr[j].onclick = function(){
					click(table,this,this.row,this.col);
				};
			};
		};
	};

	this.over = function(table,obj,row,col){
		if (!highlightCols && !highlightRows) obj.className = obj.css + " over";
		if(check1(obj,col)){
			if(highlightCols) highlightCol(table,obj,col);
			if(highlightRows) highlightRow(table,obj,row);
		};
	};
	this.out = function(table,obj,row,col){
		if (!highlightCols && !highlightRows) obj.className = obj.css;
		unhighlightCol(table,col);
		unhighlightRow(table,row);
	};
	this.down = function(table,obj,row,col){
		obj.className = obj.css + " down";
	};
	this.up = function(table,obj,row,col){
		obj.className = obj.css + " over";
	};
	this.click = function(table,obj,row,col){
		if(check1){
			if(selectable) {
				unselect(table);
				if(highlightCols) highlightCol(table,obj,col,true);
				if(highlightRows) highlightRow(table,obj,row,true);
				document.onclick = unselectAll;
			}
		};
		clickAction(obj);
	};

	this.highlightCol = function(table,active,col,sel){
		var css = (typeof(sel) != "undefined") ? "selected" : "over";
		var tr = table.getElementsByTagName("tr");
		for (var i=0;i<tr.length;i++){
			var arr = new Array();
			for(j=0;j<tr[i].childNodes.length;j++){
				if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
			};
			var obj = arr[col];
			if (check2(active,obj) && check3(obj)) obj.className = obj.css + " " + css;
		};
	};
	this.unhighlightCol = function(table,col){
		var tr = table.getElementsByTagName("tr");
		for (var i=0;i<tr.length;i++){
			var arr = new Array();
			for(j=0;j<tr[i].childNodes.length;j++){
				if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j])
			};
			var obj = arr[col];
			if(check3(obj)) obj.className = obj.css;
		};
	};
	this.highlightRow = function(table,active,row,sel){
		var css = (typeof(sel) != "undefined") ? "selected" : "over";
		var tr = table.getElementsByTagName("tr")[row];
		for (var i=0;i<tr.childNodes.length;i++){
			var obj = tr.childNodes[i];
			if (check2(active,obj) && check3(obj)) obj.className = obj.css + " " + css;
		};
	};
	this.unhighlightRow = function(table,row){
		var tr = table.getElementsByTagName("tr")[row];
		for (var i=0;i<tr.childNodes.length;i++){
			var obj = tr.childNodes[i];
			if(check3(obj)) obj.className = obj.css;
		};
	};
	this.unselect = function(table){
		tr = table.getElementsByTagName("tr")
		for (var i=0;i<tr.length;i++){
			for (var j=0;j<tr[i].childNodes.length;j++){
				var obj = tr[i].childNodes[j];
				if(obj.className) obj.className = obj.className.replace("selected","");
			};
		};
	};
	this.unselectAll = function(){
		if(!tableover){
			tables = document.getElementsByTagName("table");
			for (var i=0;i<tables.length;i++){
				unselect(tables[i])
			};
		};
	};
	this.check1 = function(obj,col){
		return (!(col == 0 && obj.className.indexOf("empty") != -1));
	}
	this.check2 = function(active,obj){
		return (!(active.tagName == "TH" && obj.tagName == "TH"));
	};
	this.check3 = function(obj){
		return (obj.className) ? (obj.className.indexOf("selected") == -1) : true;
	};

	start();

};

window.onload = tablecloth;



$(document).ready(function(){

    /* Time */

    var get_time = $('input[name=get_time]').attr('value');
    var digital = new Date(get_time);

    //var digital = new Date("Apr, 28 2010 21:34:17");

    $('#clockntp').html(digital.getHours()+":"+digital.getMinutes()+":"+digital.getSeconds());


    setInterval(function() {
        var hours = digital.getHours();
        var minutes = digital.getMinutes();
        var seconds = digital.getSeconds();

        digital.setSeconds( seconds + 1 );

        if (minutes <= 9) minutes = "0" + minutes;
        if (seconds <= 9) seconds = "0" + seconds;

        dispTime = hours + ":" + minutes + ":" + seconds;

        //alert(dispTime);
        //document.getElementById("clockntp").innerHTML = dispTime;
        $('#clockntp').html(dispTime);
        //document.getElementById("clockntp").innerHTML = Math.random();
    }, 1000);

    /* Close page */
    $('#close-schedule').click(function(e) {
        e.preventDefault();

        window.close();

    });



    /* Hints */

    var count_airs = $('input[name=count-airs]').attr('value');
    var bbimg_path = $('input[name=img-path]').attr('value');
    var full_profile_path = $('input[name=full-profile-path]').attr('value');
    var rand = 0;
    var air_name = null;
    var air_djs = null;
    var popup_colors = new Array('azure', 'blue', 'green', 'orange', 'violet', 'yellow', 'grey');
    var dj_data = null;
    var out_data = '';

    function get_random(){
        var ranNum= Math.floor(Math.random()*7);
        return ranNum;
    }

  
    if (count_airs > 0) {

        for (i = 1; i <= count_airs; i++) {

            rand = get_random();

            air_name = $('input[name=data-air'+i+'-name]').attr('value');
            air_djs = $('input[name=data-air'+i+'-djs]').attr('value');
            out_data = '';

            //if (air_djs.indexOf(',') != '-1') {
                dj_data = air_djs.split(',');
                for ($i=0; $i < dj_data.length; $i++) {
                    dj_data_curr = dj_data[$i].split('|||');
                    //alert(dj_data_curr[1]);
                    out_data += ', <a href="'+full_profile_path+dj_data_curr[0]+'" class="dj-popup" target="_BLANK">'+dj_data_curr[1]+'</a>';
                    //alert(dj_data_curr);
                }
                out_data = out_data.substr(2);
                //alert(dj_data.length);
            //}

            $('#air'+i).SetBubblePopup({
                innerHtml: '<div><span style="font-weight: bold;">Эфир:</span> '+air_name+'</div><div><span style="font-weight: bold;">ДиДжеи:</span> '+out_data+'</div>',
                bubbleAlign: 'center',
                tailAlign: 'center',
                imageFolder: bbimg_path,
                color: popup_colors[rand],
                showOnMouseOver: true,
                contentStyle: 'font-family:Arial; font-size: 12px; line-height: 1.2em; background-color: #fff; color: #666;'
            });

        }

    }




});

