// change $ to jQuery so it works with Prototype
jQuery.noConflict();

// IDEA SCALE OPTIONS
var hotIdeaThreshold = 2; // an idea must have this many votes to be considered "hot"
var numOfIdeasToDisplay = 3; // display this many ideas on the labs homepage

// get JSON response, parse and render on page
function ideaScale() {
	jQuery.getJSON("http://api.ideascale.com/akira/api/ideascale.ideas.getTopIdeas?apiKey=b2683878-b53e-401d-9d08-75151ccc2c81&jsoncallback=?&callback=?",
        function(data){
  			var numOfIdeas = 0;
  			var numOfHotIdeas = 0;
  			
          jQuery.each(data.response.ideas, function(i,idea){
          	
          	ideaTextShort = idea.text;          
            ideaTextShort = (ideaTextShort.length > 110 ? ideaTextShort.substr(0,110) + "..." : ideaTextShort);

			// only print the first 3 ideas
		   if ( i < numOfIdeasToDisplay ) jQuery("div#ideas").append("<div class=\"idea\"><span class='votes'><span><a href='" + idea.url + "'>" + idea.voteCount + "</span><br /> votes</a></span><a class='votebutton' href='" + idea.url + "'></a>\n <a href=\"" + idea.url + "\"><strong>" + idea.title + "</strong></a><p class='from'>by " + idea.author + "</p>\n <p class='ideatext'>" + ideaTextShort + "</p><div class='clear'></div></div>");
		   	
            numOfIdeas++;
            if (idea.voteCount >= hotIdeaThreshold) numOfHotIdeas++;
            
        });
        jQuery("div#ideas").append("<div id='ideaadditional'><a href='http://ideas.garfieldgroup.com' target='_blank'>All Ideas (" + numOfIdeas + ")</a> | <a href='http://ideas.garfieldgroup.com' target='_blank'>Hot Ideas (" + numOfHotIdeas+ ")</a> | <a href='http://ideas.garfieldgroup.com' target='_blank'>Submit Your Idea</a></div>");
    });
}

var newwin;
function launch(winurl,winname,winfeatures){
	newwin = window.open(winurl,winname,winfeatures);
}

function PictoBrowser() {
	var flickrid = jQuery("div#pictobrowser div#flickrid").text();
	var pbflashvars = {
		source: "sets",
		//names: "DIA", we dont need this for this functionality
		userName: "ggiagency",
		userId: "32481526@N07",
		ids: flickrid,
		titles: "on",
		displayNotes: "off",
		thumbAutoHide: "off",
		vAlign: "mid",
		vertOffset: "0",
		colorHexVar: "EEEEEE",
		initialScale: "off",
		imageSize: "medium",
		bgAlpha: "100"		
	};
	var pbparams = {
		wmode: "transparent"
	};
	var pbattributes = false;
	if(typeof swfobject != 'undefined'){
		swfobject.embedSWF("http://www.db798.com/pictobrowser.swf", "flickrid", "350", "500", "9.0.0","expressInstall.swf", pbflashvars, pbparams, pbattributes);
	}
	jQuery("div#pictobrowser").append("<span>&nbsp;</span>");
}

// ************************************************************************************************
jQuery(document).ready(function () {
	if(jQuery().easySlider){ jQuery("#labs_latest_scroller").easySlider(); }
	jQuery("div#header ul").addClass("rep");
	jQuery("div#main").addClass("rep");
	jQuery("h2.grey:contains(\"The Lab\")").click(function() {
		document.location.href="/thelab";
	});
	jQuery("h1.grey:contains(\"The Lab\")").click(function() {
		document.location.href="/thelab";
	});
	FLIR.init();
	FLIR.replace( 'div#header ul li a', new FLIRStyle({ cFont:'garfont' }) );
	FLIR.replace( 'div#main h2', new FLIRStyle({ cFont:'garfont' }) );
	FLIR.replace( 'div#main h1', new FLIRStyle({ cFont:'garfont' }) );
	FLIR.replace( 'table#homeBlogLinkTable span', new FLIRStyle({ cFont:'garfont' }) );
	FLIR.replace( 'div#PageComments_holder h4', new FLIRStyle({ cFont:'garfont' }) );
	if(typeof swfobject != 'undefined'){
		swfobject.embedSWF("/homeFlash.swf", "homepage_flash", "763", "345", "9.0.0","expressInstall.swf");    
		swfobject.embedSWF("/MillenialMain.swf", "careersheader", "763", "300", "9.0.0","expressInstall.swf");
	}
	PictoBrowser();
	
	initSignupForm();
//	adjustLayoutForLabBug();
});

function initSignupForm(){
//text in signup box
	var default_value = 'Your Email Address'
	jQuery("#Form_Form_Email")
		.val(default_value)
		.focus(function(){
			if(this.value == default_value){ //clear if default
				this.value = '';
			}
		})
		.blur(function(){
			if(this.value==''){
				this.value = default_value;
			}
		})
	;
//ajax submission
	var options = { 
        beforeSubmit:  validateEmail,  // pre-submit callback 
        success:       showThankYou  // post-submit callback 
    }; 
 // bind form using 'ajaxForm' 
    jQuery('#Form_Form','div#footer').ajaxForm(options);
}

function validateEmail(formData, jqForm, options){
	var email = formData[1].value;
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Please enter a valid email address')
		return false;
	}
	//email good
	jQuery('#Form_Form #Form_Form_Email','div#footer').hide().siblings('img').show();
	return true;
}
function showThankYou(responseText, statusText, xhr, $form){
	jQuery('#Form_Form img, input.action','div#footer').hide().siblings('span#response-msg').html(responseText).fadeIn(1000);
	if(jQuery('span#response-msg').html().indexOf("Oops")!=-1){
		jQuery('span#response-msg').css('background','red');
	}
	
}
//================no longer in use====================
function adjustLayoutForLabBug() {
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && document.documentElement.clientWidth ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && document.body.clientWidth) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	
	// minimum inner width safe for bug: 1122px
	if (myWidth < 1122) {
		// move logo left 54px;
		jQuery('div#header h1').css('position','relative').css('left','-54px');
		jQuery('div#header div.logoHolder').css('position','relative').css('left','-54px');
	}
}
//======END no longer in use==============

function processIdeaVoteUp(voteUrl, ideaId) {
	jQuery.cookie('ideavote_' + ideaId, 'true', {path: '/'});
	jQuery.post(voteUrl, function(data){jQuery('div#voteCountUp_idea' + ideaId).html(data)});
	jQuery('div#voteContainer_idea' + ideaId).css('display','none');
	jQuery('div#voteThankYou_idea' + ideaId).css('display','block');
}

function processIdeaVoteDown(voteUrl, ideaId) {
	jQuery.cookie('ideavote_' + ideaId, 'true', {path: '/'});
	jQuery.post(voteUrl, function(data){jQuery('div#voteCountDown_idea' + ideaId).html(data)});
	jQuery('div#voteContainer_idea' + ideaId).css('display','none');
	jQuery('div#voteThankYou_idea' + ideaId).css('display','block');
}
