/**
 * @author Stuart 
 */

var previousMenuItem = null;
var mainSlide = null;

var fadeVal = 0.2;
var fadeSpeed = "normal"


//Used for boxy overlay window
var box = null;


$(document).ready(function(){

	
	
	
	loadNews();
	
	//doScrolling();
	
	$(window).bind('resize', function() { sizeContent();});
	sizeContent();
	
	if ($("#tv").length > 0 ) {
		
		//Default will be image scrolling
		
		showSlideShow();
		
		
	}
	
	if ($("#scrollable").length > 0) {
		//flashProducts();
		
		
		$(".product").bind("mouseover", function(){
		
			var el = this;
			var c =0;
			$(this).fadeTo("fast", 1.0);
			//$(".product:not(this)")
			
			$(".product").each(function (i) {
				
				if( el != this ){
					$(this).fadeTo("normal", fadeVal);
				}
				
			});

			
		});
	}
	

});

function flashProducts(){
	
	$(".product").animate({ 
        opacity: 0.1
      }, 800, "linear", function(){
	  	
	  		$(".product").animate({ 
        		opacity:1.0
      		}, 800 );
			
	  } );

	
}



function sizeContent(){
	
	var win = $(window).height();
	var doc = $(document).height();
	
	
	
	if( doc > win){
		$(".wrapper").css( "height", doc+"px");
	}else{
		$(".wrapper").css( "height", "100%");
	}
	
	
	if (box != null) {
		x = $(window).width()/2;
		y = $(window).height()/2;
		xpos = x-425;
		box.moveTo(xpos,60 );
		
	}
	
	
	
}


slideShowImgs = new Array();
slideShowImgs[0]="images/slideshow/CDs by Title";
slideShowImgs[1]="images/slideshow/Movies by Genre";
slideShowImgs[2]="images/slideshow/Movies by Year";
slideShowImgs[3]="images/slideshow/Movies by Title";
slideShowImgs[4]="images/slideshow/Movies by Director";


function showSlideShow(){
	
	$(".tvControls a").css("color","#cd8a48");
	$(".imagesLink").css("color","#fff");
	
	data = "<div id='slideShow'>";
	for( i=0;i<slideShowImgs.length;i++){
		smlImg = slideShowImgs[i]+".jpg";
		data+="<img src='../../"+smlImg+"' onclick='showImage("+i+")'/>";
	}
	data+="</div>";
	
	$("#tvScreen").html( data);
							

	$("#slideShow").cycle({
		fx: 'fade',
		speed: 900,
		timeout: 4000
	});
		
}

function showImage( img ){
	var url = slideShowImgs[img]+".jpg";
	
	content = '<img src="../../'+url+'"/>';
	box = new Boxy(content, {title: ".", modal:true, fixed:false, closeable:true});

}


//Used on home page
function showVideo( url,el ){
	
	$(".tvControls a").css("color","#cd8a48");
	$(".tvControls a:eq("+el+")").css("color","#fff");
	
	$("#tvScreen").html("");
	
	$('#tvScreen').flash(
        { src: url,
          width: 453,
          height: 250,
		  loop: true },
        { version: 8 }
    );

}




//loads news stories as XML feed and displays them as a scrolling marquee.
function loadNews(){	
	
	feed = "http://dfsolutions.helpserve.com/rss/index.php?_m=news&_a=view&group=default";

	var feed = new google.feeds.Feed(feed);
      feed.load(function(result) {
        if (!result.error) {
			
	          var container = document.getElementById("feed");
			  
			  var content = "";
			  
	          for (var i = 0; i < result.feed.entries.length; i++) {
		            var entry = result.feed.entries[i];
		            
				content += "<div class='newsItem'>";
					
				content += "<a class='newsTitle' href='../../lib/js/"+entry.link+"'>"+formatDate( entry.publishedDate);
				content += ": "+entry.title+"</a>";
				
				content += "<div class='newsContent'>";
				content += entry.content+"</div>";
				content += "</div>";	
			}

			$('#marquee').html(content);
			
			var marquee = $('#marquee').marquee("marqueeStyle").mouseover(function () {
				$(this).trigger('stop');
			}).mouseout(function () {
				 $(this).trigger('start');
			});
			
			//Force it to resize
			
			$(".marqueeStyle").height(170);
			
			

			//Create JQuery implementation
			/*
			var marquee = $('marquee').marquee().mouseover(function () {
				$(this).trigger('stop');
			}).mouseout(function () {
				 $(this).trigger('start');
			});
*/
			
        }
      });

    

	

}

function formatDate( date ){
	
	//Wed, 03 Sep 2008 00:13:28 -0700
	
	formatted = date.substring(4,16);
	
	
	return formatted;
}


var currentProduct = -1;

var products = new Array();
products[0] = "multiroom_media_server";
products[1] = "video_node";
products[2] = "sonix";


var productNames = new Array();
productNames[0] = "Multiroom Media Server";
productNames[1] = "Video Node";
productNames[2] = "Sonix Audio Server";


var slideSpeed = "slow";


function loadProduct( prodId ){
	
	if( currentProduct != prodId ){
		
		//$(".product:eq("+currentProduct+")").fadeTo( fadeSpeed,fadeVal);
		currentProduct = prodId;
		//$(".product:not(:eq("+currentProduct+"))").fadeTo(fadeSpeed,fadeVal);
		$("#product").fadeOut(slideSpeed, loadPage);
	}
}


function loadPage(){
	
	var url = "products/"+products[currentProduct]+"/main.html";

	$.ajax({
		type: "POST",
		url: url,
		data: "",
		success: function(html){

			$("#product").html(html);
			$("#product").fadeIn(slideSpeed);
			$(".product:eq("+currentProduct+")").fadeTo(fadeSpeed,1.0);
			sizeContent();
		}
	});
	
}

function loadContent( content ){
		
		
	var url = "products/"+products[currentProduct]+"/" + content+".html";
	
	
	$.ajax({
		type: "POST",
		url: url,
		data: "",
		success: function(html){
			//sizeContent();
			$(".productMoreInfo").html(html);
			sizeContent();
		}
	});
}

function moreInfo(){
	
	var url = "products/"+products[currentProduct]+"/description.html";
	
	$.ajax({
		type: "POST",
		url: url,
		data: "",
		success: function(html){
			
			content = '<div class="productMoreInfo">'+html+'</div>';
			box = new Boxy(content, {title: ".", modal:false, fixed:false, closeable:true});
			//pos = box.getPosition();
			//box.moveTo(pos[0],60 );
		}	
	});
}

function boxPicture(src, iwidth, iheight){
        content = '<img src="'+src+'" height="'+iheight+'" width="'+iwidth+'" />';
        box = new Boxy(content, {title: ".", modal:false, fixed:false, closeable:true});
        pos = box.getPosition();
        box.moveTo(pos[0],600 );
}


/**
 * Detects the clients browser and makes some adjustments to styles.
 * Returns
 */
function tweakBrowser(){
}


function doScrolling(){

	var scroll = new Scroller("#marquee");
	
}


function Scroller(id){ 
 this.elId = id; 
	this.scrollAmount = -1;
 this.doScroll = doScroll; 
 this.addItem = addItem; 
	
setInterval(this.doScroll, 1000);
}


function addItem(el){
	alert("item added");
}


function doScroll(){
	

	
	offset = $("#marquee").offset();
	scroll = offset.top - 1;
	
	if( this.scrollAmount == -1 ){
		this.scrollAmount = offset.top;
	}	
	this.scrollAmount = this.scrollAmount -1;
	
	
	$("#marquee").css("top", scroll);
	
	$("#marquee").html(this.scrollAmount);
}
	
	




















