var footerHeight = 173;
var headerHeight = 116;

$(document).ready(function() {
	
	$('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title'),'width=704, height=500, scrollbars'); e.preventDefault(); });
	
	bVersion = getInternetExplorerVersion();
	
	agent = navigator.userAgent.toLowerCase();
    is_chrome = agent.indexOf('chrome') > -1;
	is_iphone = ((agent.indexOf('iphone') != -1));
	is_ipad = ((agent.indexOf('ipad') != -1));
	
	if (is_iphone) { 
		$('#page').css({"padding":"0px 10px"});	
	}	
	
	if (is_ipad) { 
		$('#page').css({"padding":"0px 10px"});	
	}
	
	$('.qaBlock:eq(0)').css({"padding-top":"15px"});

	$('.qaBlockQuestion').click(function(e) {
    	//$('.qaBlockQuestion').children('h3').removeClass('open');
    	if($(this).children('h3').hasClass('open')) {
			$(this).siblings('.qaBlockAnswer').animate({height: '0px'},300, "swing");
			$(this).children('h3').removeClass('open');
			$(this).parent().removeClass('qaBlockQuestionOpen');    	
    	} else {
	    	$(this).children('h3').addClass('open');
    	    $('.qaBlockAnswer').stop(true, true);
			selectedElement = $(this).parent();
			answerHeight = $('.qaBlockAnswerContainer', selectedElement).height();
			if(!$(selectedElement).hasClass('qaBlockQuestionOpen')) {
				if($('.qaBlockQuestionOpen').length == 0) {
					$('.qaBlockAnswer', selectedElement).animate({height: 15+answerHeight+'px'},300, "swing");
					$(selectedElement).addClass('qaBlockQuestionOpen');
				} else {

					$('.qaBlockAnswer', selectedElement).animate({height: 15+answerHeight+'px'},300, "swing");
					$(selectedElement).addClass('qaBlockQuestionOpen');
				}
			}			
        }
    });
    
    if($('.videoActive').length > 0) {
        $('.videoActive').each(function(i) {
            identifier = 'videoElement'+i;
            $('.imageTextVideoFeatureImage', this).attr('id', identifier);
            showVideo($('.videoPlay', this).attr('href'), $('.videoImage', this).html(), $('.videoTitle', this).html(), $('.video264', this).html(), identifier);
        });
    }    
    
    $('.videoPlay').click(function(e) {
        e.preventDefault();
        $('body').prepend('<div class="popupOverlay">&nbsp;</div><div class="popupContainer"><div class="popupWrapper"><div class="popupWindow"><a href="#" class="popupClose">close video</a><div class="videoDetails"></div><div class="popupWindowContent" id="infoBlockFlashElement"></div></div></div></div>');
        showOverlay($(this).attr('href'), $('.videoImage', this).html(), $('.videoTitle', this).html(), $('.video264', this).html());
    });
    
});

function popupWindow(url, name, attributes){
	var queryStringExists = false;
	if(url.match(/\?/)) {
		queryStringExists = true;
	}
	if(queryStringExists) {
		if(!url.match(/(\?|&)popup=/)) {
			url = url + '&popup=true';
		}
	} else {
		url = url + '?popup=true';
	}
	
	var newWindow = window.open(url, name.replace(/[^A-z]/ig, ''), attributes);
	newWindow.focus();
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function showOverlay(videoFile, imageFile, videoTitle, video264) {
    $('.popupOverlay').css({display: 'block'});
    $('.popupContainer').css({display: 'block'});
    //if(bVersion == 6) {
        totalHeight = footerHeight + headerHeight + $('#content').height();
        $('.popupOverlay, .popupContainer').css({height: totalHeight+'px'})
    //}
    $(window).scrollTo(100, 800);
    $('.popupOverlay').animate({opacity: 0.7}, 300, "swing", function() {
    	$('.popupContainer').animate({opacity: 1.0}, 300, "swing", function() {
            if (is_ipad || is_iphone) {
                $('#infoBlockFlashElement').append('<video id="videoElement" width="779" height="451" controls="controls" preload="none" style="background-color:#FFFFFF;" src="'+baseHref+'uploads'+video264+'" poster="'+baseHref+'uploads'+imageFile+'">&nbsp;</video>');
            	$('#videoElement').append('<img src="'+baseHref+'uploads'+imageFile+'" />');
            } else {
                var so = new SWFObject("swf/video-player.swf", "infoBlockVideoElement", "779", "451", "9.0.0", "#ffffff");
                so.addParam("wmode", "transparent");
                so.addParam("allowFullScreen", "true");
                so.addParam("allowScriptAccess", "always");
                so.addParam("allowNetworking", "all");
                so.addVariable('videoPath', baseHref+'uploads'+videoFile);
                so.addVariable('splashPath', baseHref+'uploads'+imageFile);
                so.addVariable("color", "#74798c");
                so.write("infoBlockFlashElement");
            }
            if(videoTitle != '') {
                $('.videoDetails').append('<h3>'+videoTitle+'</h3>');
            } 
            $('.closeOverlay, .popupClose').click(function(e) {
                e.preventDefault();
                closeOverlay();
            });
    	});
    });
}

function closeOverlay() {
    $('.popupContainer').animate({opacity: 0.0}, 100, "swing", function() {
    	$('.popupOverlay').animate({opacity: 0.0}, 100, "swing", function() {
        	$('.popupOverlay').remove();
            $('.popupContainer').remove();
    	});
    });		    
}

function showOverlay(videoFile, imageFile, videoTitle, video264) {
    if (is_ipad || is_iphone) {
        $('#infoBlockFlashElement').append('<video id="videoElement" width="779" height="451" controls="controls" preload="none" style="background-color:#FFFFFF;" src="'+baseHref+'uploads'+video264+'" poster="'+baseHref+'uploads'+imageFile+'">&nbsp;</video>');
        $('#videoElement').append('<img src="'+baseHref+'uploads'+imageFile+'" />');
    } else {
        var so = new SWFObject("swf/video-player.swf", "infoBlockVideoElement", "779", "451", "9.0.0", "#ffffff");
        so.addParam("wmode", "transparent");
        so.addParam("allowFullScreen", "true");
        so.addParam("allowScriptAccess", "always");
        so.addParam("allowNetworking", "all");
        so.addVariable('videoPath', baseHref+'uploads'+videoFile);
        so.addVariable('splashPath', baseHref+'uploads'+imageFile);
        so.addVariable("color", "#74798c");
        so.write("infoBlockFlashElement");
    }
}

function showVideo(videoFile, imageFile, videoTitle, video264, identifier) {
    if (is_ipad || is_iphone) {
        $('#'+identifier).html('<video id="videoElement" width="285" height="165" controls="controls" preload="none" style="background-color:#FFFFFF;" src="'+baseHref+'uploads'+video264+'" poster="'+baseHref+'uploads'+imageFile+'">&nbsp;</video>');
        $('#videoElement').append('<img src="'+baseHref+'uploads'+imageFile+'" />');
    } else {
        var so = new SWFObject("swf/video-player.swf", "infoBlockVideoElement", "285", "165", "9.0.0", "#ffffff");
        so.addParam("wmode", "transparent");
        so.addParam("allowFullScreen", "true");
        so.addParam("allowScriptAccess", "always");
        so.addParam("allowNetworking", "all");
        so.addVariable('videoPath', baseHref+'uploads'+videoFile);
        so.addVariable('splashPath', baseHref+'uploads'+imageFile);
        so.addVariable("color", "#74798c");
        so.write(identifier);
    }
}
