﻿var objField,objCountField,objCountFieldText;

function SwapVideo(videoId)
{
    var VideoCode = "<div style='height:350px; width:425px; text-align:center;'><object width='425' height='350'><param name='movie' value='http://www.youtube.com/v/" + videoId +
                    "'></param><embed src='http://www.youtube.com/v/" + videoId + "' type='application/x-shockwave-flash' width='425' height='350'></embed></object></div>";
    var VideoContainer = document.getElementById("MainVideo");
    
    VideoContainer.innerHTML = VideoCode;
}
function inputCounter(field, countfield, maxlimit, action) {
    
	if (!objField) objField = document.getElementById(field);
	if (!objCountField) objCountField = document.getElementById(countfield);
	if (!objCountFieldText) objCountFieldText = document.getElementById(countfield).childNodes[0]; 

	if (objField.value.length > maxlimit){ 
            objCountFieldText.nodeValue = "Text box is full";
            objField.value = objField.value.substring(0,maxlimit);
	    }
	    else
	    {
	        objCountFieldText.nodeValue = maxlimit + " chars max";
	    }
}
function setPos(obj){
    var rng   =   obj.createTextRange();  
        rng.moveStart("character",obj.value.length);  
        rng.moveEnd("character",0);  
        rng.select();
}
function insertAtCursor(myField, myValue) {
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
    }
    else {myField.value += myValue;}
}
function stripTags(s){
	s = s.replace(/<br \/>/gi,"\r");
	s = s.replace(/<br\/>/gi,"\r");
	s = s.replace(/<br>/gi,"\r");
	s = s.replace(/<i>/gi,"");
	s = s.replace(/<\/i>/gi,"");
	s = s.replace(/<b>/gi,"");
	s = s.replace(/<\/b>/gi,"");
	s = s.replace(/<blockquote>/gi,"[quote]");
	s = s.replace(/<\/blockquote>/gi,"[/quote]");
	s = s.replace(/<p>/gi,"");
	s = s.replace(/<\/p>/gi,"");
	s = s.replace(/<a[\s]*target=\"_blank\"[\s]*rel=\"nofollow\"[\s]*href=\"[\w.\?=_\-\/:]+\">/gi,"");
	s = s.replace(/<\/a>/gi,"");
	s = s.replace(/<img[\s]*class=\"Thumb\"[\s]*src=\"([^\"]*)*\"[^>]*>/gi,"[img]$1[/img]");
    s = s.replace(/<.*\/>/gi,"");
    s = s.replace(/<.*>/gi,"");
    s = s.replace(/<\/.*>/gi,"");
    s = s.replace(/[\n]+/gi,"\r");
    while(s != s.replace(/\r[\s]*\r/gi,"\r")){
        s = s.replace(/\r[\s]*\r/gi,"\r");
    }
    return s;
}
function revertTags(s){
	s = s.replace(/\[b\]/gi,"<b>");
	s = s.replace(/\[i\]/gi,"<i>");
	s = s.replace(/\[B\]/gi,"<b>");
	s = s.replace(/\[I\]/gi,"<i>");
	s = s.replace(/\[\/b\]/gi,"</b>");
	s = s.replace(/\[\/i\]/gi,"</i>");
	s = s.replace(/\[\/B\]/gi,"</b>");
	s = s.replace(/\[\/I\]/gi,"</i>");
	s = s.replace(/\[quote\]/gi,"<blockquote>");
	s = s.replace(/\[\/quote\]/gi,"</blockquote>");
	s = s.replace(/\r/gi,"<br>");
	s = s.replace(/\[hr\]/gi,"<hr>");	
	s = s.replace(/\[img\]([^\"]*)\[\/img\]/gi,"<img class=\"Thumb\" src=\"$1\" />");
    return s;
}
function Quote(username,contentId, textBoxId){
    var objTextBox = document.getElementById(textBoxId);
    var content = document.getElementById(contentId).innerHTML;
    content = stripTags(content);
    var quote = "[quote][b]" + username + " said:[/b] " + content + "[/quote]\r";
    objTextBox.value = objTextBox.value + quote;
    objTextBox.focus(); 
}
function resizePictures(){
    var ForumPane = document.getElementById('MWForumPosts');
    var images = ForumPane.getElementsByTagName('img');
    for (var i=0; i<images.length; i++){
	    var img = images[i];
		reSize(img,500,500);
    }
}
function reSize(myImg, maxWidth, maxHeight)
{
	var iniPicWidth = myImg.width;
	var iniPicHeight = myImg.height;
	var iniRate = iniPicHeight/iniPicWidth;
	if(iniPicWidth>maxWidth || iniPicHeight>maxHeight){
		var newW = maxWidth;
		var newH = Math.floor(newW*iniRate);
		if (newH>maxHeight)
		{
			var newH = maxHeight;
			var newW = Math.floor(newH/iniRate);
		}
		myImg.width = newW;
		myImg.height = newH;
	}
}

/*************** Actions *******************/
function doVote(m){mode = m;votePars = votePars + '&m=' + mode;$.ajax({url: "/ajax/VoteHandler.ashx",data:votePars,success:onVoteSuccess})}
function onVoteSuccess(txt){
    var result = txt;
    var lbVoteError = $('#lbVoteError'); // error message
    var voteAction = $('#voteAction');
    if(result == 'invalid_params')
        lbVoteError.html("invalid params");
    else if(result == 'update_err')
        lbVoteError.html("updating data error:(");
    else if(result == 'has_voted'){
        lbVoteError.html("");
        voteAction.hide();
    }
    else{
        $('#voteAction').slideUp('slow');
        if(mode == 'love'){
            $("#loveEffect").animate( { fontSize:"20px",padding:"10px",border:"5px dashed #f01485"}, 1000 );
            litLove.text(result);
        }
        else{
            $("#hateEffect").animate( { fontSize:"20px",padding:"10px",border:"5px dashed #6b6b6b"}, 1000 );
            litHate.text(result);
        }
    }
}
function FormatQuote(s){
    s = s.replace(/\"/gi,"\'");
    return s;
}
function InsertImage(src, alt, objId){
    var img = '<div class="show"><img alt="'+FormatQuote(alt)+'" class="thumb" src="' + src +'" /></div>';
    document.getElementById(objId).value = document.getElementById(objId).value + "\n" + img;
}

