/* Element Creative */

function previewMsg() {
	msg = document.comments_form.text.value;
	msg = cleanComment(msg);
	$('previewMessage').update(msg);
	if(msg != "") {
		$('previewMessage').style.display="block";
		}
	else {
		$('previewMessage').style.display="none";
		}	
}

function cleanComment(text) {
	text = text.replace(/<a.*javascript:.*?>/ig, ''); 	
	text = text.replace(/<a.*class=.*?>/ig, ''); 	
	text = text.replace(/<a.*style=.*?>/ig, ''); 	
	text = text.replace(/<a.*on\w+=.*?>/ig, ''); 	
	text = text.replace(/<i.*class=.*?>/ig, ''); 	
	text = text.replace(/<i.*style=.*?>/ig, ''); 	
	text = text.replace(/<i.*on\w+=.*?>/ig, ''); 	
	text = text.replace(/<b.*class=.*?>/ig, ''); 	
	text = text.replace(/<b.*style=.*?>/ig, ''); 	
	text = text.replace(/<b.*on\w+=.*?>/ig, ''); 	
	text = text.replace(/<img.*?>/g, ''); 	
	text = text.replace(/<(?!a.*?|\/a|b|\/b|i|\/i).*?>/ig, ''); 

	//text = autoLink(text);

	var pars = text.split(/\r?\n\r?\n/);
	var newtext = ''; 
	for(i = 0; i < pars.length; i++) {
		pars[i] = pars[i].replace(/\r?\n/g, '<br />\n'); 	
		newtext += '<p>\n' + pars[i] + '</p>\n';
	}
	newtext = cleanQuotes(newtext);
	return newtext;
}

function autoLink(text) {
    text = text.replace( /(^|\s+)(http:\/\/\S+)/igm, '$1<a href="$2">$2</a>');
    var amatches = text.match(/>(.*?)<\/a>/g);
    if(amatches) {
        for(i = 0; i < amatches.length; i++) {
            var href = amatches[i];
            if(href.length > 60) {
                var nhref = href.substr(0, 59);
                var index = text.indexOf(href);
                var stext = text.substr(0, index);
                var etext = text.substr(index + href.length, text.length);
                text = stext + nhref + "...</a>" + etext;  
            }
        }
    }
    return text;
}

function cleanQuotes(text) {
    // clean smart chars and such...
    // 8216, 8217, 8220, 8221, 8212, 8211
    var chars = text.split("");
    var newtext = '';
    for(i = 0; i < chars.length; i++) {
        var chr = chars[i].charCodeAt(0);
        switch( chr ) {
            case 8216: newtext += "'"; break;
            case 8217: newtext += "'"; break;
            case 8220: newtext += '"'; break;
            case 8221: newtext += '"'; break;
            case 8211: newtext += "-"; break;
            case 8212: newtext += "-"; break;
            default: newtext += chars[i];
        }
    }
    return newtext;
}

function loadSpinner(id, bg) {
	var flashvars = {};
	var params = {bgcolor: bg};
	var attributes = {};
	attributes.id = "spinner";
	attributes.align = "middle";
	swfobject.embedSWF("/_resources/loader.swf", id, "46", "42", "8.0.0", false, flashvars, params, attributes);
	}

var results = "";
var resultState = "hidden";
function testMe() {
	alert("Hello!");
	}

function kwSearch() {
	kw = document.forms['searchFrm'].search.value;
	doSearch(kw,'search');
	}

function loadResults() {
	$('searchResults').replace('<div id="searchResults">' + results + '</div>');
	}

function showLoading() {
	$('searchResults').replace('<div id="searchResults"><div id="spin">Loading...</spin></div>');

	loadSpinner('spin', "#eeeeee");		
	
	if(resultState == "hidden") {
		new Effect.Appear('srWrap', {from: 0.0, to: 1.0, duration: 0.2});
		resultState = "visible";
		}
	}

function showResults() {
	//new Effect.Appear('searchResults', {from: 0.0, to: 1.0, duration: 0.5, afterFinish: loadResults});
	$('searchResults').replace('<div id="searchResults">' + results + '</div>');
	}

function hideResults() {
	$('searchResults').replace('<div id="searchResults"></div>');
	new Effect.Fade('srWrap', {from: 1.0, to: 0.0, duration: 0.2});
	resultState = "hidden";
	}

function doSearch(query,type) {
	new Ajax.Request('/_resources/search.php?type='+type+'&query='+query, {
 		onLoading: showLoading(),
 		onSuccess: function(transport){
     		results = transport.responseText;
			showResults();
			}
		});
	}

/* ===== Start Image Slideshow ===== */

var ssNum;
var ssPrev;
var ssNext;
var ssTot;
var ssPhotos;
var ssPath = ""; // Optional

// Pre-load images
if(typeof ssPhotos != "undefined") {
	for(p=0;p<ssPhotos.length;p++) {
		newPhoto = "photo"+p;
		newPhoto = new Image();
		newPhoto.src = ssPath+ssPhotos[p];
		}
	}
	
function ssCurr(ssNum) {
    ssNum = parseInt(ssNum);
    ssPrev = ssNum-1;
    ssNext = ssNum+1;

    if(ssPrev < 0) {
        ssPrev = ssTot-1;
        }
    if(ssNext > ssTot-1) {
        ssNext = 0;
        }

    $('ssLgImg').setAttribute('src',ssPath+ssPhotos[ssNum]);
    $('ssCaption').replace('<span id="ssCaption">'+ssCaptions[ssNum]+'</span>');
    $('dispSSCurr').replace("<span id='dispSSCurr'>" + (ssNum+1) + " of " + ssTot + "</span>");
    }

function ssShow(ssNum) {
	if(!ssNum) {
		ssNum = 0;
		}
    $('ssWrap').style.display="block";
    ssCurr(ssNum);
    }
    
function ssHide() {
    $('ssWrap').style.display="none";
    }    

function ssPrevImg() {
	ssShow(ssPrev);
    }    
    
function ssNextImg() {
	ssShow(ssNext);
    }

/* ===== End Image Slideshow ===== */

function isloggedin() {
    var u = mtGetUser();
    var loggedin = u && u.is_authenticated && u.is_author ? true : false;
    var eid = 'logged_in';
    conditional_block(loggedin, eid);
    if (!loggedin) {
        var p = $('login_message');
        if (!p) return;
        if (u && !u.is_author) 
            p.innerHTML = "In order to comment you must first register.";
        else
            if (u && !u.can_post)
                p.innerHTML = "You do not have permission to post.";
            else
                p.innerHTML = '<a href="javascript:void(0)" onclick="return mtSignInOnClick(\'login_message\')">Sign in to comment.</a>';
    }
	}

function portOver(id) {
	$(id).toggle();
	}

function portOut(id) {
	$(id).toggle();
	}
	
var arrQuestions = new Array();
arrQuestions[0] ="Do you build websites?";
arrQuestions[1] ="Really? You seem pretty small. Are you sure you're a full-service company that can design, architect, write, and develop user experiences?";
arrQuestions[2] ="We already have a great-looking site, but our copy is atrocious. Can you write it for me?";
arrQuestions[3] ="We have good content on our site, but it looks <em>so</em> 1997. Can you redesign it?";
arrQuestions[4] ="I'd like to add a form to my site so that I can get some basic information from new customers before I call them. Is that possible?";
arrQuestions[5] ="I want my customers to Google my company and find me right away. Got any ideas?";
arrQuestions[6] ="Do you guys do e-Commerce?";
arrQuestions[7] ="We have new employees (products, services, updates, press releases, etc.) every week, so I need to easily update the site myself. Is that possible?";
arrQuestions[8] ="Can you build me a new content management system that will actually do what I need it to?";
arrQuestions[9] ="I keep hearing about &#8220;social networking&#8221; and &#8220;community building&#8221; online. Could you come in and explain it to me - and my marketing staff?";
arrQuestions[10] ="I have an idea for an online business, and don't know where to begin. Could we get together and brainstorm?";

var c = 1;

function loadQuestion() {
	$('question').replace('<span id="question">' + arrQuestions[c] + '</span>');
	new Effect.Appear("question-fade", {from: 0, to: 1, duration: .3}); 
	}

function switchQuestion() {
	new Effect.Fade("question-fade", {from:1, to:0, duration: .3, afterFinish:loadQuestion});

	c++;
	if(c == arrQuestions.length) {
		c = 0;
		}
	}

function initQuestion() {
	new PeriodicalExecuter(switchQuestion, 5);
	}
	
function initQuestionIE() {
	alert("hello, IE");
	}	

/*
function getTweets() {
	getTwitters('element-tweets', { 
		id: 'elementcreative', 
		count: 8, 
		enableLinks: true, 
		ignoreReplies: true, 
		clearContents: true,
		template: '%text% <p><a href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a></p>'
		}
	*/	