var videoSrc; 
var videoExt;

//for the future we should use this to open links in new windows because target is being deprecated
$().ready(function() {
	$('a.newWindow').each(function() {
		$(this).click(function(event) {
			event.preventDefault();
			window.open($(this).href,'_blank');
		});
	});
    
    
    //$.swapImage(".swapImage");	
	
	$("#demoBtn").click(function(){
		videoSrc = $(this).attr('rel');
		
		if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) { 
		   
			if (document.cookie.indexOf("iphone_redirect=false") == -1) {
		   	   window.location = "/videos/demos/" + videoSrc +"_idevice.m4v"; 
			}
			
		} else {
			centerPopup();
			loadPopup();
		}
	});
				
	$("#popupContactClose").click(function(){
		disablePopup();
	});

	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});


function initReviewCarousel(){
	$('#reviewCarousel').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout:         5000,  // milliseconds between slide transitions (0 to disable auto advance) 
	    speed:           500,
		delay:   4000,
		random:1,
		sync: 0,
		pause: 1
	
	});
}

function initAppCarousel() {
	$('#appNav').find('ul').first().children('li').each(function(item){
		$(this).css('height',$(this).children('a').first().css('height'));
		$(this).css('width',$(this).children('a').first().css('width'));
	});
    
    $('.appNavCarousel').tinycarousel();
}

function carouselWillMoveFromClick(dist){
    $('#selectArrow').animate({left:(parseInt($('#selectArrow').css('left')) + dist)},{duration:600});
}


function loadVideo(videoSrc) {
	fv = $("#video").flareVideo({
		autoplay: true,
		flashSrc: '../../videos/demos/' + videoSrc + ".swf",
        width: '100%',
        height: '100%'
	});
      fv.load([
        {
        	height: 480,
			src:  '../../videos/demos/' + videoSrc + ".mp4",
        	type: 'video/mp4',
			width: 320
    	},
		{
        	height: 480,
			src:  '../../videos/demos/' + videoSrc + ".ogg",
        	type: 'video/ogg; codecs="theora, vorbis"',
			width: 320
    	}
	]);
	fv.onpause(loadBtns);
    fv.onended(loadBtns);
	fv.onplay(hideBtns);
}

var btnState = 0;

function loadBtns() {
	$('#downBtns').fadeIn(600);
}

function hideBtns() {
	$('#downBtns').fadeOut(600);	
}

// --------------------------------------------------------
// @Author: Adrian "yEnS" Mato Gondelle
// @website: www.yensdesign.com
// @email: yensamg@gmail.com					
// --------------------------------------------------------

var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0) { 
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		$("#backgroundPopup").fadeIn(600);
		$("#popupContact").fadeIn(600);
		popupStatus = 1;
		setTimeout('loadVideo(videoSrc)',600);
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut(600);
		$("#popupContact").fadeOut(600);
		popupStatus = 0;
		//$("#video").empty();
		fv.remove();
	}
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();

	$("#popupContact").css({
		"position": "absolute",
		"top": 10,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}
