var myvan = myvan || {};
myvan.site = myvan.site || {};


//INIT
$(document).ready(function() {

	myvan.site.general.init();
	myvan.site.general.columnsOnResize();

	/* on resize of viewport change max-width of wrapper to force isotope (looks nice :-) ) */
	$(window).resize(myvan.site.general.columnsOnResize);

});

myvan.site.general = {
	init: function() {
		myvan.site.general.dropdownmenu();
		myvan.site.general.behaviourSearchform();
		myvan.site.general.rotate_social();
		myvan.site.general.columnsOnResize();
		myvan.site.general.relayout();
		myvan.site.general.disclaimer();
	},
	dropdownmenu: function () {
		/* nice hover effect for dropdown list */
		$('#program').mouseenter(function(e){
			$('.programs').stop().animate({'height' : '250px'}, 100);
			//$('.programs ul').stop().animate({'top': '18px'}, 350);
			$('.choose_program a').addClass('leer');
		}).mouseleave(function(e){
			//$('.programs ul').stop().animate({'top': '-250px'}, 100);
			$('.programs').stop().animate({'height' : '0px'}, 350);
			$('.choose_program a').removeClass('leer');
		});

		/* language selector */
		$('a.lang_sel').click(function(e){
			$('#icl_language_menu').stop().animate({'height' : '102px'}, 300);

			var t = window.setTimeout(function(){
				$('#icl_language_menu').stop().animate({'height' : '0px'}, 300);
			},
			15000);
		});
		$('#icl_language_menu').mouseenter(function(){
			clearTimeout(t);
		}).mouseleave(function(e){
			//window.setTimeout(function(){
				$('#icl_language_menu').stop().animate({'height' : '0px'}, 300);
			//},
			//4500);
		});
	},
	relayout: function() {
		/* move boxes around */
		if($('#inner-wrapper').length > 0) {
			$('#inner-wrapper').isotope({
				itemSelector: myvan.site.config.masonry.itemselector,
				masonry : {
					columnWidth : myvan.site.config.masonry.columnWidth,
					columnHeight: myvan.site.config.masonry.columnHeight
				},
				animationEngine: 'jquery'
			});
		}
	},
	rotate_social: function() {
		/* SOCIAL HUB ANIMATIONS */
		if($('.rotation').length > 0) {
			/* horizontal scrolling of the inner divs */
			$('.rotation').cycle({
				fx:     'scrollHorz',
				timeout: myvan.site.config.rotate_social.horz_timeout,
				speed: myvan.site.config.rotate_social.horz_speed,
				rev:     1,
				next: myvan.site.config.rotate_social.horz_prev,
				prev: myvan.site.config.rotate_social.horz_next,
				manualTrump: 0
			});

			/* vertical scrolling of the outer divs */
			$('.vertrotation').cycle({
				fx:		'scrollVert',
				timeout: myvan.site.config.rotate_social.vert_timeout,
				rev:     1,
				speed: myvan.site.config.rotate_social.vert_speed,
				next: myvan.site.config.rotate_social.vert_next,
				prev: myvan.site.config.rotate_social.vert_prev,
				manualTrump: 0
			});

			/* vertical scrolling of the headline */
			$('.headline_rotation').cycle({
				fx:		'scrollVert',
				timeout: myvan.site.config.rotate_social.vert_timeout,
				speed: myvan.site.config.rotate_social.vert_speed,
				rev:     1,
				next: myvan.site.config.rotate_social.vert_next,
				prev: myvan.site.config.rotate_social.vert_prev,
				manualTrump: 0
			});

			/* nice hover icon menu in social hub */
			$(".external li a").mouseenter(function(event) {
				$('.regular', this).stop().animate({'top':	'32px'},	myvan.site.config.rotate_social.icons_timeout);
				$('.yellow', this).stop().animate({'top':	'0px'},		myvan.site.config.rotate_social.icons_timeout);

				$('.ext_tooltip').stop().animate({'opacity': '0'}, 300, function(){
					$('.ext_tooltip').html($(event.target).closest('.ext').attr('title'));
					$('.ext_tooltip').animate({'opacity': '1'}, 300);
				});
			}).mouseleave(function() {
				$('.ext_tooltip').stop().animate({'opacity': '0'}, 300, function(){
					$('.ext_tooltip').html('');
					$('.ext_tooltip').animate({'opacity': '1'}, 300);
				});
				$('.regular', this).stop().animate({'top':	'0px'},		myvan.site.config.rotate_social.icons_timeout);
				$('.yellow', this).stop().animate({'top':	'-32px'},	myvan.site.config.rotate_social.icons_timeout);
			});
		}
	},
	columnsOnResize: function() {
		$('.myvan-likebox').css('opacity', '0.0');
		var wid = $(window).width();
			/*
			 * resize outer und inner wrapper to force isotope relayouting on viewport resize
			 * resize inner-footer div & inner-header, so the whole page's width is not more than the total width of the columns	 *
			 * */
		if(wid <= myvan.site.config.columns.three_columns_max_width
		&& myvan.site.config.columns.two_columns) {
			//$('#register').css('display', 'none');
			$('#outer-wrapper').css('max-width', myvan.site.config.columns.two_columns_max_width);
			$('#inner-wrapper').css('max-width', myvan.site.config.columns.two_columns_max_width);
			$('#header-inner').css('width', myvan.site.config.header_footer.footer_inner_2);
			$('#header-inner').css('left', '-10px');
			$('#footer-inner').css('width', myvan.site.config.header_footer.header_inner_2);
		}
		if(wid > myvan.site.config.columns.three_columns_max_width
		&& wid <= myvan.site.config.columns.four_columns_max_width
		&& myvan.site.config.columns.three_columns) {
			//$('#register').css('display', 'block');
			$('#outer-wrapper').css('max-width', myvan.site.config.columns.three_columns_max_width);
			$('#inner-wrapper').css('max-width', myvan.site.config.columns.three_columns_max_width);
			$('#header-inner').css('width', myvan.site.config.header_footer.header_inner_3);
			$('#header-inner').css('left', '0px');
			$('#footer-inner').css('width', myvan.site.config.header_footer.footer_inner_3);
		}
		if(wid > myvan.site.config.columns.four_columns_max_width
			  && myvan.site.config.columns.four_columns) {
			//$('#register').css('display', 'block');
			$('#outer-wrapper').css('max-width', myvan.site.config.columns.four_columns_max_width);
			$('#inner-wrapper').css('max-width', myvan.site.config.columns.four_columns_max_width);
			$('#header-inner').css('width', myvan.site.config.header_footer.header_inner_4);
			$('#header-inner').css('left', '0px');
			$('#footer-inner').css('width', myvan.site.config.header_footer.footer_inner_4);
		}
		//myvan.site.main.relayout();
	},
	playerCallBack: function(callbackvar) {
		/*
		 * If the like or share icon in the player is clicked, we get a call to this function
		 * containing the action (like or share) and the article url
		 *
		 * call the respective function to handle the callback properly
		 */

		
		$('.myvan-sharebox').css('opacity', '0.0');
		
		/*if(callbackvar == 'hide' || callbackvar == 'hideFull' || callbackvar == 'mouseOut') {*/
		if(callbackvar == 'mouseOut') {
			$('#at20mc').css('display', 'none');
			$('.myvan-likebox').css('opacity', '0.0');
		}else {
			var callback = callbackvar.split('|');
			if(callback[0] == 'like') myvan.site.general.likethis(callback[1]);
			if(callback[0] == 'share') myvan.site.general.sharethisaddthis(callback[1]);
		}
	},
	likethis: function(url) {
		var likebox = $('.myvan-likebox');
		var sharebox = $('.myvan-sharebox');


		var likebox_fb = $('.myvan-likebox .fbook');
		likebox_fb.html('<div id="fb-root"></div><fb:like href="'+url+'" send="false" layout="button_count" width="60" show_faces="false" action="like" font=""></fb:like>')
        if (typeof FB  != "undefined"){
            FB.XFBML.parse(document.getElementById('facebookparserlookhere'))
        }

		/* Now we're going to render the g+1 buttons :-) */
		var likebox_gp = $('.gplus');
		likebox_gp.html('<div class="g-plusone" data-size="standard" data-count="false" data-href="'+url+'"></div>');

		/* render all buttons */
		gapi.plusone.go();

		var usediv = 'ex';
		var use_as_x_offset = 0;
		var use_as_y_offset = 0;

		if($('.article-single-image').length > 0) {
			/* we're on a single page */
			 usediv = '.article-single';
			 use_as_x_offset = myvan.site.config.likebox.article_x;
			 use_as_y_offset = myvan.site.config.likebox.article_y;
		}
		else {
			/* we're on the main screen or an archive screen */
			 usediv = '.item.box.article.active';
			 use_as_x_offset = myvan.site.config.likebox.main_x;
			 use_as_y_offset = myvan.site.config.likebox.main_y;
		}

		var xpos = $(usediv).css('left');//.replace(/[^-\d\.]/g, '');
		var ypos = $(usediv).css('top');//.replace(/[^-\d\.]/g, '');

		likebox.css('top', (parseInt(ypos) + use_as_y_offset) + 'px');
		likebox.css('left', (parseInt(xpos) + use_as_x_offset) + 'px');

		
		
		
		sharebox.animate({
			opacity: 0
		}, 10, function() {
			likebox.css('display','block');
			sharebox.css('display', 'none');
			likebox.stop().animate({
			    opacity: 1.0
			  }, 10, function() {
			    // Animation complete.
				  likebox
				  	.mouseenter(function(){

				  	})
				  	.mouseleave(function(){
				  		likebox.stop().animate({
						    opacity: 0
						  }, 10);
				  	});

			  });
			});
	},
	sharethisaddthis: function(url, player) {
		var usediv = undefined;
		var off = undefined;
		$('#at20mc').css('display', 'block');
		if(player === undefined) {
			if($('.article-single-image').length > 0) {
				/* we're on a single page */
				 usediv = '.article-single';
				 off = $(usediv).offset();

				 var plusleft = 460;
				 var plustop = 170;
			}else {
				/* we're on the main screen or an archive screen */
				 usediv = '.item.box.article.active';
				 off = $(usediv).offset();
				 var plusleft = 240;
				 var plustop = 90;
			}

			$('div.addthis').css('top', off.top + plustop);
			$('div.addthis').css('left', off.left+plusleft);
		}
		if(player === false) {
			off = $('.share a').offset();

			$('div.addthis').css('top', off.top );
			$('div.addthis').css('left', off.left - 150);
		}
		$('.addthis_button_compact').trigger('click');
	},
	behaviourSearchform: function() {
		var searchform = $('form#searchform input#s');
		searchform.focus(function(){
			var this_val = searchform.val();
			if(this_val == myvan.site.config.search.defaultValueEn || this_val == myvan.site.config.search.defaultValueDe)
				searchform.val('');
		}).blur(function(){
			var this_val = searchform.val();
			if(this_val == "") {
				if(searchform.hasClass('german'))
					searchform.val(myvan.site.config.search.defaultValueDe);
				else
					searchform.val(myvan.site.config.search.defaultValueEn);
			}
		})

	},
	disclaimer: function() {
		if($('label#accept_privacy_policy-label').length > 0) {
			$('span#privacy_policy').css('display', 'none');
			if($('body').attr('id') == 'text_german') {
				var text = 'Ich akzeptiere die <a href="http://www.myvan.com/legal-notice-de/?lang=de" target="_blank">allgemeinen Nutzungsbedingungen</a> und den <a href="http://www.myvan.com/wp-content/pdf/Code_of_Conduct_deutsch_2010.pdf" target="_blank">Datenschutz-Verhaltenskodex</a>.';
			}
			else {
				var text = 'I accept the <a href="http://www.myvan.com/legal-notice/" target="_blank">terms and conditions</a> and the <a href="http://www.myvan.com/wp-content/pdf/Code_of_Conduct_englisch_2010.pdf" target="_blank">data protection code of conduct</a>.';
			}
			$('label#accept_privacy_policy-label').children('span').html(text);
		}
			
	}
	
}
