jQuery.noConflict();
(function($) {
$(document).ready(function() {

    var showVideo = false;
    if (location.search.indexOf('video') > -1) {
        showVideo = true;
    }

    Overlay.setup();
    Cufonts.setup();
    Typographer.replace();

    if (!showVideo) {
        homeFlash.init();
    }

    $('#teaserbar a[href^=http://]').each(function() {
        this.target = "_blank";
    });

    $($('.button')[1]).click
        (
            function() {
                $('#flashContent').before("<div id=\"mediaspace\"></div>").hide();
                //  center object
                $('#content').css('margin-left', '150px');
                $('#mediaspace').show();
                $('#mediaspace').jwPlayer
                (
                    {
                        swf: '/fileadmin/templates/html/flash/player.swf',
                        file: '/fileadmin/templates/html/flash/Company_video_2010.flv',
                        image: '/fileadmin/templates/html/images/Company_video_2010.jpg',
                        height: 352,
                        width: 625,
                        autostart: true,
                        controlbar: 'over',
						backcolor: '#ffffff',
						screencolor: '#ffffff'
                    }
                );
            }
        );

    if (showVideo) {
        //$($('.button')[1]).click();
    }
});

	var homeFlash = {
		init : function() {
			if ($('body.p65')) {
				var flashvars = {};
				flashvars.fvTimer = "6";
				flashvars.fvDataXmlUrl = "/?type=106";
				var params = {};
				params.wmode = "transparent";
				var attributes = {};
				swfobject.embedSWF(
						"/fileadmin/templates/html/flash/highlight.swf",
						"flashContent", "970", "400", "9.0.124", false,
						flashvars, params, attributes);
			}
		}
	}

	var Cufonts = {
		setup : function() {
			setTimeout(function() {
				Cufon.replace('#content h2', {
					'fontFamily' : 'Myriad Pro SemiBold',
					'font-weight' : '600'
				});
				Cufon.replace('#teaserbar h2', {
					'fontFamily' : 'Myriad Pro',
					'font-weight' : '400'
				});

				Cufonts.cleanup();
			}, 10);
		},
		cleanup : function() {
			$(window).unload(function() {
				$("canvas, span.cufon").remove();
			});
		}
	}

	var Typographer = {
		replace : function() {
			$('#nav a span').each(function() {
				switch ($(this).text()) {
				case 'CubiX3':
					$(this).parent().addClass('cubix3');
					break;
				case 'Axios mAX':
					$(this).parent().addClass('axiosmax');
					break;
				case 'Welcome':
					$(this).parent().addClass('welcome');
					break;
				}

			});
			$('h1 img')
					.each(
							function() {
								switch ($(this).attr('title')) {
								case 'CubiX3':
									$(this)
											.attr('src',
													'/fileadmin/templates/html/images/backgrounds/CubiX3-active.png');
									break;
								case 'Axios mAX':
									$(this)
											.attr('src',
													'/fileadmin/templates/html/images/backgrounds/axiosmax-active.png');
									break;
								}
							});
		}
	}

	var Overlay = {
		setup : function() {
			$('a [href^=/videos/]').addClass('video_popover');
		
			// set hide on close overlay button
			$('.overlay_close_image').live('click', function() {
				$('.simple_overlay_container').hide();
				$('.transparent_overlay').remove();
			});
			
			$('#video_linking').live('click', function(e) {
				$('#video_linking').select();
			});

			$('select.overlay_videos_select').live(
					'change',
					function() {
						var link = $(
								'select.overlay_videos_select option:selected')
								.val();
						$.get(
								link + '?type=105',
								function(data) {
									$('.simple_overlay_container').replaceWith(
											data);
									var winH = $(window).height();
									var winW = $(window).width();
									var id = ".simple_overlay";
									// Set the popup window to center
									$(id).css('top',
											winH / 2 - $(id).height() / 2);
									$(id).css('left',
											winW / 2 - $(id).width() / 2);
									$(id).css('height', 'auto');
								});
					});

			// get html for overlay using ajax
			var video_popover = $('.video_popover');
			
			// get link_value
			var querystring = window.location.search.substring(1);
			var vid_array = querystring.split('=');

			var link = vid_array[1];

			if (vid_array[0] == 'vid' && link) {
				// already present, so don't ajax but just show it
				if ($('.simple_overlay_container').length) {
					$('.simple_overlay_container').show();
				} else // get video overlay html using ajax
				{
					$
					.get(link + '?type=105', function(data) {
						$("body").append(data);
						// Get the window height and width
							var winH = $(window).height();
							var winW = $(window).width();
							var id = ".simple_overlay";
							// Set the popup window to center
							$(id).css('top',
									winH / 2 - $(id).height() / 2);
							$(id).css('left',
									winW / 2 - $(id).width() / 2);
							$(id).css('height', 'auto');
						});
				}

				if ($('.transparent_overlay').length == 0) {
					$("body").append(
							'<div class="transparent_overlay"></div>');
					var maskHeight = $(document).height();
					var maskWidth = $(window).width();
					$('.transparent_overlay').css( {
						'width' : maskWidth,
						'height' : maskHeight
					});
				}
			}

			// get link_value
			var link = video_popover.attr('href');

			video_popover.click(function(event) {
				// already present, so don't ajax but just show it
					if ($('.simple_overlay_container').length) {
						$('.simple_overlay_container').show();
					} else // get video overlay html using ajax
					{
						$
								.get(link + '?type=105', function(data) {
									$("body").append(data);
									// Get the window height and width
										var winH = $(window).height();
										var winW = $(window).width();
										var id = ".simple_overlay";
										// Set the popup window to center
										$(id).css('top',
												winH / 2 - $(id).height() / 2);
										$(id).css('left',
												winW / 2 - $(id).width() / 2);
										$(id).css('height', 'auto');
									});
					}

					if ($('.transparent_overlay').length == 0) {
						$("body").append(
								'<div class="transparent_overlay"></div>');
						var maskHeight = $(document).height();
						var maskWidth = $(window).width();
						$('.transparent_overlay').css( {
							'width' : maskWidth,
							'height' : maskHeight
						});
					}

					//stop default a tag behavior
					event.stopImmediatePropagation();
					event.preventDefault();
					return false;
				});

		}
	}

})(jQuery);

