/**
 * @author vpavlyuk
 *
 * Depends on JQuery 1.4.2
 */

( function() {

	SF.addToDOMReady( function() {
		/**
		 * Top navigation menu
		 */
		$('#lang-contact-login .lcl-level-1 > li > span').hover( function() {
			$(this.parentNode).find('ul').removeClass('hidden');
		}, function(e) {
			var a = $('*', this.parentNode).filter(e.relatedTarget);
			if(!$(e.relatedTarget).hasClass('lcl-level-2') && a.length == 0) {
				$(this.parentNode).find('ul').addClass('hidden');
			}
		}
		);
		$('#lang-contact-login .lcl-level-2').mouseleave( function(e) {
			var a = $(this).find(e.relatedTarget);
			if(a.length == 0) {
				$(this).addClass('hidden');
			}
		});
		/**
		 * Email a friend form
		 */
		$('#copyright_social_media ul li a.eaf').click( function(e) {
			var w = 500, h = 460, hash,
			a = $('#formWindow');
			a.addClass('mail-popup');
			var b = a.find('#formContent');
			var iframeSrc = this.href.split('#')[0] + "?fromURL=" + escape(location.href);
			hash = this.href.split('#')[1];

			if(hash && /\d+x\d+/i.test(hash)) {
				w = parseInt(hash.split('x')[0]);
				h = parseInt(hash.split('x')[1]);
			}

			b.html("<iframe id='mail-frame' src='" + iframeSrc + "' width='" + w + "px' height='" + h + "px' frameborder='0' ></iframe>");
			$('#formWindow').jqmShow();

			return false;

		});
		$('#top-nav .top-menu a.l-1, #reg-top-nav .top-menu a.l-1').hover( function() {
			var id = parseInt(this.id),
			dd = $('#' + id + '_dd'),
			t = $(this),
			leftDD, leftT;

			if(dd.length > 0) {
				dd.css('visibility', 'hidden');
				dd.removeClass('hidden');

				leftDD = dd.offset().left;
				leftT = t.offset().left;

				if(leftDD - 8 > leftT) {
					dd.css('right', (leftDD - leftT) + 7 + 'px');
				}

				dd.css('visibility', 'visible');
			}

		}, function(e) {
			var id = parseInt(this.id);
			var el = $('#' + id + '_dd');
			var a = $('*', el).filter(e.relatedTarget);

			if(!$(e.relatedTarget).hasClass('drop-down') && a.length == 0) {
				el.addClass('hidden');
			}
		}
		);

		$('#nav-bar .drop-down').mouseleave( function(e) {
			var a = $(this).find(e.relatedTarget);
			if(a.length == 0) {
				$(this).addClass('hidden');
			}
		});
		/**
		 * Top navigation menu END
		 */

	});
	/**
	 * Homepage banner
	 */

	function bannerCotnentLoaded() {
		var banners = $('.fce-hp-banner-item'),
		container = $('.fce-hp-banner'),
		height = 0,
		selector,
		buttons,
		current,
		timer;

		if(banners.length == 0)
			return;

		banners.each( function(index) {
			var h = $(this).outerHeight(true),
			btn;

			if(h > height) {
				height = h;
			}
			$(this).css('z-index', banners.length - index)
			.attr('id', index + '_hpb');

			if (banners.length > 1) {
				$(this).hover( function() {
					stopAnimation();
				}, function() {
					startAnimation();
				}
				);

				selector = selector || $('.fce-hp-banner-selector');
				btn = $('<div id="' + index + '_hpb_sel" class="fce-hpb-sel-btn">').click(changeBanner);
				if(index == 0) {
					btn.addClass('fce-hpb-sel-btn-act');
					current = $(this);
					//current.css('z-index', 1);
				}
				selector.append(btn).css('z-index', banners.length + 1);
			}
		});
		banners.css('height', height + 'px');
		banners.css({
			visibility: 'visible',
			opacity: 0
		}).animate({
			opacity: 1
		}, 500);
		container.animate({
			height: height + 'px'
		}, 300);

		if(selector) {
			selector.removeClass('hidden');
		}

		function changeBanner(e) {
			var id = parseInt($(this).attr('id')),
			target = $(banners.get(id));

			if(!$(this).hasClass('fce-hpb-sel-btn-act')) {
				current.fadeOut('slow')
				target.fadeIn('slow');
				current = target;
				$('.fce-hpb-sel-btn-act', container).removeClass('fce-hpb-sel-btn-act');
				$(this).addClass('fce-hpb-sel-btn-act')
				resetAnimation();
			}

		}

		function startAnimation() {
			timer = setInterval( function() {
				var id = parseInt(current.attr('id')),
				nextId = (id + 1 < banners.length) ? id + 1 : 0,
				target = $('#' + nextId + '_hpb');

				current.fadeOut('slow')
				target.fadeIn('slow');
				current = target;

				$('.fce-hpb-sel-btn-act', container).removeClass('fce-hpb-sel-btn-act');
				$('#' + nextId + '_hpb_sel').addClass('fce-hpb-sel-btn-act');

			}, 6000);
		}

		function stopAnimation() {
			clearInterval(timer);
		}

		function resetAnimation() {
			stopAnimation();
			startAnimation();
		}

		if(banners.length > 1) {
			startAnimation();
		}

	}

	$(window).load(bannerCotnentLoaded);

	/**
	 * Homepage banner END
	 */

}());
/**
 * Homepage news ticker
 */
SF.addToDOMReady( function() {

	var news = $('#news_ticker #news_items > a'),
	timer,
	current,
	next;

	if(news.length > 1) {

		current = $(news.get(0));

		news.hover( function() {
			stopAnim();
		}, function() {
			startAnim();
		});
		startAnim();

		$('#news_ticker ul li.next').click( function() {
			next = current.next();
			if(next.length == 0) {
				next = $(news.get(0));
			}
			current.fadeOut('fast', function() {
				next.fadeIn()
			});
			current = next;
			restartAnim();
		});
		$('#news_ticker ul li.prev').click( function() {
			next = current.prev();
			if(next.length == 0) {
				next = $(news.get(news.length - 1));
			}
			current.fadeOut('fast', function() {
				next.fadeIn()
			});
			current = next;
			restartAnim();
		});
	}

	function startAnim() {
		timer = setInterval( function() {
			next = current.next();
			if(next.length == 0) {
				next = $(news.get(0));
			}
			current.fadeOut('fast', function() {
				next.fadeIn()
			});
			current = next;
		}, 6000);
	}

	function stopAnim() {
		clearInterval(timer)
	}

	function restartAnim() {
		stopAnim();
		startAnim();
	}

});
/**
 * Homepage Video
 */
SF.addToDOMReady( function() {

    $('.fce-hpvid-l').live("click", function(e){
        var el = $(e.currentTarget);

        var href = el.attr('href');
        var hash = href.split('#')[1];
        if(hash && /\d+x\d+/i.test(hash)) {
                var w = parseInt(hash.split('x')[0]);
                var h = parseInt(hash.split('x')[1]);
        }
        
        w = Number(w) + 5;
        h = Number(h) + 5;
        videoReposition(w,h);
        $(window).resize(function(){
	    	if($('.jqmWindow')){
	    		videoReposition(w,h);
	    	}
	    });
        var fWin = $('#formWindow').addClass('fce-hpvid-popup');
        var winContent = $('#formContent', fWin);
        winContent.html('<iframe id="mail-frame" src="' + href + '" width="' + w + 'px" height="' + h + 'px" frameborder="0"></iframe>');
        fWin.jqmShow();
        return false;
	});
	
	var context = $('.fce-hp-video');
	context.each( function(index) {
		var ctx = $(this),
		links = $('.fce-hpvid-links a', ctx),
		mainImage = $('.fce-hpvid-image', ctx);

		links.mouseenter( function(e) {
			setImage($(this), mainImage);
		});
		setImage($(links.get(0)), mainImage);
	});
	context.live('click', function(e) {
		var target = $(e.target),
		fWin, winContent,
		w = 515, h = 300,
		hash, href;

		if(target.is('a.fce-hpvid-l') || target.closest('a.fce-hpvid-l').is('a.fce-hpvid-l')) {
			href = target.attr('href') || target.closest('a.fce-hpvid-l').attr('href');
			hash = href.split('#')[1];

			if(hash && /\d+x\d+/i.test(hash)) {
				w = parseInt(hash.split('x')[0]);
				h = parseInt(hash.split('x')[1]);
			}
			$(window).resize(function(){
		    	if($('.jqmWindow')){
		    		videoReposition(w,h);
		    	}
		    });

			fWin = $('#formWindow').addClass('fce-hpvid-popup');
			winContent = $('#formContent', fWin);
			winContent.html('<iframe id="mail-frame" src="' + href + '" width="' + w + 'px" height="' + h + 'px" frameborder="0" ></iframe>');
			fWin.jqmShow();

			e.preventDefault();
			return false;
		}
	});
	function setImage(link, imgContainer) {
		var img = link.parent('li').find('img').clone();
		imgContainer.empty();
		link.clone().empty().appendTo(imgContainer).append(img);
	}
	
	function videoReposition(w,h){
		// Run me with the width and height of the video, and ill move the player to center.
		var browserWidth = $(window).width();
		var browserHeight = $(window).height();
		posX = ((browserWidth - w) / 2) - 30;
		posY = ((browserHeight - h) / 2) - 30;
		//console.log('posX'+posX+'xPosY'+posY);
		$('.jqmWindow').css({
			'margin-left':'0px',
			'left':posX+'px',
			'top':posY+'px'
		});
	}

});
/**
 * Cookie tracking
 */
SF.addToDOMReady( function() {

	var cpid = $.query.get('cpid'), //Internal campaign ID. This is new style.
	sf_ppc_keyword = $.query.get('kw'), //Search keyword passed as GET parameter when visitor come from PPC system
	sf_ppc_source = $.query.get('source'), //PPC system name
	ref = document.referrer, //Referrer
	campaign_ID = $.query.get('Campaign_ID'), //External campaign ID which is used in okd-style links
	tag = $.query.get('TAG'),
	cmpLeadSource = $.query.get('CmpLeadSource'),
	campVars;

	//If new style campaign ID is present use it and delete old style cookie
	if(cpid) {
		$.cookie('sf_cpid', cpid, {
			expires: 90,
			path: '/'
		});
		$.cookie('sf_campaignVars', null, {
			path: '/'
		});
	}

	//If old style campaing info is present
	//sf_campaignVars CampaignID|TAG|LeadSource|Referrer
	if(campaign_ID || tag || cmpLeadSource) {
		campVars = [campaign_ID, tag, cmpLeadSource, ref];
		$.cookie('sf_campaignVars', campVars.join('|'), {
			expires: 90,
			path: '/'
		});
		$.cookie('sf_cpid', null, {
			path: '/'
		});
	}

	//Set PPC vars sf_ppcVars source|keyword|Referrer
	if(sf_ppc_source) {
		$.cookie('sf_ppcVars', sf_ppc_source + '|' + sf_ppc_keyword + '|' + ref, {
			expires: 90,
			path: '/'
		});
	}

	//Set SEO vars
	//If the referrer is not successfactors, set the SEO variables
	if(ref && ref.indexOf('www.successfactors.com') < 0 && ref.indexOf('http://successfactors.com') < 0 && ref.indexOf('http://dev-ss.successfactors.com') < 0) {
		$.cookie('sf_seo_referrer', ref, {
			expires: 90,
			path: '/'
		});
	}

});
$(document).ready( function() {
	SF.bootstrap();
});
