// JavaScript Document

//Google event tracking
function googleEvent(x,y,z) {
	try {
	pageTracker._trackEvent(x, y, z);
	} catch(err) {
		// console.log('Google tracking is turned off.');
	}
}

function switchAjax(oItemName) {
	var aErrorSet=new Array( // create the default properties in case of failure
						  'selino',
						  'Oops!',
						  'That item could not be loaded at this time. Please choose another project to examine.'
						  );
	$("#scrollmenu").attr("title", "");
	$.getJSON("widgets/switchmenu.php", { name: oItemName}, function(bsInfo){ // ajax call to data
	  $("#beautyShotDescription").hide('slide'); // hide the description
	  $("#beautyShot").fadeOut(function() { // fade first
			var myImg = new Image(); // create the img object
			$(myImg).load(function() { // once the img is loaded
				  switchReal(bsInfo.name,bsInfo.title,bsInfo.description,bsInfo.url,bsInfo.sample_js,bsInfo.sample_css,bsInfo.sample_php);
			   }).error(function () { // if failed to load
				  switchReal(aErrorSet[0],aErrorSet[1],aErrorSet[2]);
			  }).attr('src', '/images/beautyshots/' + bsInfo.name + '.jpg'); // set the img src to url
		  });
	  });
}

function switchReal (name,title,description,url,sample_js,sample_css,sample_php) {
  googleEvent('Scroller', 'Click', name); // Google Analytic Event Tracking
  $("#beautyShot").css('backgroundImage','url(/images/beautyshots/'+name+'.jpg)'); // assign img to div background
  $("#descriptionTitle").html(title); // change the title
  $("#descriptionText").html(description); // change the description
  
  // check for samples
  if (url != null) {
	  $("#descriptionText").append(
  		'<a href="' + url + '" title="visit ' + title + '" target="_blank"><img class="icon-code" src="/casestudy/images/page_house.png" width="16" height="16" alt="Home" /></a>'
	  );
	  //beautyshot clickable
	  $('#beautyShot').unbind('click');
	  $('#beautyShot').bind('click', function() { window.open(url) });
	  $('#beautyShot').attr('title', 'visit ' + title);
	  $('#beautyShot').css('cursor','pointer');
  } else {
	  	  $('#beautyShot').unbind('click');
		  $('#beautyShot').attr('title', title);
  }

  sample_css != null ? $("#descriptionText").append(
  		'<a href="' + sample_css + '" title="review CSS code" target="_blank"><img class="icon-code" src="/casestudy/images/page_css.png" width="16" height="16" alt="CSS" /></a>'
  ) : sample_css;

  sample_js != null ? $("#descriptionText").append(
  		'<a href="' + sample_js + '" title="review Javascript code" target="_blank"><img class="icon-code" src="/casestudy/images/page_code.png" width="16" height="16" alt="Javascript" /></a>'
  ) : sample_js ;
  
  sample_php != null ? $("#descriptionText").append(
  		'<a href="' + sample_php + '" title="review PHP code" target="_blank"><img class="icon-code" src="/casestudy/images/page_white_php.png" width="16" height="16" alt="PHP" /></a>'
  ) : sample_php;
    
  $("#beautyShot").fadeIn(); // fade back in
  $("#beautyShotDescription").show('slide','slow');
}

$(document).ready(function() {
	// initialize scrollables 
    $("div.port_scroll").scrollable({ 
        vertical:true,  
        size: 3,
		speed: 1500,
		clickable: true
    }).mousewheel().circular().autoscroll({
		autoplay: true,
		steps: 1,
		interval: 6000
		});
	
	$("div.scrollable").scrollable({ 
	  size: 4,
	  clickable: false
	}).circular();
	
	// overlay
	$("a[rel]").overlay({ 
 
        expose: 'black', 
        effect: 'apple', 
 
        onBeforeLoad: function() { 
 
            // grab wrapper element inside content 
            var wrap = this.getContent().find(".contentWrap"); 
 
            // load the page specified in the trigger 
            wrap.load(this.getTrigger().attr("href")); 
        } 
    });
	
    setTimeout(function(){$("#wrapper").fadeIn();},2000);
	
	// set default scroller items
	// Hide the feature content
	$('#featureExpanded').hide(); // just in case... yeh, I'm paranoid
	$('#toggleFeature').html('&mdash; &darr; read more &darr;');
		
	// Google Analytic Event Tracking
	$('#historyItem').bind('click', function() { googleEvent('Bottom Items', 'Click', 'History'); });
	$('#toolsItem').bind('click', function() { googleEvent('Bottom Items', 'Click', 'Tools'); });
	//$('#buzzwordItem').bind('click', function() { googleEvent('Bottom Items', 'Click', 'Buzzword Compliance'); });
	$('#casestudyItem').bind('click', function() { googleEvent('Bottom Items', 'Click', 'Case Studies'); });
	$('#musicItem').bind('click', function() { googleEvent('Bottom Items', 'Click', 'Music'); });
	
	//Bind toggle to feature link
	$('#toggleFeature').bind('click', function() {
		if ($('#featureExpanded').is(':visible')) { // check if the feature is showing
			$('#featureExpanded').slideUp(); // hide it if showing
			$('#toggleFeature').html('&mdash; &darr; read more &darr;');
			googleEvent('Links', 'Click', 'Read Less'); // Google Analytic Event Tracking
	  } else { // if the feature is hidden
		  $('#featureExpanded').load('articles/featured.html', function() { // load the html file
			$('#featureExpanded').slideDown(); // show the feature
			$('#toggleFeature').html('&mdash; &uarr; show less &uarr;');
			  googleEvent('Links', 'Click', 'Read More'); // Google Analytic Event Tracking
		  });
	  }
	});
	
	//Bind ajax call to beautyshot thumbnails
	$('.scrollEl img').bind('click', function() {
		switchAjax($(this).attr("alt"));
	});
	
	// set default screen
	switchAjax('selino'); 

}); // end doc ready


