// Bold
//AddThis button customization
var addthis_config = {
          services_compact: 'linkedin, facebook, twitter, email, delicious, more',
          services_exclude: 'print'
}

jQuery(document).ready(function ($) {
  if (isIE9) {
    Cufon.set('engine', 'canvas');
  }

	if (Drupal.settings.conductor.standardTheme) {
		Cufon.replace("#navigation ul.links li", { 'fontFamily': 'Kozuka Light' })
		Cufon.replace('#news #block-block-8 .menu li a, .block220px p b, .block230px-text p b, .block230px-text h2 b, .block230px-text h2, .font-heavy, #header h2, #content h2, #content h3, #content-bottom h2, h1.title, .block230px h2 b', {'fontFamily' : 'Kozuka Bold'}); 
		// Light
		Cufon.replace('.block220px p, .font-light, blockquote, .block230px-text p', {'fontFamily' : 'Kozuka Light'});
		// Medium
		Cufon.replace('.font-medium, #content-bottom h3, a.btn-careers, .block230px h2, .block230px h2 b, .block220px h2, h3.testimonial', {'fontFamily' : 'Kozuka Medium', hover: true});

		Cufon.replace('#linking-solution #container div.internal-content-603 h3', {'fontFamily' : 'Kozuka Bold'});
		Cufon.replace('#linking-solution #container .tabs-container .blockinner h3 span', {'fontFamily' : 'Kozuka Bold'});
		Cufon.replace('#linking-solution #container div.internal-content-221 h3', {'fontFamily' : 'Kozuka Bold'});
		Cufon.replace('#block-block-22 div.content', {'fontFamily' : 'Kozuka Bold'});
	}

	if ( $("#tab-container").get(0) ) {
		$("#tab-container > .block").addClass("tab-block");

		//iterate through each block chaining functions.
		var tabs = '';
		$("#tab-container > div.tab-block").each(function() {
			tabs += '<li class="' + this.id + '"><a href="#' + this.id + '">'+ $(this).find('div.title').text() +'</a></li>';
		});
		$("#tab-container").prepend('<ul class="tab-title-group">'+ tabs +'</ul>');
		$("#tab-container").tabs(); //({fxAutoheight: false, fxSlide: false, fxFade: true, fxSpeed: 180});
	}

	$(".hidden").removeClass("hidden").addClass("image-h2");

	var ul = $('#testimonials-rotate div.item-list > ul');
	ul.children('li').hide();
	ul.children('li:first').show();
});

function initTestimonials(){
	var li = $('#testimonials-rotate div.item-list > ul > li:visible');
		if ( li.next('li').length ) {
			li.fadeOut('medium', function(){
			li.next('li').fadeIn('medium')
		})
		} 
		else {
			li.fadeOut('medium', function(){
			$('#testimonials-rotate div.item-list > ul > li:first').fadeIn('medium')
		})
	}
}

jQuery(document).ready(function ($) {
	TabsInit('tab-container');
	subscribeInit();
	if ( $('blockquote').length > 0 ) {
		$("blockquote").each(function(){
			$(this).wrap("<div class='blockquote'></div>");
			if ( $(this).find('em').length > 0 ) {
				$(this).find('em:last').addClass('autor');
			}
		});
	}
});

function TabsInit(container_id) {
	var container = document.getElementById(container_id);
	if ( container )
	{
		var ul = container.getElementsByTagName('UL');
		var li = ul[0].getElementsByTagName('LI');

		li[0].className += ' first';
		li[li.length-1].className += ' last';
	}
}
function subscribeInit(){
	var mail_def = 'Enter e-mail';
	jQuery('#feed-subscribe input#input-text').focus(function(){
		if ( this.value == mail_def ) this.value = '';
	});
	jQuery('#feed-subscribe input#input-text').blur(function(){
		if ( this.value == '' ) this.value = mail_def;
	});
	jQuery('#feed-subscribe #form-error').hide();
	jQuery('#feed-subscribe input#input-submit').click(function(){
		var mail = $('#feed-subscribe input#input-text').val()
		if ( mail == mail_def ) {
			jQuery('#feed-subscribe #form-error')
				.html('<p>Please enter email address to subcribing</p>')
				.slideDown('slow')
			return false;
		} else if ( !checkSubscribe( mail ) ) {
			jQuery('#feed-subscribe #form-error')
				.html('<p>Please enter valid email address to subcribing</p>')
				.show('slow')
			return false;
		}
	});
}
function checkSubscribe( mail ) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if ( reg.test(mail) == false )
		 return false;
	else return true;
}

function rand(min, max) {
  return Math.floor((max-(min - 1))*Math.random()) + min;
}

function createRandomLine(randomPosition) {
    num = rand(1, 6);               // The line number
    time = rand(20000,65000);       // Animation duration
    origin = rand(1, 2);            // The direction origin
    topmargin = rand(0, 40);        // Randomizing heights of objects
    
    // Randomize left or right entrance
    if (origin%2) {
        // Left
leftPosition = (randomPosition) ? rand(0,swidth) : $(this).width() * -1;
$('<li class="line'+num+'"></li>').appendTo("#billboard-lines").css("left", leftPosition+"px").css("bottom", "-" + topmargin + "px").animate({ left: swidth+"px"}, time, 'linear', function() {
            $(this).remove();
        });

    } else {
        // Right
rightPosition = (randomPosition) ? rand(0,swidth) : swidth;
        $('<li class="line'+num+'"></li>').appendTo("#billboard-lines").css("left", rightPosition+"px").css("bottom", "-" + topmargin + "px").animate({ left:"-"+ $(this).width() +"px"}, time, 'linear', function() {
        $(this).remove();
        });

    }
    setTimeout("createRandomLine()",time);
}   

jQuery(document).ready(function ($) {
          
          $("#LinkMarketerApp, #LinkAgencyApp, #PublisherApp, .form-validate").validate({
		rules : {
			C_FirstName : 		{required : true, minlength: 1},
			C_LastName : 		{required : true, minlength: 2},
			C_EmailAddress :	{required : true, email: true},
			C_Company :			{required : true, minlength: 2},
			C_Website :			{required : true, minlength: 4},
			C_Title :			{required : true, minlength: 2}
		},
		messages : "This is a required field."
	});
	$(".webform-client-form").validate({
		rules : {
			'submitted[first_name]' : 	{required : true, minlength: 3},
			'submitted[last_name]'  : 	{required : true, minlength: 3},
			'submitted[email]'      :	{required : true, email: true},
			'submitted[phone_number]' :	{required : true, minlength: 3}
		},
		messages : "This is a required field."
	});
          
       

          
	$('a.openvideo').click(function(){
		var videoBlock = $(this).parents('div.block-block').find('div.video-popup');
		if (videoBlock.length < 1) return;
		videoBlock.css({
			display: 'block',
			visibility: 'hidden'
		});

		var videoTop = videoBlock > $(document).height() ? $(document).scrollTop() :
					($(document).scrollTop() + ($(window).height() - videoBlock.height())/2);

		videoBlock.css({
			top: videoTop,
			display: 'none',
			visibility: 'visible'
		}).fadeIn('medium');

		var width = $(document).width();
		var height= $(document).height();
		if ( $('div#video-fader').length < 1 ) {
			var div = $('<div />').attr('id', 'video-fader');
			$('body').append(div);
		}
		$('div#video-fader').css({
			'width'  : width,
			'height' : height,
			'opacity': 0,
			'display': 'block'
		}).animate({opacity: 0.7}, 'medium').bind('click', function(){
			closeVideoPopup();
		});
		return false;
	});

	$(document).keypress(function(e){
		if (27 == e.keyCode && $('div#video-fader:visible').length > 0) {
			closeVideoPopup();
		};
	});
    
    
    // Homepage billboard click
    $('#billboard-takecontrol').click(function(){
          var videoBlock = jQuery("#video-takecontrol");
          if (videoBlock.length < 1) return;
          videoBlock.css({
                  display: 'block',
                  visibility: 'hidden'
          });

          var videoTop = videoBlock > $(document).height() ? $(document).scrollTop() :
                                  ($(document).scrollTop() + ($(window).height() - videoBlock.height())/2);

          videoBlock.css({
                  top: videoTop,
                  display: 'none',
                  visibility: 'visible'
          }).fadeIn('medium');

          var width = jQuery(document).width();
          var height= jQuery(document).height();
          if ( jQuery('div#video-fader').length < 1 ) {
                  var div = jQuery('<div />').attr('id', 'video-fader');
                  jQuery('body').append(div);
          }
          jQuery('div#video-fader').css({
                  'width'  : width,
                  'height' : height,
                  'opacity': 0,
                  'display': 'block'
          }).animate({opacity: 0.7}, 'medium').bind('click', function(){
                  closeVideoPopup();
          });
          return false;
        });
    
    
// Load the data
$.ajax({
    url: Drupal.settings.conductor.xmlPath + "clients.xml",
    dataType: 'xml',
    success: function(data) {

        var html = '';
        var iNumImages = 0;
        var iNumImagesLoaded = 0;
        var cache = [];

        $(data).find("client").each(function(index, element) {
            iNumImages += 1;
            //_name = $(element).find("name").text();
            _logo = $(element).find("logo").text();
            cache.push($("<img />").attr("src", "http://www.conductor.com" + _logo).load(function() {
                iNumImagesLoaded += 1;
                if(iNumImages == iNumImagesLoaded) {
                    $(cache).each(function() {
                        html = html + '<li style="background-image: url(' + $(this).attr("src") + '); width: ' + $(this)[0].width + 'px">';
                    });

									if (!Drupal.settings.conductor.isFront || true) {
										$("#logos").append(html).simplyScroll({
												autoMode: 'loop',
												frameRate: 30,
												speed: 1
										}).show();
									} // end if
                } // end if
            }) // end load
        ); // end push
        }); // end each
    
    } // end success
}); //end ajax


	function closeVideoPopup() {
		$('div.video-popup').fadeOut('medium');
		$('div#video-fader').fadeOut('medium');
	}

	$('#close-popup, .close-popup').click(function(){
		closeVideoPopup();
		return false;
	});

	$('#search-block-popup > #search-init').click(function(){
		$(this).hide();
		$(this).nextAll('div#search-popup').show('medium').find('input.form-text').focus();
		if ($.browser.msie) {
			$('div.block-overlay').fadeOut('medium');
		}
		return false;
	});
    
    // Homepage Billboard
    if($("#billboard2").length > 0) {
           // Video launcher

            // background animation
            swidth = $("#billboard-wrapper").width();

	    // Create our initial events
            for (i=0;i<=6;i++) {
                createRandomLine(true);
            }

	    // Create our streaming events
	    for (i=0;i<=4;i++) {
                delay = rand(0, 35000)
	        setTimeout("createRandomLine()", delay);
            }

            $('#billboard2').cycle({
                fx: 'scrollHorz',
                easing: 'easeOutQuint',
                timeout: 9000,
			next: '#billboard-next',
			prev: '#billboard-prev'
            });

		$("#mastercycle").cycle({
			fx: 'scrollUp',
			timeout: 120000,
			autostop: 1
		});

		$(window).resize(function() {
			$("#billboard-wrapper, #billboard2, #billboard2 li").css("width", $(window).width() + "px");
		});
    }
    
    
          
    // about/careers: Employee quote cycle
    if($("#employeequotes").length > 0) {
          $("#employeequotes").cycle({
          fx: 'scrollHorz',
          timeout: 5000
      });
    }
    
    
    
});

jQuery(window).scroll(function(){
	var div = jQuery('div.video-popup:visible');
	if (div.length > 0) {
		var videoTop = div > $(document).height() ? $(document).scrollTop() :
					(jQuery(document).scrollTop() + ($(window).height() - div.height())/2);
		div.stop().animate({top: videoTop}, 'fast');
	}
});

