/**
* global.js
*
* Initializes tooltips and lightbox links
*
* @package WordPress
* @subpackage Domena
* @since Domena 1.0
*/

var Tooltips = {
	init: function(id) {
		$tooltip = $('<div class="tooltip" id="tooltip_' + id + '" style="display:none"></div>');
		$('body').append($tooltip);
	},
	
	getTooltip: function(id) {
		return $('#tooltip_' + id);
	},
	
	buildTooltip: function(id, options) {
		var defaults = {
			content: "Sample Tooltip"
		}

		options = $.extend({}, defaults, options);
		this.getTooltip(id).html('<div class="inner">' + options.content + '</div><span class="triangle"></span>');
	},
	
	showTooltip: function(id) {
		var tooltipAnchor = $('#' + id + ' > a');
		var tooltip = this.getTooltip(id);
		var x = $(tooltipAnchor).offset().left + ($(tooltipAnchor).outerWidth() / 2);
		var y = $(tooltipAnchor).offset().top - $(tooltip).height() - 10;

		var css = {
			left: x,
			top: y
		};

		$(tooltip).css(css).fadeIn();
	},
	
	hideTooltip: function(id) {
		this.getTooltip(id).fadeOut();
	}
}

function isMediaQueryActive() {
	if($('#content_wrapper h2:visible').length > 0) {
		return false;
	}else{
		return true;
	}
}

/*
* insertParam
* used for adding "lightbox=true" param to lighbox URL's
* 
* code modified from:
* http://stackoverflow.com/questions/486896/adding-a-parameter-to-the-url-with-javascript
*
*/ 
function insertParam(url, key, value) {
    var key = escape(key); 
	var value = escape(value);
	
	if(url.indexOf('#') > 0) {
		var hash = url.substr(url.indexOf('#'));
	}else{
		var hash = '';
	}
	
    var kvp = url.replace(hash, '').split('&');

    var i=kvp.length; var x; while(i--) {
        x = kvp[i].split('=');

        if (x[0]==key) {
                x[1] = value;
                kvp[i] = x.join('=');
                break;
        }
    }

    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

	kvp = kvp.join('&');

	if(kvp.indexOf('?') == -1) {
		kvp = kvp.replace("&", '?');
	}

    return kvp + hash; 
}

$(document).ready(function() {
	$("a.lightbox").click(function(e) {
		//if the media query is not activated, load lightbox...
		if(!isMediaQueryActive()) {
			var href = $(this).attr('href');

			if(href != '') {
				href = insertParam(href, 'lightbox', 1);
			}

			$.colorbox({iframe:true, width:"420", height:"70%", opacity: 0.5, href: href});
			return false;		
		}		
	});
	
	if($('div.selling-points').length > 0) {
		//initialize tooltips (don't initialize if the admin is in the page layout editor)
		if(typeof(blnEditMode) == "undefined" || blnEditMode == false) {
			$('div.selling-points ul > li > a').each(function(index, element) {
				var $tooltipContentDiv = $(element).parent().find('div.tooltip_content');

				if($tooltipContentDiv.length > 0) {
					var tooltipID = $(element).parent().attr('id');

					Tooltips.init(tooltipID);
					Tooltips.buildTooltip(tooltipID, {
						content: $tooltipContentDiv.html()
					});
				}
			});

			$('div.selling-points ul > li > a').click(function(e) {
				if(!isMediaQueryActive()) {
					return false;
				}
			});
	
			$('div.selling-points ul > li > a').mouseover(function(e) {
				var tooltipID = $(this).parent().attr('id');

				if(tooltipID && !isMediaQueryActive()) {
					Tooltips.showTooltip(tooltipID);
				}
			});
	
			$('div.selling-points ul > li > a').mouseout(function(e) {
				var tooltipID = $(this).parent().attr('id');

				if(tooltipID) {
					Tooltips.hideTooltip(tooltipID);
				}
			});
		}
	}
});
