/**
 * Custom JS for the project. You can delete this file or you can place 
 * your project-related JS code here
 * $Id: cognient.js 58 2011-06-09 16:33:37Z shad $
 */

$(function() {
	Cognient.initialize();
	Ui.initialize();
});

var Cognient = {

	/**
	 * The cached string for the browser.
	 */
	browser: null,
		
	/**
	 * Base context URL
	 */
	baseUrl: '/',	
	
	/**
	 * Static URL
	 */
	staticUrl: '/',
	
	/**
	 * Data Url
	 */
	dataUrl: '/',
	
	/**
	 * Design Url
	 */
	designUrl: '/',
		
	/**
	 * Constructor
	 */
	initialize: function() {
		
	},
	
	/*------------------------------------------------------------------------*/
	/* Generic functions
	/*------------------------------------------------------------------------*/
	
	/**
	 * GoTo specific URL
	 */
	goTo: function(url, base) {
		window.location.href = (base ? Cognient.baseUrl : '') + (url || '');
	},	
	
	/**
	 * Open the link in a new window.
	 * @param node
	 * @return false
	 */
	open: function(node) {
		if (node.href) {
			window.open(node.href);
		}
		return false;
	},
	
	/**
	 * Detect the browser type, based on feature detection and not user agent.	 *
	 * @return string
	 */
	getBrowser: function() {
		if (SynFlex.browser)
			return SynFlex.browser;

		var s = $.support;

		if (!s.hrefNormalized && !s.tbody && !s.style && !s.opacity) {
			if ((typeof document.body.style.maxHeight != "undefined") || (window.XMLHttpRequest))
				SynFlex.browser = 'ie7';
			else
				SynFlex.browser = 'ie6';
		} else if (s.hrefNormalized && s.tbody && s.style && !s.opacity) {
			SynFlex.browser = 'ie8';
		} else {
			SynFlex.browser = 'other';
		}

		return SynFlex.browser;
	},
	
	/**
	 * Register ajax form
	 */
	registerAjaxForm: function(formid, targetid, onsuccessurl) {
		$('#' + formid).ajaxForm({
			dataType:  'json', 
	        beforeSubmit: function() {
	        	$('#form_loader_' + formid).show();
	        },
	        success: function(data) {
	        	$('#form_loader_' + formid).hide();
	        	if (data.status == 'OK') {
	        		SynFlex.goTo(onsuccessurl);
	        	} else if (data.status == 'ERROR') {
	        		$(targetid).html(data.html);
	        	}
	        }
	    });
	},
	
};

/**
 * Ui
 */

var Ui = {
		
	initialize: function() {
		$('div.download-link, .trans-box, .trans-box-round, .login-box-inner button').corner('5px');	    
	    $('.trans-box-title, .login-box-inner').corner('top 5px');	    
	    $('.login-box-inner button, .search-box button, a.prof-link, .back-link-box').corner('5px');
	},
	
	registerToolTip: function(target, position, content) {
		if (position == undefined) {
			position = "tip_right_bottom";
		}
		if (content == undefined) {
			content = $(target).attr('title');
		} else {
			$(target).removeAttr('title');
		}
		$(target).tipTip({
			maxWidth: "200px", 
			defaultPosition: position,
			content: content
		});
	},
	
	registerWarnToolTip: function(target, position, content) {
		if (position == undefined) {
			position = "tip_right";
		}
		if (content == undefined) {
			content = $(target).attr('title');
		} else {
			$(target).removeAttr('title');
		}
		$(target).tipTip({
			maxWidth: "200px", 
			defaultPosition: position,
			content: content
		});
	},

	/**
	 * Shows the spash message
	 */
	showSplash: function(text, title) {
		if (title == undefined) {
			title = 'Notification';
		}
		$('#splashmsg').remove();
		$('body').append('<div class="splash" id="splashmsg"><div class="splash-title-wrapper"><div class="splash-title">' + title + '</div></div><div class="splash-text">' + text + '</div></div>');
		$('#splashmsg').toggle('drop', {}, 500).delay(3000).toggle('slide', {}, 500);	
	},
		
};

/**
 * Methods for creating, reading, and deleting cookies.
 */
var Cookie = {

	/**
	 * Cached cookies.
	 */
	cache: {},

	/**
	 * Create a cookie. Can accept a third parameter as a literal object of options.
	 * @param key
	 * @param value
	 * @param options
	 */
	create: function(key, value, options) {
		options = $.extend({}, options);
		options.expires = options.expires || 1; // 1 hour

		if (typeof options.expires == 'number') {
			var hours = options.expires;
			options.expires = new Date();
			options.expires.setTime(options.expires.getTime() + (hours * 3600000));
		}

		var cookie = [
			encodeURIComponent(key) +'=',
			options.escape ? encodeURIComponent(value) : value,
			options.expires ? '; expires=' + options.expires.toUTCString() : '',
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		];

		document.cookie = cookie.join('');

		if (Cookie.cache) {
			if (options.expires == -1)
				delete Cookie.cache[key];
			else
				Cookie.cache[key] = value;
		}
	},

	/**
	 * Read a cookie.
	 * @param key
	 * @return string
	 */
	read: function(key) {
		// Use cache when available
		if (Cookie.cache[key])
			return Cookie.cache[key];

		var cache = {};
		var cookies = document.cookie.split(';');

		if (cookies.length > 0) {
			for (var i = 0; i < cookies.length; i++) {
				var parts = cookies[i].split('=');

				if (parts.length >= 2)
					cache[$.trim(parts[0])] = parts[1];
			}
		}

		Cookie.cache = cache;
		return cache[key] || null;
	},

	/**
	 * Delete a cookie.
	 * @param key
	 */
	erase: function(key) {
		Cookie.create(key, true, {
			expires: -1
		});
	}
};

/**
 * Prototype overwrites.
 */
String.prototype.trim = function() {
	return $.trim(this);
};
