/*
 * 	Micronano Studio layout 1.2
 *	written by Dragan Micic
 *	Author URI: http://www.bubo-design.com
 *	Author URI: http://www.micronanostudio.com
 *	Author e-mail: d.micic@micronanostudio.com
 *	Copyright (c) 2011 Dragan Micic & Micronanostudio.com
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Written for jQuery library
 *	http://jquery.com
 *
 */
// summary:When DOM is ready loads scripts
$(document).ready(function(){	
	PageStart();
});
// summary:loads scripts 
var timeout;
function PageStart()
{
	SetLayout();WindowResize();ShowRemote();ShowRemotee();
	window.setTimeout("ChangeChanel('home');",2000);
	Remote();	
}
//summary:sets positions of elements in page according to window size
function SetLayout()
{
	var wW = $(window).width();
	var wH = $(window).height();
	var mwPT =(wH>620) ? ((wH-620) + "px") : (0 + "px");
	$("#mainWrap").css("padding-top",mwPT);	
}
//summary:changes positons of elements in page according to window size
function WindowResize()
{
	$(window).bind("resize", function(){
		SetLayout();
		var wH = $(window).height();
		var mT =(wH>400) ? ((wH-400) + "px") : (0 + "px");
		if(wH>400)$("#remote").css({top:mT});
		else $("#remote").css({top:0});
	});
}
//summary:enables Easyslider plugin
function PortfolioSlider()
{
	jQuery("#portfolio").easySlider({
		auto: true,
		continuous: true,
		pause:4000
	});
}
//************************************************************//
//******************REMOTE FUNCTIONS**************************//
//************************************************************//
//summary:if div remote isn't visible on screen sets top position according to window height and left position to 55px
function ShowRemote(){
	$("a.whiteRemote").click(function(){											  
		var wWL = ($(window).width()/2)*-1;									  
		var pos_x = $("#remote").css("left");								  
		if(pos_x = wWL){
				$("#remote").removeClass("hidden").addClass("visible");
				$(this).attr("title","Dock the Remote control to left bottom.");}
				var wH = $(window).height();
				var mT =(wH>400) ? ((wH-400) + "px") : (0 + "px");
				$("#remote").animate({
						top: mT,
						left: '55px'
					});				
	});
}

function ShowRemotee(){
                                          
        var wWL = ($(window).width()/2)*-1;                                      
        var pos_x = $("#remote").css("left");                                  
        if(pos_x = wWL){
                $("#remote").removeClass("hidden").addClass("visible");
                $(this).attr("title","Dock the Remote control to left bottom.");}
                var wH = $(window).height();
                var mT =(wH>400) ? ((wH-400) + "px") : (0 + "px");
                $("#remote").animate({
                        top: mT,
                        left: '55px'
                    });                
}
//summary:hides remote div
function HideRemote()
{
	$('input[name=powerTv]').val('1');
	var wWL = ($(window).width()/2)*-1;	
	var pos_x = $("#remote").css("left");
	var leftPos = wWL + "px";
	if(pos_x != wWL)
	{
		$("#remote").animate({
						top: "0px",
						left: leftPos
					});	
		$("#remote").removeClass("visible").addClass("hidden");
		$("a.whiteRemote").attr("title","Take control!");
	}
}
//summary:enables dragging of the remote & portfolio slider function
function Remote()
{	PortfolioSlider();
	$("#remote").draggable({ containment:'body', handle:'#remoteHandBottom', start:function(){}});
	(function($){var a=$.ui.mouse.prototype._mouseMove;$.ui.mouse.prototype._mouseMove=function(b)
    {if($.browser.msie&&document.documentMode>=9){b.button=1};a.apply(this,[b]);}}(jQuery));		
}
//summary:changes chanel on tv set according to the pressed button on the remote
function ChangeChanel(chanel,powerState)
{
	DefaultStates();
	if(chanel!='exit')$('input[name=pageName]').val(chanel);
	if(powerState =='on')$('input[name=powerTv]').val('0');
	switch(chanel)
	{
		case 'home':$("#home").css({ top: 0});break;
		case 'portfolio':$("#portfolio").css({ top: -300});break;
		case 'partners':$("#partners").css({ top: -600});break;
		case 'eControl':$("#eControl").css({ top: -900});break;
		case 'services':$("#services").css({ top: -1200});break;
		case 'video':$("#video").css({ top: -1500});break;
		case 'contact':$("#contact").css({ top: -1800});break;
		case 'team':$("#team").css({ top: -2100});break;
		case 'exit':HideRemote();break;		
	}	
	clearTimeout(timeout);
	ShowChanelName(chanel);
	timeout = setTimeout('FadeOutChanelName()', 2500);
}
//summary:turn tv off or on and restore previous chanel
function PowerOnOff()
{
	if($('input[name=powerTv]').val()=='0')
	{
		$('input[name=powerTv]').val('1');DefaultStates();
	}
	else if($('input[name=powerTv]').val()=='1')
	{
		$('input[name=powerTv]').val('0');ChangeChanel($('input[name=pageName]').val(),'off');
	}
}
//summary:sets default position of chanels
function DefaultStates()
{	
	$("#home").css({ top: "300px"});
	$("#portfolio").css({ top: "300px"});
	$("#partners").css({ top: "300px"});
	$("#eControl").css({ top: "300px"});
	$("#services").css({ top: "300px"});
	$("#video").css({ top: "300px"});
	$("#contact").css({ top: "300px"});
	$("#team").css({ top: "300px"});
	$("#chanelName").hide();
}
//summary:switch chanels throw list of chanels
function SwitchChanel(direction)
{
	var chanelList=["home","services","eControl","team","portfolio","partners","contact","video"];
	var nextPage;
	var page=$('input[name=pageName]').val();
	var currentPage = $.inArray(page,chanelList);
	if(direction=='up')nextPage = (currentPage!=7)?currentPage+1:0;
	else if(direction=='down')nextPage = (currentPage!=0)?currentPage-1:7;
	switch(nextPage)
	{
		case 0:ChangeChanel('home','on');break;
		case 1:ChangeChanel('services','on');break;
		case 2:ChangeChanel('eControl','on');break;
		case 3:ChangeChanel('team','on');break;
		case 4:ChangeChanel('portfolio','on');break;
		case 5:ChangeChanel('partners','on');break;
		case 6:ChangeChanel('contact','on');break;	
		case 7:ChangeChanel('video','on');break;
	}	
}

function ShowChanelName(chanelName)
{
	if(chanelName!='exit')
	{
		$("#chanelName>h3").text('channel: '+ chanelName);
		$("#chanelName").show('fast');	
	}
}
function FadeOutChanelName()
{
	$("#chanelName").fadeOut('normal');	
}



