// page init
jQuery(function() {
	initReg();
	initCufon();
	initNav();
	initCycleGallery();
	
});

function initReg(){
	jQuery(".right-sidebar:first").addClass('first-sidebar');
	var swfplayer="http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf";
	flowplayer("a.media_player", swfplayer);
	
		
	jQuery(".video_link").fancybox({
		'width'				: 630,
		'height'			: 435,
		'overlayShow'		: true,
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	jQuery(".video_link_smaller").fancybox({
		'width'				: 330,
		'height'			: 255,
		'overlayShow'		: true,
        'autoScale'     	: true,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'			: 'no'
	});	
	
	$(".hoverlink").parent().parent().parent().hover(
		function(){
			$(".hoverlink").each(function(){
				$(this).find("img").attr("src", $(this).find("img").attr("src").replace(/.gif/, "-r.gif"));
			});
		},
		function(){
			$(".hoverlink").each(function(){
				$(this).find("img").attr("src", $(this).find("img").attr("src").replace(/-r.gif/, ".gif"));
			});
		}
	);
	
	$(".contact-container h2, .contact-container p").addClass('cufonated');
	

	//check for submenu for graphics
	
	$("ul#menu-graphics-menu > li > a").parent().each(function(){
		var children=$(this).find('ul').size();
		if(children > 0){
			if($(this).hasClass('active'))
			{
				$(this).addClass('retract-menu-white');
			}
			else
			{
				$(this).addClass('expand-menu');
			}
		}
	});
	
	
	$(".expand-menu-white").live('click', function(){
		$(this).find('ul').show();
		$(this).removeClass('expand-menu-white');
		//if this has class active, use white image class
		$(this).addClass('retract-menu-white');
	});
	
	$(".retract-menu-white").live('click', function(){
		$(this).find('ul').hide();
		$(this).removeClass('retract-menu-white');
		$(this).addClass('expand-menu-white');
	});		
	
	$(".expand-menu").live('click', function(){
		$(this).find('ul').show();
		$(this).removeClass('expand-menu');
		$(this).addClass('retract-menu');
	});
	
	$(".retract-menu").live('click', function(){
		$(this).find('ul').hide();
		$(this).removeClass('retract-menu');
		$(this).addClass('expand-menu');
	});	
	
	$(".retract-menu ul, .expand-menu ul, .expand-menu-white ul, .retract-menu-white ul").click(function(e){
		e.stopPropagation();
	});
	
}

// replace cufon elements
function initCufon() {
	Cufon.set('fontFamily', 'Gotham Bold');
	Cufon.replace('#carousel .title');
	Cufon.replace('#carousel blockquote');
	Cufon.replace('#main h2');
	Cufon.replace('#twocolumns h3');
	Cufon.replace('.text-container .title, .login-big, .learn-holder .learn, .alaised, .cufonated, .right-sidebar h2, .engage_title');
	//$('ul#menu-graphics-menu > li > a').addClass('cufonated');
	Cufon.replace('.comparision .title', { textShadow: '#222 1px 1px' });
	Cufon.replace('#intro h1', { textShadow: '#222 2px 2px'});
}

// cycle gallery init
function initCycleGallery() {
	if(jQuery('#carousel .frame li').size()>1){
	jQuery('#carousel').cycleGallery({
		slider:'.frame > ul',
		slides:'>li',
		pagerLinks:'.switcher li',
		autoRotation:10000,
		animSpeed:500
	});}
	
	jQuery(".engage_box").cycle();
}

// init autoscaling nav
function initNav() {
	initAutoScalingNav({
		menuId: "main-nav",
		sideClasses: true,
		minPaddings: 10
	});
	initAutoScalingNav({
		menuId: "add-nav",
		sideClasses: true,
		minPaddings: 10
	});
}

// cycle gallery
jQuery.fn.cycleGallery = function(options){
	var options = jQuery.extend({
		holder: '.holder',
		slider: '.slider',
		slides: 'li',
		btnPrev: '.link-prev',
		btnNext: '.link-next',
		pagerLinks: '.pager a',
		curItem: '.cur-item',
		allItems: '.all-items',
		activeClass: 'active',
		autoRotation: false,
		animSpeed: 650
	}, options);

	return this.each(function(){
		var gal = jQuery(this);
		var holder = jQuery(options.holder, gal);
		var slider = jQuery(options.slider, gal);
		var slides = jQuery(options.slides, slider);
		var btnPrev = jQuery(options.btnPrev, gal);
		var btnNext = jQuery(options.btnNext, gal);
		var pagerLinks = jQuery(options.pagerLinks, gal);
		var curItem = jQuery(options.curItem, gal);
		var allItems = jQuery(options.allItems, gal);
		var activeClass = options.activeClass;
		var animSpeed = options.animSpeed;
		var autoRotation = options.autoRotation;
		
		var animating = false;
		var len = slides.length;
		var slideWidth = slides.eq(0).outerWidth(true);
		var index = 0, prevIndex = 0;
		var direction = true, timer;
		
		slides.css({position: 'absolute', left: slideWidth}).eq(0).css({left: 0}).addClass(activeClass);
		slider.css({position: 'relative'}).height(slides.eq(0).outerHeight(true));
		
		function switchSlide() {
			animating = true;
			stopAutoSlide();
			if(direction) {
				slides.eq(prevIndex).removeClass(activeClass).stop().animate({left: -slideWidth}, {
					duration: animSpeed
				})
				slides.eq(index).addClass(activeClass).css({left: slideWidth}).stop().animate({left: 0}, {
					duration: animSpeed,
					complete: function() {
						animating = false;
					}
				})
			}
			else {
				slides.eq(prevIndex).removeClass(activeClass).stop().animate({left: slideWidth}, {
					duration: animSpeed
				})
				slides.eq(index).addClass(activeClass).css({left: -slideWidth}).stop().animate({left: 0}, {
					duration: animSpeed,
					complete: function() {
						animating = false;
					}
				})
			}
			slider.stop().animate({height: slides.eq(index).outerHeight(true)}, {
				duration: animSpeed
			})
			prevIndex = index;
			refresh();
			autoSlide();
		}
		function refresh() {
			var cur = index >= 0 ? index >= len ? 1 : index +1 : len - Math.abs(index) + 1;
			if(curItem.length) curItem.text(cur);
			if(allItems.length) allItems.text(len);
			if(pagerLinks.length) pagerLinks.removeClass(activeClass).eq(cur - 1).addClass(activeClass);
		}
		function slideNext() {
			if(!animating) {
				if(index == len - 1) index = 0;
				else index++;
				direction = true;
				switchSlide();
			}
		}
		function slidePrev() {
			if(!animating) {
				if(index == 0) index = len - 1;
				else index--;
				direction = false;
				switchSlide();
			}
		}
		btnNext.click(function() {
			slideNext();
			return false;
		})
		btnPrev.click(function() {
			slidePrev();
			return false;
		})
		pagerLinks.each(function(n) {
			jQuery(this).click(function() {
				if(!animating && index != n) {
					index = n;
					if(index > prevIndex) direction = true;
					else direction = false;
					switchSlide();
				}
				return false;
			})
		})
		function stopAutoSlide() {
			clearTimeout(timer);
		}
		function autoSlide() {
			if(autoRotation) {
				clearTimeout(timer);
				timer = setTimeout(slideNext, autoRotation + animSpeed);
			}
		}
		refresh();
		autoSlide();
	});
}

// autoscaling navigation function
function initAutoScalingNav(o) {
	if (!o.menuId) o.menuId = "nav";
	if (!o.tag) o.tag = "a";
	if (!o.spacing) o.spacing = 0;
	if (!o.constant) o.constant = 0;
	if (!o.minPaddings) o.minPaddings = 0;
	if (!o.liHovering) o.liHovering = false;
	if (!o.sideClasses) o.sideClasses = false;
	if (!o.equalLinks) o.equalLinks = false;
	if (!o.flexible) o.flexible = false;
	var nav = document.getElementById(o.menuId);
	if(nav) {
		nav.className += " scaling-active";
		var lis = nav.getElementsByTagName("li");
		var asFl = [];
		var lisFl = [];
		var width = 0;
		for (var i=0, j=0; i<lis.length; i++) {
			if(lis[i].parentNode == nav) {
				var t = lis[i].getElementsByTagName(o.tag).item(0);
				asFl.push(t);
				asFl[j++].width = t.offsetWidth;
				lisFl.push(lis[i]);
				if(width < t.offsetWidth) width = t.offsetWidth;
			}
			if(o.liHovering) {
				lis[i].onmouseover = function() {
					this.className += " hover";
				}
				lis[i].onmouseout = function() {
					this.className = this.className.replace("hover", "");
				}
			}
		}
		var menuWidth = nav.clientWidth - asFl.length*o.spacing - o.constant;
		if(o.equalLinks && width * asFl.length < menuWidth) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].width = width;
			}
		}
		width = getItemsWidth(asFl);
		if(width < menuWidth) {
			var version = navigator.userAgent.toLowerCase();
			for (var i=0; getItemsWidth(asFl) < menuWidth; i++) {
				asFl[i].width++;
				if(!o.flexible) {
					asFl[i].style.width = asFl[i].width + "px";
				}
				if(i >= asFl.length-1) i=-1;
			}
			if(o.flexible) {
				for (var i=0; i<asFl.length; i++) {
					width = (asFl[i].width - o.spacing - o.constant/asFl.length)/menuWidth*100;
					if(i != asFl.length-1) {
						lisFl[i].style.width = width + "%";
					}
					else {
						if(navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)
							lisFl[i].style.width = width + "%";
					}
				}
			}
		}
		else if(o.minPaddings > 0) {
			for (var i=0; i<asFl.length; i++) {
				asFl[i].style.paddingLeft = o.minPaddings + "px";
				asFl[i].style.paddingRight = o.minPaddings + "px";
			}
		}
		if(o.sideClasses) {
			lisFl[0].className += " first-child";
			lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";
			lisFl[lisFl.length-1].className += " last-child";
			lisFl[lisFl.length-1].getElementsByTagName(o.tag).item(0).className += " last-child-a";
		}
		nav.className += " scaling-ready";
	}
	function getItemsWidth(a) {
		var w = 0;
		for(var q=0; q<a.length; q++) {
			w += a[q].width;
		}
		return w;
	}
}
