function smilies(text) {
	if(($("textarea[name='message']").val().length+text.length+1)<=200){
        $("#createMessage textarea").eq(0).val($("#createMessage textarea").eq(0).val()+ ' ' + text);
        charactersLeft();
    }
}    

function createMessage(){
    $("div.adBanner").hide();
    $("div.adZone").hide();
    $('body').append('<div id="popupOverlay"></div>');	
	// Get page sizes
	var arrPageSizes = ___getPageSize();
	// Style overlay and show it
	$('#popupOverlay').css({
		backgroundColor:	'#000',
		opacity:			0.8,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn();

    if($(this).hasClass("forceVote")){
        $("#createMessage div.actualVote").eq(0).html($(this).html());
        $("#createMessage input[name='note']").val($(this).html());
    } else if (window.location.hash) {
        if (window.location.hash.indexOf('vote') != -1) {
            
            var peices = window.location.hash.split(/_/);
            if (peices.length > 1 && peices[1] >=  1 && peices[1] <=  10) {
                $("#createMessage div.actualVote").eq(0).html(peices[1]);
                $("#createMessage input[name='note']").val(peices[1]);
            } // if
        } //if  
    }
    
    centerPopup();
    $("#createMessage").show("slow",function(){
        $("div#createMessage div.customBoxContent form").css("visibility","visible");
    });
    return false;
}

function limitTextArea(evt){
    var keynum = (window.event) ? evt.keycode : evt.which;
    switch(keynum){
        case  8://backspace
        case  9://tab
        case 33://page up
        case 34://pagedown
        case 35://end
        case 36://home
        case 37://left arrow
        case 38://up arrow
        case 39://right arrow
        case 40://down arrow
        case 45://insert
            return true;
        default:
            if($("textarea[name='message']").val().length>=200){
                return false;
            }        
    }
}

function charactersLeft(){
    //alert($("textarea[name='message']").val());
    $("span.charLeft").html(200-$("textarea[name='message']").val().length);
}

$(document).ready(function(){
    $("div.readMessage a.reply").click(createMessage);
    $("#createMessage").click(function(){
        $("div.messageBubble").remove();
    });
    $("#createMessage a.send").click(sendForm);
    $("#createMessage a.cancel").click(function(){
        $("div.messageBubble").remove();
        $("#popupOverlay").remove();
        $("#createMessage").hide("slow");
        $("div.adBanner").show();
        $("div.adZone").show();
        $("div#createMessage div.customBoxContent form").css("visibility","hidden");
        return false;
    });        
    $("#createMessage").hide();
    $("a.vote").click(function(){
        $("div.actualVote").eq(0).html($(this).html());
        $("input[name='note']").val($(this).html());
        $("a.vote").each(function(){
           $(this).parent().removeClass("selected");
        });
        $(this).parent().addClass("selected");
    });
    $("textarea.vipOnly").keyup(charactersLeft);
    $("textarea.vipOnly").keydown(limitTextArea);
    $("input.vipOnly").change(invalidateCheckbox);
});


//centering popup  
function centerPopup(){  
    //request data for centering  
    var windowWidth = document.documentElement.clientWidth;  
    var windowHeight = document.documentElement.clientHeight;  
    var popupHeight = $("#createMessage").height();  
    var popupWidth = $("#createMessage").width();  
    var top = windowHeight/2-popupHeight/2;
    var width = null;
    
    if($("div.contentRight").length){
        width = $("div.contentLeft").width();
    }else{
        width = $("div.mainContent").width();
    }
    
    top = (windowHeight/2)-(popupHeight/2)+$(document).scrollTop();
    
    if(top<260){
        top = 260;
    }
    //centering
    $("#createMessage").css({"position": "absolute",
                             "top": top,  
                             "left": windowWidth/2-popupWidth/2,
                             "z-index" : 95
    });  
    //only need force for IE6          
    $("#createMessage").css({  
        "height": windowHeight
    }); 
    
    $("#createMessage").css({"height": popupHeight});
}

function invalidateCheckbox(){
    this.checked=false;
}

/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,(pageHeight+yScroll/2),windowWidth,windowHeight);
	return arrayPageSize;
};

function sendForm(){
    if($("input[name='note']").val().length==0){
        $("div.errorMessages").html(dylg.dic.section_messaging_create_errors['noVote']);
        return false;
    }else if($("textarea[name='message']").val().length==0){
        $("div.errorMessages").html(dylg.dic.section_messaging_create_errors['noMessage']);
        return false;
    }
    $("form[name='message']").get(0).submit();
    return false;
}