/********************************/
/** SiteSpider.co.uk :: lib.js **/
/********************************/

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function windowopen(url, title, options)
{
	title = title || "popup";
	options = options || {};

	var toolbar = "toolbar=" + (options.toolbar ? options.toolbar : 'no');
	var location = "location=" + (options.location ? options.location : 'no');
	var directories = "directories=" + (options.directories ? options.directories : 'no');
	var status = "status=" + (options.status ? options.status : 'no');
	var menubar = "menubar=" + (options.menubar ? options.menubar : 'no');
	var resizable = "resizable=" + (options.resizable ? options.resizable : 'yes');
	var scrollbars = "scrollbars=" + (options.scrollbars ? options.scrollbars : 'yes');
	var height = "height=" + (options.height ? options.height : '600px');
	var width = "width=" + (options.width ? options.width : '700px');

	var newwin = window.open(url, title, [toolbar, location, directories, status, menubar, resizable, scrollbars, height, width].join(','));
}

function init_codebox()
{
	if ($('#code_toggle')) {
		$('#code_toggle').toggle();
		$('#code_detail').toggle();
	}
}

function shcode()
{
	$('#code_detail').slideToggle(600);
	if ($('#shlink').html() == "Show" )
		$('#shlink').html("Hide");
	else
		$('#shlink').html("Show");
}

function init_jsshow()
{
	if ($('#jsshow'))
		$('#jsshow').toggle();
}

function changeStyle(s)
{
	var set = -1;

	$('link[@rel*=style][@title]').each(function(i)
    {
		if(this.title == "ACSS" + s.name && this.disabled === true) {
			this.disabled = false;
			s.innerHTML = s.innerHTML + " off";
			set = 1;
		} else if(this.title == "ACSS" + s.name) {
			this.disabled = true;
			s.innerHTML = s.innerHTML.replace(" off", "");
			set = 0;
		}
    });

	$('body').css("display", "none");
	$('body').css("display", "");

	// check for saved display prefs
	if ($.cookie('SiteSpiderUIPrefs')) {
		var prefs = $.cookie('SiteSpiderUIPrefs').split(':')[2];
		var nprefs, nVal;

		if (set == 1)
			nprefs = prefs+ s.name +";";
		else if (set === 0)
			nprefs = prefs.replace(";"+ s.name +";",";");

		nVal = $.cookie('SiteSpiderUIPrefs').replace(":"+ prefs +":", ":"+ nprefs +":");
		if (set > -1)
			$.cookie('SiteSpiderUIPrefs', nVal, { expires: new Date("January 12, 2038"), path: "/", domain: ".sitespider.co.uk" });
	}
}

function loadStyle(disp)
{
	// check for saved display prefs
	var prefs = "";
	if ($.cookie('SiteSpiderUIPrefs'))
		prefs = $.cookie('SiteSpiderUIPrefs').split(':')[2];

    $('link[@rel*=style][@title]').each(function(i)
    {
	    if (this.title.substring(0,4) == "ACSS") {
		    var eID = this.title.replace("ACSS", "");
		    if (prefs.indexOf(";" + eID + ";") > 3)
		    {
		    	this.disabled = false;

		    	if ($('#' + eID))
		    		$('#' + eID).html($('#' + eID).html() + " off");
	    	}
		    else
		    	this.disabled = true;

		    if ($('#' + eID))
		    	$('#' + eID).css("display", disp);
		}
    });

	$('body').css("display", "none");
	$('body').css("display", "");
}

function rssform(enabled)
{
	if (enabled) {
		$('#title').removeAttr("disabled");
		$('#link').removeAttr("disabled");
		$('#description').removeAttr("disabled");
		$('#creator').removeAttr("disabled");
	}
	else {
		$('#title').attr("disabled", "disabled");
		$('#link').attr("disabled", "disabled");
		$('#description').attr("disabled", "disabled");
		$('#creator').attr("disabled", "disabled");
	}
}

function init_rssform()
{
	if ($("input[@name='enablerss']:checked").val() == "no") {
		$('#title').attr("disabled", "disabled");
		$('#link').attr("disabled", "disabled");
		$('#description').attr("disabled", "disabled");
		$('#creator').attr("disabled", "disabled");
	}
}

function init_thickbox()
{
	$('head').append('<link rel="stylesheet" media="screen" href="/css/thickbox.css" type="text/css" />');
	$('head').append('<script type="text/javascript" src="/js/jquery.thickbox.js"></script>');

	tb_init('a.sandbox170134');
	imgLoader = new Image();
	imgLoader.src = tb_pathToImage;
}

function init_diary_datepicker()
{
	$('head').append('<script type="text/javascript" src="/js/date.js"></script>');
	$('head').append('<script type="text/javascript" src="/js/jquery.datepicker.js"></script>');

	$('#datePicker')
		.datePicker({inline:true})
		.bind(
			'dateSelected',
			function(e, dateSelected, $td)
			{
				Date.format = 'dd';
				$('#ExpiryDay').val(dateSelected.asString());
				Date.format = 'dd mm yyyy';
				$('#ExpiryMonth').val(dateSelected.getMonthName());
				$('#ExpiryYear').val(dateSelected.getFullYear());
			}
		);

	$('.datePicker').toggle();
	$('.dp-popup').toggle();
}

function shdatepicker_simple()
{
	var inputDate = new Date();
	inputDate.setYear($('#ExpiryYear').val());
	inputDate.setMonth($('#ExpiryMonth').val() - 1);
	inputDate.setDate($('#ExpiryDay').val());

	$('.dp-popup').slideToggle(600);
	if (inputDate < new Date())
			$('#datePicker').dpSetStartDate(inputDate.asString());

	$('#datePicker').dpSetSelected(inputDate.asString());

	return false;
}

function init_news_datepicker()
{
	$('head').append('<script type="text/javascript" src="/js/date.js"></script>');
	$('head').append('<script type="text/javascript" src="/js/jquery.datepicker.js"></script>');

	$('#datePicker')
		.datePicker({inline:true})
		.bind(
			'dateSelected',
			function(e, dateSelected, $td)
			{
				Date.format = 'yyyy-mm-dd';
				$('#date').val(dateSelected.asString() + " " + $.datepick_adv_time);
			}
		);

	$('.datePicker').toggle();
	$('.dp-popup').toggle();
}

function shdatepicker_adv()
{
	var inputDate = new Date();
	var matches;

	if (matches = $('#date').val().match(/^(\d{4,4})-(\d{2,2})-(\d{2,2}) (\d{2,2}):(\d{2,2}):(\d{2,2})$/)) {
		inputDate.setYear(matches[1]);
		inputDate.setMonth(matches[2] - 1);
		inputDate.setDate(matches[3]);

		$.datepick_adv_time = matches[4] + ":" + matches[5] + ":" + matches[6];
	}

	Date.format = 'yyyy-mm-dd';

	$('.dp-popup').slideToggle(600);
	if (inputDate < new Date())
			$('#datePicker').dpSetStartDate(inputDate.asString());

	$('#datePicker').dpSetSelected(inputDate.asString());

	return false;
}

function del_news_attachment()
{
	document.getElementById("del_att").value = "1";
	$('#newsvattach').hide();
	$('#newsattach').show();
	return false;
}

function del_news_upload()
{
	document.getElementById("del_upl").value = "1";
	$('#newsvupload').hide();
	$('#newsupload').show();
	return false;
}

function del_diary_upload()
{
	document.getElementById("del_upl").value = "1";
	document.getElementById("EventContactLink").value = "";
	$('#diaryvupload').hide();
	$('#diarynupload').show();
	return false;
}

function showdiaryextra(arg)
{
	$('#diarylink').hide();
	$('#diarytarget').hide();
	$('#diarycontact').hide();
	$('#diaryfile').hide();

	if (arg == -1) {
		if ($('#EventContactLink').val().match(/\/diarybin\/[^\/]+\/(.)+/)) {
			arg = 3;
			$('#dif').attr("checked", true);
		}
		else if ($('#EventContactLink').val() != '') {
			arg = 2;
			$('#dil').attr("checked", true);
		}
		else if ($('#EventContact').val() != '') {
			arg = 1;
			$('#dit').attr("checked", true);
		}
		else {
			$('#din').attr("checked", true);
		}
	}

	switch (arg)
	{
	case 1:
		$('#EventContactLbl').html('Contact :');
		$('#diarycontact').show();
		break
	case 2:
		$('#EventContactLbl').html('Link Text :');
		$('#EventContactTargetLbl').html('Open Link In :');
		$('#diarylink').show();
		$('#diarytarget').show();
		$('#diarycontact').show();
		break
	case 3:
		$('#EventContactLbl').html('Link Text :');
		$('#EventContactTargetLbl').html('Open File In :');
		$('#diaryfile').show();
		$('#diarytarget').show();
		$('#diarycontact').show();
		break
  	}
}

function init_newseditor()
{
	if ($('#message')) {
		$("#message").wymeditor({
		skin: 'compact',
		containersHtml: '',
		classesHtml: '',
		toolsItems: [
			{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'},
			{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
			{'name': 'InsertOrderedList', 'title': 'Ordered_List', 'css': 'wym_tools_ordered_list'},
			{'name': 'InsertUnorderedList', 'title': 'Unordered_List', 'css': 'wym_tools_unordered_list'},
			{'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'},
			{'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'},
			{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'},
			{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
			{'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'},
			{'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'},
			{'name': 'ToggleHtml', 'title': 'HTML', 'css': 'wym_tools_html'},
			{'name': 'Preview', 'title': 'Preview', 'css': 'wym_tools_preview'}
		]
		});
	}
}