$(document).ready(function(){

	$("#Stations div:nth-child(5)").attr('style', 'clear: right;');

	$('#add_mylist').click(function() {
		add = $(this).attr('data-id');

		$.get("../ajax.php", {add: add}, function(data) {

			if (data == 'already')
				alert('התחנה כבר קיימת ברשימה שלך');
			else {
				alert('התחנה נוספה לרשימת התחנות שלך');
				$("#BigList").children("div:nth-child(2)").html(data);
				/*$('#add_mylist').children("img").attr('src', '../images/remove.png');
				$('#add_mylist').prop('id', 'remove_mylist');*/
			}

		});
		return false;
	});

	$('#remove_mylist').click(function() {
		remove = $(this).attr('data-id');

		$.get("../ajax.php", {remove: remove}, function(data) {

			if (data == 'none')
				alert('התחנה אינה קיימת ברשימה שלך');
			else {
				alert('התחנה הוסרה מרשימת התחנות שלך');
				$("#BigList").children("div:nth-child(2)").html(data);
				/*$('#remove_mylist').children("img").attr('src', '../images/add.png');
				$('#remove_mylist').prop('id', 'add_mylist');*/
			}

		});
		return false;
	});

	var NumTiles;
	var animate = 0;
	NumTiles = $("#StationsTiles li").length;
	if($.cookie('ShowedTile') > 1 && $.cookie('ShowedTile') <= NumTiles)
	{
		$("#StationsTiles ul").children("li:nth-child("+$.cookie('ShowedTile')+")").children("img").show();
		$("#StationsTiles ul").children("li:nth-child("+$.cookie('ShowedTile')+")").addClass("active");
		$("#BigList").children("div:nth-child("+$.cookie('ShowedTile')+")").show();
	}
	else
	{
		$("#StationsTiles ul").children("li:nth-child(1)").children("img").show();
		$("#StationsTiles ul").children("li:nth-child(1)").addClass("active");
		$("#BigList").children("div:nth-child(1)").show();
		$.cookie('ShowedTile', "1",{expires: 30});
	}
	
	$("#StationsTiles ul li").click(function(){
		if(animate == 0){
			animate = 1;
			$("#StationsTiles ul li").children("img").hide();
			$("#StationsTiles ul li").removeClass("active");
			$(this).children("img").show();
			$(this).addClass("active");
			$("#BigList").css("height",$("#BigList").css("height"));
			$("#BigList").children("div").hide();
			$("#BigList").children("div:nth-child("+$(this).attr("title")+")").slideDown('fast',function(){animate=0;});
			$.cookie('ShowedTile', $(this).attr("title"),{expires: 30});
		}
	});
	
	var stationsCookie;
	var stcnt = 0;
	sCookie = $.cookie('stationsCookie');
	stationsCookie = explode(",,",sCookie);
	while(stationsCookie[stcnt])
	{
		stationsCookie[stcnt] = str_replace (",", "", stationsCookie[stcnt]);
		$("#Stations").children("div:nth-child("+stationsCookie[stcnt]+")").children(".Links").children(".MoreLinks").show();
		$("#Stations").children("div:nth-child("+stationsCookie[stcnt]+")").children(".NoMRG").children("img").attr("src","/images/more2.jpg");
		stcnt++;
	}
	$(".NoMRG").click(function(){
		if($("#Stations").children("div:nth-child("+$(this).attr("rel")+")").children(".Links").children(".MoreLinks").css("display") == "none")
		{
			$("#Stations").children("div:nth-child("+$(this).attr("rel")+")").children(".Links").children(".MoreLinks").slideDown();
			$("#Stations").children("div:nth-child("+$(this).attr("rel")+")").children(".NoMRG").children("img").attr("src","/images/more2.jpg");
			sCookie = str_replace (","+$(this).attr("rel")+",", "", sCookie);
			sCookie = sCookie + ","+$(this).attr("rel")+",";
			sCookie = str_replace ("null", "", sCookie);
			$.cookie('stationsCookie', sCookie,{expires: 30});
		}
		else
		{
			$("#Stations").children("div:nth-child("+$(this).attr("rel")+")").children(".Links").children(".MoreLinks").slideUp();	
			$("#Stations").children("div:nth-child("+$(this).attr("rel")+")").children(".NoMRG").children("img").attr("src","/images/more.jpg");
			sCookie = str_replace (","+$(this).attr("rel")+",", "", sCookie);
			sCookie = str_replace ("null", "", sCookie);
			$.cookie('stationsCookie', sCookie,{expires: 30});
		}
		return false;
	});
	
	var allcnt = 1;
	
	$("#OpenAll").click(function(){
		allcnt = 1;
		if($(this).text() != "סגור את כל התחנות"){
			$("#Stations").children("div").children(".Links").children(".MoreLinks").slideDown();
			$("#Stations").children("div").children(".NoMRG").children("img").attr("src","/images/more2.jpg");
			while($("#Stations").children("div:nth-child("+allcnt+")").children(".Links").children(".MoreLinks").attr("class") == "MoreLinks")
			{
				sCookie = str_replace (","+allcnt+",", "", sCookie);
				sCookie = sCookie + ","+allcnt+",";
				sCookie = str_replace ("null", "", sCookie);
				allcnt++;
			}
			$(this).text("סגור את כל התחנות");
		}
		else{
			$("#Stations").children("div").children(".Links").children(".MoreLinks").slideUp();
			$("#Stations").children("div").children(".NoMRG").children("img").attr("src","/images/more.jpg");
			sCookie = "";
			$(this).text("פתח את כל התחנות");
		}
		$.cookie('stationsCookie', sCookie,{expires: 30});
		return false;
	});
});

function explode (delimiter, string, limit) {
    var emptyArray = {
        0: ''
    };
 
    // third argument is not required
    if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') {
        return null;
    }
 
    if (delimiter === '' || delimiter === false || delimiter === null) {
        return false;
    }
 
    if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') {
        return emptyArray;
    }
 
    if (delimiter === true) {
        delimiter = '1';
    }
 
    if (!limit) {
        return string.toString().split(delimiter.toString());
    }
    // support for limit argument
    var splitted = string.toString().split(delimiter.toString());
    var partA = splitted.splice(0, limit - 1);
    var partB = splitted.join(delimiter.toString());
    partA.push(partB);
    return partA;
}

function str_replace (search, replace, subject, count) {

    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = Object.prototype.toString.call(r) === '[object Array]',
        sa = Object.prototype.toString.call(s) === '[object Array]';
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

function det_time()
{
var d = new Date();
var c_hour = d.getHours();
var c_min = d.getMinutes();
var c_sec = d.getSeconds();
var t = c_hour + ":" + c_min + ":" + c_sec;
return t;
}
