var wcc = {
	offset: 0,
	currentIndex: 0,
	
	init: function() {
		// hide close button
		$('#close_button').hide();
		
		// close button event
		$('#close_button').click(function() {
			$('.panel_left').slideUp('fast');
			$('.panel_right').slideUp('fast');

			$('#footer_container').animate({
				'top': '343px'
			}, function() {
				$('#footer_container').css('position', 'absolute');
				$('#copyright, #footer_links').css('padding-bottom', '20px');
			});

			$('#close_button').fadeOut('fast');

			$('#footer_content').slideDown();
			
			return false;
		});
	},
	
	icons: function() {
		// icon click event
		$('.icon, .wwd_block').click(function(e) {

			// if this is the back link on a 3rd level page, don't do anything else
			if ($(e.target).hasClass('redirect') || $(e.target).parents('div:first').hasClass('home_link')) {
				return true;
			}
			
			var link_index;
			
			// wwd block
			if ($(e.target).hasClass('wwd_block') || $(e.target).parent('div').hasClass('wwd_block')) {
				link_index = $(this).index();
				
				$('html, body').animate({
					scrollTop: 0
				}, 0);
			} else {
				link_index = $(this).index();
				
				window.location.hash = $(this).find('a').attr('href');
				
				var nice_page_name = $(this).find('a').attr('href').substring(6);

				_gaq.push(['_trackPageview', '/'+nice_page_name]);
			}

			wcc.currentIndex = link_index;

			wcc.offset = (link_index * 1100);	

			var left_offset = (($('body').width() / 2)) - (wcc.offset + 550);

			$('#panels').animate({
				'left': left_offset+'px'
			});

			$('.panel').find('.panel_overlay').fadeIn();
			$('.panel:eq('+link_index+')').find('.panel_overlay').fadeOut();

			if ($('#footer_container').css('top') == '343px') {

				// show panel content
				$('.panel_left').show();
				$('.panel_right').show();
				
				var top_offset;

				if ($('.icon_image').length > 0) {
					top_offset = $(window).height() - 122;
				} else {
					top_offset = $(window).height() - 82;
				}

				$('#footer_container').animate({
					'top': top_offset+'px'
				}, function() {
					$('#footer_container').css('position', 'fixed');
					$('#copyright, #footer_links').css('padding-bottom', '1000px');
				});

				$('#close_button').fadeIn()
			} else {
				$('html, body').animate({
					scrollTop: 0
				}, 0);
			}

			$('#footer_content').slideUp();

			return false;
		});
		
		// hover events for icons
		$.each($('.icon img'), function() {
			var path = $(this).attr('src').split('_off');
			var new_path = path[0] + "_on.png";

			var img = new Image();
			img.src = new_path;
		});

		$('.icon').mouseover(function() {
			try {
			  	var path = $('img', this).attr('src').split('_off');
				var new_path = path[0] + "_on.png";
				$('img', this).attr('src', new_path);
			} catch(err) {}
			$('p:last, p:last a', this).css('color', '#737373');
		});

		$('.icon').mouseout(function() {
			try {
				var path = $('img', this).attr('src').split('_on');
				var new_path = path[0] + "_off.png";
				$('img', this).attr('src', new_path);
			} catch(err) {}
			$('p:last, p:last a', this).css('color', '#AFAFAE');
		});
	},
	
	// query string nav based on index number
	navigation: function() {
		if (window.location.href.indexOf('?') < 0) return;
		
		if (window.location.href.indexOf('?thankyou') >= 0) return;
		
		var link_index = window.location.href.split('?');
		link_index = link_index[1].split('#');
		link_index = link_index[0];
		
		if (link_index == '' || link_index == undefined) {
			return;
		}
		
		wcc.currentIndex = link_index;

		wcc._move_to_panel();
	},
	
	// detect hash when coming from another site
	hash_detect: function() {
		if (window.location.href.indexOf('?') > -1) return;
		
		if (window.location.hash.length > 1) {
			var posttitle = window.location.hash.substring(6); // to remove #page- from hash string
			var matching_panel_index = $('.panel[id="'+posttitle+'"]').index();
			
			wcc.currentIndex = matching_panel_index;
			
			_gaq.push(['_trackPageview', '/'+posttitle]);
		} else {
			return;
		}
		
		wcc._move_to_panel();
	},
	
	// links that go to other panels in the same section
	same_section_links: function() {
		$('#panels, .text_overlay').live('click', function(e) {
			
			if (e.target.href.indexOf('#') > -1) {
				var hash = e.target.href.split('#');
				
				if (window.location.hash.length > 1 && hash[1] != window.location.hash) {
					
					var posttitle = hash[1].substring(5); // to remove page- from href string
					var matching_panel_index = $('.panel[id="'+posttitle+'"]').index();

					wcc.currentIndex = matching_panel_index;
					
					// close overlay if needed
					if ($('.text_overlay').is(':visible')) {
						wcc.close_overlay();
					}
					
					wcc._move_to_panel();
				
				}
			}
		});
	},
	
	// helper for previous three methods
	_move_to_panel: function() {
		wcc.offset = (wcc.currentIndex * 1100);	

		var left_offset = (($('body').width() / 2)) - (wcc.offset + 550);

		// $('#panels').css('left', left_offset+'px');
		
		// animate over to the right panel, not sure if this is a good idea...
		$('#panels').css({ // *** this was animate before
			'left': left_offset+'px'
		});

		$('.panel').find('.panel_overlay').show();
		$('.panel:eq('+wcc.currentIndex+')').find('.panel_overlay').hide();

		// show panel content
		$('.panel_left').show();
		$('.panel_right').show();
		
		var top_offset;

		if ($('.icon_image').length > 0) {
			top_offset = $(window).height() - 122;
		} else {
			top_offset = $(window).height() - 82;
		}

		$('#footer_container').css({
			'top': top_offset+'px'
		});
		
		$('#footer_container').css('position', 'fixed');
		$('#copyright, #footer_links').css('padding-bottom', '1000px');

		$('#close_button').show();
		
		$('#footer_content').hide();
		
		$('html, body').animate({
			scrollTop: 0
		}, 0);
	},
	
	slide: function(window_resize) {
		var width = ($('.panel').length * 1100) + 'px';
		
		var left_offset, center_offset;
		
		center_offset = 550;
		
		$('.panel:eq(0)').find('.panel_overlay').hide();
		
		if (window_resize) {
			if (wcc.offset == 0) {
				left_offset = ($('body').width() / 2) - (wcc.offset + center_offset);
			} else {
				left_offset = ($('body').width() / 2) - ((wcc.offset-1100) + center_offset);
			}
		} else {
			left_offset = ($('body').width() / 2) - (center_offset);
		}

		$('#panels').css({
			'width': width,
			'left': left_offset+'px',
			'position': 'relative'
		});
	},
	
	accordian: function() {
		$('#accordian .accordian_content').hide();

		$('#accordian h3').click(function() {
			$('#accordian .accordian_content').slideUp('fast');
			$('#accordian span').text('+');
			$('#accordian h3 a').css('color', '#45a1dd');

			if ($(this).next('.accordian_content').is(':visible')) {
				return false;
			}

			$(this).next('.accordian_content').slideDown('fast');
			$('span', $(this)).text('–');
			$(this).find('a').css('color', '#8ab5d2');

			return false;
		});
	},
	
	preload: function() {
		var images = ['wwd_shape1_over.png', 'wwd_shape2_over.png'];
		$.each(images, function() {
			var img = new Image();
			img.src = 'http://www.wcclinical.com/wp-content/themes/worldcare/images/'+this;
		});
	},
	
	overlays: function() {
		$('.overlay').click(function() {
           
            // build lightbox
            var image_path = $(this).attr('href');
			var caption = $(this).children('img').attr('alt');

            // load image before opening overlay
            $('<img />')
                .attr('src', image_path)
                .load(function() {
                    
                    // build html for overlay
                    var content = '<div class="overlay_container thumbnail_overlay">';
                    content += '<img src="'+image_path+'" />';
                    content += '<a class="close_overlay" href="#" title="Close Overlay">';
                    content += '<img src="http://www.wcclinical.com/wp-content/themes/worldcare/images/close_overlay.png" alt="close overlay" />';

					if (caption != '') {
						content += '</a><p>'+caption+'</p></div>';
					} else {
						content += '</a></div>';
					}
                    
                    content = $(content);
                    
                    $('<div id="overlay"></div>')
                        .appendTo('body')
                        .hide()
                        .fadeIn(100, function() {
                            $('body').append(content);
                            
                            // set overlay size based on image size
                            $('.thumbnail_overlay').css('width', $('.thumbnail_overlay img').width());
                            $('.thumbnail_overlay').css('marginLeft', -$('.thumbnail_overlay img').width()/2);
                        });
                });
            
            return false; 
        });

		$('.video_overlay_link').click(function() {

            // build html for overlay
            var content = '<div class="overlay_container video_overlay">';
            content += '<iframe src="http://player.vimeo.com/video/20889930?title=0&amp;byline=0&amp;portrait=0" width="720" height="405" frameborder="0"></iframe>';
            content += '<a class="close_overlay" href="#" title="Close Overlay">';
            content += '<img src="http://www.wcclinical.com/wp-content/themes/worldcare/images/close_overlay.png" alt="close overlay" /></a></div>';
            
            content = $(content);
            
            $('<div id="overlay"></div>')
                .appendTo('body')
                .hide()
                .fadeIn(100, function() {
                    $('body').append(content);
                });
            
            return false; 
        });
	},
	
	trialdata: function() {
		$('#trialdata img').each(function() {
			var that = $(this);
			
			var img = new Image();
			img.src = that.attr('data-rollover');
			
			$(this).mouseover(function() {
				that.attr('src', that.attr('data-rollover'));
			});
			
			$(this).mouseout(function() {
				that.attr('src', that.attr('data-normal'));
			});
			
			$(this).click(function(e) {
				var block = e.target.id;
				
				// build html for overlay
                var content = '<div class="overlay_container text_overlay">';
                content += wcc.trialdata_content[block];
                content += '<a class="close_overlay" href="#" title="Close Overlay">';
                content += '<img src="http://www.wcclinical.com/wp-content/themes/worldcare/images/close_overlay.png" alt="close overlay" />';
				content += '</a></div>';
                
                content = $(content);
                
                $('<div id="overlay"></div>')
                    .appendTo('body')
                    .hide()
                    .fadeIn(100, function() {
                        $('body').append(content);
                    });
					
				return false;
			});
		});
	},
	
	close_overlay_triggers: function() {
        // close by cancel link or clicking black overlay
        $('.close_overlay, #overlay').live('click', function() {
            wcc.close_overlay();
            
            return false;
        });
        
        // close by hitting escape key
        $(document).keyup(function(e) {
            if (e.keyCode == 27) {
                wcc.close_overlay();
            }
            
            return false;
        });
    },
    
    close_overlay: function() {
        $('#overlay').fadeOut(100, function() {
            $(this).remove();
        });
        $('.overlay_container').fadeOut(100, function() {
            $(this).remove();
        });
    },

	back_to_top: function() {
		$('.back_to_top').click(function() {
			$('html, body').animate({
				scrollTop: 0
			}, 250);
			
			return false;
		});
	},

	trialdata_content: {
		'block1': '<p><strong>Technology that gives you control</strong></p><p>WCC’s WorldPro image management system ensures that data validity is automatic.  Among the most advanced image transmission technology in the clinical trial world, it facilitates the entire trial from initial image acquisition and secure upload to transfer and review, all under complete control of the sponsor with only the click of a mouse.  A comprehensive web portal centralizes all trial activity between sites, sponsors, WCC project managers, and the rest of the study team to ensure total 24-hour visibility from anywhere in the world.</p><p>DICOM header-scanning capabilities and eCRFs with specific Imaging Acquisition Protocol guidelines enable the system to perform its own edit checks and ensure inaccurate or invalid data is never accepted to the study. And because sponsors can log on to view every piece of the trial from image data to queries to project status at any time of the day or night, they can ensure validity of the data and processes as the study moves forward.  Total visibility means total control, thanks to WorldPro’s smart checks and automated QC.</p><p><a href="http://www.wcclinical.com/#page-WorldProPlatform">Read more about WorldPro</a></p>',
		'block2': '<p><strong>Training and Testing to ensure reader accuracy</strong></p><p>WCC’s approach to reader performance is grounded in a statistical approach to imaging as a science.  With precise tools and stringent training, WCC brings a strong expertise and finely-honed approach to image analysis that enables better precision and more uniform technique across the entire group of trial radiologists.</p><p>Selective performance testing throughout the trial also allows WCC to monitor both intra- and inter-reader variability and address inconsistencies or aberrations in analysis. Ensuring reads are completed properly and uniformly ensures that data is not only valid, but as accurate as possible.  With control over reader training and performance comes control over the data that results.</p><p><a href="http://www.wcclinical.com/#page-Committed-toTraining">Read more about why we believe in training</a></p>',
		'block3': '<p><strong>Real-time Insights</strong></p><p>WCC understands that around the clock, real-time visibility is crucial to efficient trial workflow and management.  Sponsors need access to trial-specific data on-demand, and WCC’s WorldPro provides a secure platform for this access from anywhere in the world.  Sponsors can track progress and ensure data is not only valid, but is obtained and handled according to protocol, with views into open study queries, image tracking forms, image thumbnails, and a variety of reports in multiple formats.   No matter where you are in the world, WCC enables live interaction with your study and affords complete control over every step of the process. With real-time insights into trial activity, you can ensure data quality from beginning to end with complete confidence.</p><p><a href="http://www.wcclinical.com#page-WorldProPlatform">Learn more about real-time trial views in WorldPro</a></p>',
		'block4': '<p><strong>Control drift with superior data management and biostatistics services</strong></p><p>WCC’s intensive attention to statistical insight and data management offers sponsors total control over trial data that comes through expert service offerings over the course of the study process.</p><p>Statistical analysis planning ensures a commitment to operational and clinical metrics right from the trial’s outset, and WCC executes on this planning with reader training and testing, the implementation of performance metrics, and sophisticated data management capabilities throughout the study. Our data management services ensure efficient and compliant capture, transfer, and storage of all measurements and associated project documentation for complete transparency and security control. With safe and secure data management, you can be assured your data is completely clean, protected, and unchanged from acquisition to review. Imaging data remains accurate data under WCC control.</p><p><a href="http://www.wcclinical.com/our-expertise/#page-Biostatistics-&-Data-Management">Read more about biostatistics and data management services</a></p>'
	}
}

$(function() {
	wcc.init();
	wcc.icons();
	wcc.accordian();
	wcc.preload();
	wcc.slide();
	wcc.overlays();
	wcc.trialdata();
	wcc.close_overlay_triggers();
	wcc.back_to_top();
	
	wcc.navigation();
	wcc.hash_detect();
	wcc.same_section_links();
	
	$(window).resize(function() {
	  	wcc.slide(true);
	});
});
