function boxToggle(name) {
	$('#' + name + '_editor').toggle();
	$('#' + name + '_content').toggle();
}

function ajaxLoader(element) {
	src = cms.siteurl + 'images/cms/ajax-loader.gif';
	$('#' + element).html('<div class="ajaxLoaderHolder"><img src="' + src + '"></div>');
}

function showElement(element) {
	$(element).show();
}

function areyousurereturn(msg) {
	if(confirm(msg)) {
		return true;
	}
	
	return false;
}

function actionMessage(msg, success) {

	document.write('<div id="dimmer" class="dimmer" onClick="hideActionMessage()">');
	var width = $('#dimmer').width();
	var left = (width / 2) - 150 + 'px';

	document.write('<div id="actionMessage" class="actionMessage" align="center" style="left: ' + left + ';" onClick="hideActionMessage()">' + (success ? "" : "<span style=\"color: red;\">") + msg + (success ? "" : "</span>") + '<br><br><input type="button" id="okBtn" style="width: auto; border: 1px solid #c1d1d6;" value=" OK "></div>');
	document.write('</div>');
	
	$('okBtn').focus();
}

function hideActionMessage() {
	$('#dimmer').hide();
	$('#actionMessage').hide();
}

/**
 * cms class
 */
cms = {
	
	siteurl: "",
	sitelang: "",
	
	initialize: function() {
		$(document).bind('mousemove', this.mousemoveHandler);
	},

	mousemoveHandler: function (event) {
		this.pos_x = event.pageX;
		this.pos_y = event.pageY;
		
		cms.movePopup(this.pos_x, this.pos_y);
	},
	
	showPopup: function(msg) {
		$('#cmsPopup').html(msg);
		$('#cmsPopup').css( {display: 'block'} );
	},
	
	hidePopup: function() {
		$('#cmsPopup').hide();
	},
	
	movePopup: function(posX, posY) {
		
		// get dimensions of the popup
		var width = $('#cmsPopup').outerWidth();
		var height = $('#cmsPopup').outerHeight();
		
		var popupLeft = posX - (width / 3) + 'px';

		// move popup right if it's too far in the left
		if(posX < 165) {
			var popupLeft = posX - 10 + 'px';
		}

		// move popup left if it's too far in the right
		if(posX + 300 > screen.width) {
			var popupLeft = posX - (width / 2) - 150 + 'px';
		}

		var popupTop = posY + 20 + 'px';

		// move popup underneath if it's too close to the top
		if(posY < 150) {
			var popupTop = posY + 25 + 'px';
		}
		
		// position the popup
		$('#cmsPopup').css( {'left': popupLeft, 'top': popupTop } );
	}
}

cms.initialize();

comments = {

	like: function(cid, value) {

		elemPrefix = (value == 1 ? "like" : "dislike");

		$.ajax({
			type:		'POST',
			url:		location.href,
			data:		'do-action=likecomment&do-cid=' + cid  + '&do-value=' + value,
			success:	function(data) {
							if(data != 1) {
								$('#likestatus-' + cid).html(data);
								return;
							}
							$('#' + elemPrefix + '-' + cid).html(parseInt($('#' + elemPrefix + '-' + cid).html()) + (parseInt(data))); 
						}
		});
		
	},

	submit: function(pid) {

		var data = $('#commentForm').serialize();
		$.ajax({
			type:		'POST',
			url:		location.href,
			data:		'do-action=leaveacomment&' + data,
			success:	function(data) {
							$('#pageComments-' + pid).html(data);
							$('#pageComments-' + pid).animate( {opacity: 1, duration: 500} );
						},
			dataType:	'html'
		});
		
		return;

	}
	
	
}

/** COOKIES **/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/** MOUSE POSITION **/
function mouseX(evt) {
	if(evt.pageX) {
		return evt.pageX;
	}
	else if(evt.clientX) {
	   return evt.clientX + (document.documentElement.scrollLeft ?
							   document.documentElement.scrollLeft :
							   document.body.scrollLeft);
	}
	else {
		return null;
	}
}

function mouseY(evt) {
	if(evt.pageY) {
		return evt.pageY;
	}
	else if(evt.clientY) {
		return evt.clientY + (document.documentElement.scrollTop ?
	  							document.documentElement.scrollTop :
	   							document.body.scrollTop);
	}
	else {
		return null;
	}
}

function refresh() {
	var sURL = unescape(window.location);
	window.location.href = sURL;
}

function isMouseLeaveOrEnter(e, handler)
{		
	if (e.type != 'mouseout' && e.type != 'mouseover') return false;
	var reltg = e.relatedTarget ? e.relatedTarget :
	e.type == 'mouseout' ? e.toElement : e.fromElement;
	while (reltg && reltg != handler) reltg = reltg.parentNode;
	return (reltg != handler);
}

function matchStrings(str1, str2) {
	
	if(str1 == str2) {
		return true;
	}
	else {
		return false;
	}
}

/**
 * serialize array 
 * 
 * @param mixed_value array
 * @return val serialized array
 */
function _serialize( mixed_value ) {
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += _serialize(okey) +
                        _serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") {
      val += ";";
  }
    return val;
}

Array.prototype.unique = function(){
    var vals = this;
    var uniques = [];
    for(var i=vals.length;i--;){
        var val = vals[i];  
        if($.inArray( val, uniques )===-1){
            uniques.unshift(val);
        }
    }
    return uniques;
}

