var cookieStations = '';

function save_stations(pipe) {
    cookieStations = pipe;
  	if (absolute_path == undefined) {
  		absolute_path = '/includes/ajax/';
  	}
    if (ajaxRequest != undefined) {
        ajaxRequest.abort();
    }
    //alert(absolute_path);
    ajaxRequest = $.get(absolute_path + 'ajax.php?cmd=updateStations&pipe='+pipe,
        {
            pipe: pipe
        },
        function(data)
        {
            $("div#station_list").html(data);
            var date = new Date();
            date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
            $.cookie('stations_tv', pipe, { expires: date, path: '/' });
        }
     );

}

function add_to_stations(station_id, pos) {

    var stations_array = new Array();

    if (cookieStations == '') {
        stations_array = splitPipe($.cookie('stations_tv'));
    }else {
        stations_array = splitPipe(cookieStations);
    }

    if (pos >= stations_array.length) {
        pos = stations_array.length+1;
    }
    if (pos < 1) {
        pos = 1;
    }

    var station_pos = 0;
    var alreadyInArray = false;
    for (var i=0; i<stations_array.length; i++) {
        if (station_id == stations_array[i]) {
            alreadyInArray = true;
            station_pos = i+1;
            break;
        }
    }
    if (alreadyInArray && station_pos > 0) {
        stations_array.splice(station_pos-1, 1);
    }

    stations_array.splice(pos-1, 0, station_id);
    var pipe = stations_array.join("|");

    save_stations(pipe);

}

function remove_from_stations(station_id) {

    var stations_array = new Array();

    if (cookieStations == '') {
        stations_array = splitPipe($.cookie('stations_tv'));
    }else {
        stations_array = splitPipe(cookieStations);
    }

    var station_pos = 0;
    for (var i=0; i<stations_array.length; i++) {
        if (station_id == stations_array[i]) {
            station_pos = i+1;
            break;
        }
    }
    if (station_pos > 0) {
        stations_array.splice(station_pos-1, 1);
    }

    var pipe = stations_array.join("|");

    save_stations(pipe);

}

function reload_timeview_shows(timestamp) {
    $("div#loaderdiv").show();
    if (ajaxRequest != undefined) {
        ajaxRequest.abort();
    }
    ajaxRequest = $.get(absolute_path + 'ajax.php?cmd=reloadTimeviewShows',
        {
            timestamp: timestamp
        },
        function(data)
        {
            $("div#loaderdiv").hide();
            $("div#programm_content").html(data);
        }
    );
}

function reload_compactview_shows(timestamp) {
    var limit;
    var set;
    if (arguments[1] != 'undefined') {
        limit = arguments[1];
    }else {
        limit = 0;
    }
    if (arguments[2] != 'undefined') {
        set = arguments[2];
    }else {
        set = false;
    }

    $("div#loaderdiv").show();
    if (ajaxRequest != undefined) {
        ajaxRequest.abort();
    }
    ajaxRequest = $.get(absolute_path + 'ajax.php?cmd=reloadCompactviewShows',
        {
            timestamp: timestamp,
            limit: limit,
            set: set
        },
        function(data)
        {
            $("div#loaderdiv").hide();
            $("div#programm_content").html(data);
        }
    );
}

function reload_scopeview_shows(timestamp, scope) {
    $("div#loaderdiv").show();
    $.cookie('tv_default_scope', scope, { expires: 365, path: '/', domain: '', secure: false });
  	if (absolute_path == 'undefined') {
  		absolute_path = '/includes/ajax/';
  	}
    if (ajaxRequest != undefined) {
        ajaxRequest.abort();
    }
    ajaxRequest = $.get(absolute_path + 'ajax.php?cmd=reloadScopeviewShows',
        {
            timestamp: timestamp,
            scope: scope
        },
        function(data)
        {
            $("div#loaderdiv").hide();
            $("div#programm_content").html(data);
        }
    );
}

/*function download_asset(id) {
	if (absolute_path == undefined) {
		absolute_path = '/includes/ajax/';
	}
	if (ajaxRequest != undefined) {
		ajaxRequest.abort();
	}
	ajaxRequest = $.get(absolute_path + 'ajax.php?cmd=downloadAsset',
		{
			id: id
        },
        function(data)
        {
			console.log(data);
		}
	);

}*/

