  $(document).ready(function () {

	$("#accordion").accordion({ autoHeight: false, collapsible: true });
	$('#locale').dropdown_selector();
	$('div.custom-dropdown').dropdown_selector();
	$('body.home').homeContext();
	$('body.media').mediaContext();
	$('body.projects').projectsContext();
	
	$('body.download-center #accordion h3').bind('click', function () {
		$.scrollTo('#header', 400);
	});
	
	$(".slideshow_general").slider({
		delay : 200,
		speed : 600,
		timer : true, 
		easing : 'easeOutExpo'
	});

	if (!$("body").hasClass("contact_us") && !$("body").hasClass("media")) {
		
		if (!$("body").hasClass("sponsorships")) {
			$("#content").find('.cols-2').each(function(i){
				$(this).rightAlignColTwo('.block');
			});
		}
		
		$("#content").find('.cols-1').each(function(i){
			$(this).rightAlignColTwo('li');
		});
	}
	
	$("a.menu_item_12").click(function(){
		
		
		document.cookie = "yingli_ir"+'='+$(this).attr('index')+';path=/';
		//return false;
	});
	
	$("section.page_intro").find('p:first').addClass('first-child');
	if( $("section.page_intro").find('p').length > 2 ){
		$("section.page_intro").find('p:last').addClass('last-child');
	}
  
  
  //downloads tracking
  $('.download_link').bind("click", function(){
    _gaq.push(['_trackEvent', 'Downloads', 'Click', $(this).attr("title")]);
  })
  
  //external links tracking
  $('.external_link').bind("click", function(){
    _gaq.push(['_trackEvent', 'External Link', 'Click', $(this).attr("title")]);
  })
  
  //contact 
  $('.contacts a').bind("click", function(){
    _gaq.push(['_trackEvent', 'Email Link', 'Click', $(this).text()]);
  })
  
  //press releasess tracking
  $('.press_releases_section a').bind("click", function(){
    _gaq.push(['_trackEvent', 'Press Release Link', 'Click', $.trim($(this).text())]);
  })
  
  $('.management-bios h3').bind("click", function(){
    var label = $(this).text().replace($('.bio_title', $(this)).text(), '');
    _gaq.push(['_trackEvent', 'Management Bio', 'Click', label]);
  })
});

// Randos

$('body.legal_statement h3:first').addClass('first-child');

(function($) {
	 
	 $.fn.rightAlignColTwo = function(colType){
		$(this).find(colType+':nth-child(even)').each(function(i){
			$(this).addClass("right-col").css({"float":"right"});
		});
		
		$(this).find(colType+':nth-child(odd)').each(function(i){
			$(this).addClass("left-col");
		});
	 };
	 
	$.fn.getNewsFeed = function(storyCnt) {
		
		return $(this).each(function (i, e) {
			$(e).html('');
			$.getJSON("/feed/" + storyCnt + '/',
				function(data) {
					$.each(data, function(x, story) {
						var li = '<li><span class="date">'+story['date']+'</span><a class="png_bg" href="http://ir.yinglisolar.com/phoenix.zhtml?c=213018&p=irol-newsArticle&ID='+story['articleId'][0]+'" target="_blank">'+story['title'][0]+'</a></li>';
						$(li).appendTo(e);
					});
					
					$('.press_releases_section a').bind("click", function(){
					  
             _gaq.push(['_trackEvent', 'Press Release Link', 'Click', $(this).text()]);
           })
			 });
		});
		
	};
	
	$.fn.homeContext = function () {
		
		return $(this).each(function (i, e) {

			// Homepage slideshow
			$(".slideshow_home").slider({ 
				delay : 200,
				speed : 600,
				timer : true, 
				timer_interval : 4000,
				easing : 'easeOutExpo',
				pre_change : function(curr_slide, next_slide) { 
					var caption = $(next_slide).find('.caption').html(); 
					$(curr_slide)
						.parent().parent().find(".slideshow_caption")
						.animate({
							'opacity' : 0,
							'bottom' : '1em'
						}, 200, 'easeOutCubic', function () {
							$(this).html(caption);
						});
				},	
				post_change : function(curr_slide, next_slide) { 
					var caption = $(next_slide).find('.caption').html(); 
					$(next_slide)
					.parent().parent().find(".slideshow_caption")
						.delay(100)
						.animate({
							'opacity' : 1,
							'bottom' : '2em'
						}, 300);
				}
			});
			
			$('#press_releases').getNewsFeed(3);
			
		});
		
	};
	
	$.fn.mediaContext = function () {
		
		return $(this).each(function (i, e) {
			$("#press_releases").getNewsFeed(3);
		});
	};
	
	$.fn.projectsContext = function () {
		
		return $(this).each(function (i, e) {
			
			//init timer
			$(".slideshow_projects").slider({ 
				timer : false, 
				easing : 'easeOutExpo',
				pre_change : function(curr_slide, next_slide) { 
					var caption = $(next_slide).find('.caption').html();
					$(curr_slide)
						.parent()
						.parent()
						.find(".caption-inner")
						.fadeOut(200, function () {
							$(this).html(caption);
						});
				},	
				post_change : function(curr_slide, next_slide) { 
					var caption = $(next_slide).find('.caption').html(); 
					$(next_slide).parent().parent().find(".caption-inner").fadeIn();
				},
				first_interaction : function (slides) {
					// Lazy load
					$(slides).each(function (i, e) {
						if (!$(e).find('img').size()) {
							var img_src = $(e).attr('data-img');
							var img = $('<img />', {
								className: 'thumb',
								src: img_src
							}).prependTo($(e)); 
						}
					});
				}
			});
		
		});
		
	};
	
}(jQuery));



