// first time introduction
//var cpn_2009_firsttime = 0;
//imgLoader = new Image();// preload image
//imgLoader.src = "/files/gfx/firsttime.jpg";

//function createCookie(name,value,days) {
//	if (days) {
//		var date = new Date();
//		date.setTime(date.getTime()+(days*24*60*60*1000));
//		var expires = "; expires="+date.toGMTString();
//	}
//	else var expires = "";
//	document.cookie = name+"="+value+expires+"; path=/";
//}

//function readCookie(name) {
//	var nameEQ = name + "=";
//	var ca = document.cookie.split(';');
//	for(var i=0;i < ca.length;i++) {
//		var c = ca[i];
//		while (c.charAt(0)==' ') c = c.substring(1,c.length);
//		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
//	}
//	return null;
//}

//function eraseCookie(name) {
//	createCookie(name,"",-1);
//}

//if(readCookie("cpn_2009_firsttime") != 1)	{
//	createCookie("cpn_2009_firsttime",1,6000)
//}
//else	{
//	cpn_2009_firsttime = 1;
//}

//createCookie(name,"",-1);
// lazy loading js files when needed
var curlanguages = new Array('hl','da','no','pt','fi','sv','ru');

// Check if link coming from cps
for (var i=0; i<curlanguages.length; i++) {
	if (window.location.href.indexOf("content/" + curlanguages[i] + "/") >= 0 &&
		window.location.href.indexOf('services') == -1) {
			var newUrl = window.location.href.replace("content/" + curlanguages[i] ,"content");
			window.location.replace(newUrl);
	}
}

loopLoading();

function loopLoading()	{
	if (typeof jQuery != 'undefined') {
		loadingExternals();
	}
	else	{
		setTimeout(loopLoading, 100);
	}
}

function loadingExternals() {
	$(document).ready(function()	{
		$ = jQuery;
  if ($(".ceos").length > 0) $.getScript("/files/js/jquery.ceos.js");


	// CINEMA EOS HACK
		if(window.location.href.indexOf("product/cinema_eos") >= 0) {
			$('#title_bar h1 span').html('Cinema EOS');
		}
		if(window.location.href.indexOf("index") >= 0) {
		  $('a[href*="product/cinema_eos"]').each(function(){
			$(this).parent().find('h2').html('Cinema EOS');
		  });
		}

	// CPS Hack
		if(window.location.href.indexOf("/services.do") >= 0) {
			$('#highlights h2').html('Click on your country flag to go to your local News & Events page:');
		}
		$('#related_links a[href*="product/cinema_eos"]').each(function(){
			var $text = ($(this).html().split(':'));
			$(this).html('Cinema EOS: '+$text[1]);
		});

	//EC admin hack
		if ($("body#photo_selection").length > 0) { $.getScript("/files/js/recovery.js"); }			

		//disable right click
		$("img").live("contextmenu",function(e){
			e.preventDefault();
			alert("Please note - this image is protected by copyright. CPN kindly requests that you do not copy it.");
			return false;
			});
			$("img").live("mousedown",function(e){
			if ($(this).attr('class') != 'nsohandle') {
			e.preventDefault();
			return false;
			}
		});

		//header
		$.getScript("/files/js/jquery.header.js");
		//footer
		$.getScript("/files/js/jquery.footer.js");
		//search
		$.getScript("/files/js/jquery.search.js");
		//pager
		if ($('div.page').length > 1) {	$.getScript("/files/js/jquery.pager.js"); }

		$('a').each( function() {
			 var ttle= $(this).attr('title'); if (ttle.indexOf('<')>=0)  $(this).removeAttr('title');});
		$('img').each( function() {
			 var ttle= $(this).attr('title'); if (ttle.indexOf('<')>=0)  $(this).removeAttr('title');});

		// video overlay play button
		if ($(".overview_container_5").length > 0) { // small overview
			var alinks = $(".overview_container_5").find(".thickbox");
			for (var i=0; i<alinks.length; i++) {
				if (alinks[i].href.indexOf('flv')< 0) {
					$(alinks[i]).removeClass('thickbox');
					var newUrl = './content/' + alinks[i].href.substring(alinks[i].href.indexOf('videoUrl=/files/')+16,alinks[i].href.lastIndexOf('/')) + '.do';
					$(alinks[i]).attr('href',newUrl);
				}
			}
			var imgUrl = "./files/gfx/icons/video_play_button.png";
			var containers = $(".overview_container_5").find(".thickbox").find("img");
			for (var i=0; i<containers.length; i++) {
				jQuery('<img class="video" src="' + imgUrl + '" alt="" title="" />').appendTo($(containers[i]).parent());
			}
		}
		if ($(".overview_container_3").length > 0) { // big overview
		    $('img').removeAttr('title');
			var alinks = $(".overview_container_3").find(".thickbox");
			for (var i=0; i<alinks.length; i++) {
				if (alinks[i].href.indexOf('flv')< 0) {
					$(alinks[i]).removeClass('thickbox');
					var newUrl = './content/' + alinks[i].href.substring(alinks[i].href.indexOf('videoUrl=/files/')+16,alinks[i].href.lastIndexOf('/')) + '.do';
					$(alinks[i]).attr('href',newUrl);
				}
			}
			var imgUrl = "./files/gfx/icons/video_play_button_big.png";
			var containers = $(".overview_container_3").find(".thickbox").find("img");
			for (var i=0; i<containers.length; i++) {
				jQuery('<img class="video" src="' + imgUrl + '" alt="" title="" />').appendTo($(containers[i]).parent());
			}
		}
				
		if ($("#video_home").length > 0) { // large overview
			var alinks = $("#video_home").find(".thickbox");
			for (var i=0; i<alinks.length; i++) {
				if (alinks[i].href.indexOf('flv')< 0) {
					$(alinks[i]).removeClass('thickbox');
					var newUrl = './content/' + alinks[i].href.substring(alinks[i].href.indexOf('videoUrl=/files/')+16,alinks[i].href.lastIndexOf('/')) + '.do';
					$(alinks[i]).attr('href',newUrl);
				}
			}
			var imgUrl = "./files/gfx/icons/video_play_button_big.png";
			var containers = $("#video_home").find("img");
			for (var i=0; i<containers.length; i++) {
				jQuery('<img class="video" src="' + imgUrl + '" alt="" title="" />').appendTo($(containers[i]).parent());
			}
		}

		if ($(".visual").length > 0) { // archive
			var alinks = $(".visual").find(".thickbox");
			for (var i=0; i<alinks.length; i++) {
				if (alinks[i].href.indexOf('flv')< 0) {
					$(alinks[i]).removeClass('thickbox');
					var newUrl = './content/' + alinks[i].href.substring(alinks[i].href.indexOf('videoUrl=/files/')+16,alinks[i].href.lastIndexOf('/')) + '.do';
					$(alinks[i]).attr('href',newUrl);
				}
			}
		}
		if ($(".overview_container_2_horizontal").length > 0) { // archive
			var alinks = $(".overview_container_2_horizontal").find(".thickbox");
			for (var i=0; i<alinks.length; i++) {
				if (alinks[i].href.indexOf('flv')< 0) {
					$(alinks[i]).removeClass('thickbox');
					var newUrl = './content/' + alinks[i].href.substring(alinks[i].href.indexOf('videoUrl=/files/')+16,alinks[i].href.lastIndexOf('/')) + '.do';
					$(alinks[i]).attr('href',newUrl);
				}
			}
			var imgUrl = "./files/gfx/icons/video_play_button.png";
			var containers = $(".overview_container_2_horizontal").find(".thickbox").find("img");
			for (var i=0; i<containers.length; i++) {
				jQuery('<img class="video" src="' + imgUrl + '" alt="" title="" />').appendTo($(containers[i]).parent());
			}
		}

		if ($(".scrolling_home").length > 0) { // archive
			var alinks = $(".scrolling_home").find(".thickbox");
			for (var i=0; i<alinks.length; i++) {
				if (alinks[i].href.indexOf('flv')< 0 && alinks[i].href.indexOf('tipsandtricks') < 0 ) {
					$(alinks[i]).removeClass('thickbox');
					var newUrl = './content/' + alinks[i].href.substring(alinks[i].href.indexOf('videoUrl=/files/')+16,alinks[i].href.lastIndexOf('/')) + '.do';
					$(alinks[i]).attr('href',newUrl);
				}
			}
		}
		// sliding list
		if ($("ul.expandableList").length > 0) { $.getScript("/files/js/jquery.expandable.js"); }
				
		// overlay js
		$("a[rel='blackbox']").addClass("thickbox").attr("rel","").each(function() {
			if($(this).parent().siblings(".image_entities").children("span").length > 0)
				$(this).attr("title", $(this).parent().siblings(".image_entities").children("span:first").html() );
			else
				$(this).attr("title", $(this).parent().siblings(".image_entities").html() );
		});
				
		if ($('a.thickbox').length > 0) {
			$.getScript("/files/js/jquery.thickbox.js", function(){
				var hu = window.location.search.substring(1);
				if (hu && $("#" + hu).length > 0 && $("#" + hu).attr('href').indexOf('videoplayer.swf')>0) tb_show($("#" + hu).attr('title'),$("#" + hu).attr('href'),$("#" + hu).attr('rel'),$("#" + hu));
			});
		}			
		// images zoom ICONS ONLY - popup functionality is handled by thickbox
		if ($('.blackbox').length > 0) {
			$.getScript("/files/js/jquery.blackbox.js", function(){});
		}
				
		// tabs
		if ($(".tabs").length > 0) { $(".tabs").tabs(); }
		if ($("h2").find("span").length>=0) {
		  var spns = $("h2").find("span");
		  for (var i=0; i<spns.length; i++) {
			if ($(spns[i]).text()=='??????') $(spns[i]).text('CPN')
		  }
		}
				
		// filtering list (still needed?)
		if ($("ul.filter").length > 0) { $.getScript("/files/js/jquery.filter.js"); }

		// Remove unknown sections (STILL NEEDED??)
		if ($("a:contains(??????)").length>=0) { 
			var els = $("a:contains(??????)");
			for (var i=0; i<els.length; i++) {
			  //if ($.contains($(els[i]).text(),'??????'))
			  $(els[i]).text($(els[i]).text().replace(/\?\?\?\?\?\?/,'CPN'));
			}
		}
				
		// visa page (WHY?)
		if ($(".visa_article").length > 0) {
			$("#slider").find("img").click(function(){document.location='/content/news/cpn_at_visa_2010.do'});
		}

		if ($("body#visa2010").length > 0) { $.getScript("/files/js/jquery.video_slider.js");}
				
		if ($("body#editors_current").length > 0) { $.getScript("/files/js/jquery.video_slider.js");}
				
		if ($("body#ambassadors_exhibition").length > 0 || $("body#index").length > 0) {
			$.getScript("/files/js/jquery.homepage.js");
			$.getScript("/files/js/jquery.cycle.all.min.js",function(){
				$.fn.cycle.defaults.speed   = 900;
				$.fn.cycle.defaults.timeout = 6000;
				
				$(function() {
					$('#demos pre code').each(function() {
						eval($(this).text());
					});
					$('#demos_product pre code').each(function() {
						eval($(this).text());
					});
				});
			});
		}

		if ($("body#index").length > 0) {
			$.getScript("/files/js/jquery.easing.1.1.1.js", function() {
				$('.slideshow img:first').fadeIn(1000, function() {
					$('#block_banner').cycle({
						fx: 'blindY', 
						before: function(curr, next, opts) {
							opts.animOut.opacity = 0;	
						}
					});
					$('#slideshow').cycle({
						//fx:    'scrollDown,scrollUp', 
						fx:    'scrollLeft', 
						randomizeEffects: false,
						easeIn:  'backin', 
						easeOut: 'backin', 
						speed:   900, 
						timeout: 6000,  // milliseconds between slide transitions (0 to disable auto advance) 
						delay:   -1000 // additional delay (in ms) for first transition (hint: can be negative)
					});
				});
			});
		}
				
		if ($("body#product_new").length > 0 || $("body#product_sheet").length > 0) {
			$.getScript("/files/js/jquery.cycle.all.min.js");
			$.getScript("/files/js/jquery.easing.1.1.1.js", function() {
				$('.slideshow img:first').fadeIn(1000, function() {
					$('#slideshow').cycle({
						fx:    'scrollLeft', 
						randomizeEffects: false,
						easeIn:  'backin', 
						easeOut: 'backin', 
						speed:   900, 
						timeout: 4000,  // milliseconds between slide transitions (0 to disable auto advance) 
						delay:   -1000 // additional delay (in ms) for first transition (hint: can be negative)
					});
				});
			});
		}
				
				
		if ($("body#your_space").length > 0) {
			$.getScript("/files/js/jquery.cycle.all.min.js",function(){
				$(function() {
					$('.slideshow img:first').fadeIn(1000, function() {
						$('#slideshow').cycle({
							fx: 'fade', 
							before: function(curr, next, opts) {
								opts.animOut.opacity = 0;	
							}
						});
					});
				});
			});
		}

		if ($("body#kitbag").length > 0) {
			$.getScript("/files/js/jquery.cycle.all.min.js",function(){
				$(function() {
					$('.slideshow img:first').fadeIn(1000, function() {
						$('#slideshow').cycle({
							fx:     'fade', 
							speed:  'fast'
						});
					});
				});
			});
					
			$.getScript("/files/js/zoomer.jquery.js",function(){
				$(function() {
					$("#wrap").anythingZoomer({
					   expansionSize: 30,
					   speedMultiplier: 2.2
					});
				});
			});
		}
				
		if ($("body#arctic_blogs").length > 0) {
			$.getScript("/files/js/jquery.cycle.all.min.js",function(){
				$.fn.cycle.defaults.speed   = 900;
				$.fn.cycle.defaults.timeout = 6000;
				$(function() {
					$('#slideshow').before('<ul id="nav">').cycle({
						fx:     'fade',
						speed:  'fast',
						timeout: 0,
						pager:  '#nav',
						pagerAnchorBuilder: function(idx, slide) {
							return '<li><a href="#"><img src="' + slide.src + '" width="50" height="50" /></a></li>';
						}
					});
				});
			});
		}
		if ($(".btn-slide").length > 0) {
			$(function(){
				$(".btn-slide").click(function(){
					$("#panel").slideToggle("slow");
					$(this).toggleClass("active"); return false;
				});
			});
		}
		if ($("body#tabbing_nav").length > 0) {
			$.getScript("/files/js/jquery.cycle.all.min.js",function(){
				$.getScript("/files/js/tabbing_nav.js");
			});
			
		}
		
		if ($("body#editors_previous").length > 0) {
			$("#introVidLink").click(function(){playInDiv("introVid")});
		}
		if ($("body#editors_current").length > 0) {
			$("#introVidLink").click(function(){playInDiv2("introVid")});
		}
		if ($("body#editors_choice").length > 0) {
			$("#introVidLink").click(function(){playInDiv3("introVid")});
		}
		
		if ($("body#visa2010").length > 0) {
			$("#introVidLink").click(function(){playInDiv4("introVid")});
		}
		
		if ($("select.customSelect").length > 0) {
			$.getScript("/files/js/jquery.customselect.js");
		}
		
		//editor section script
		if($("body#editors_choice_3").length > 0 || window.location.pathname.indexOf("/editors/") > -1)	{
			$.getScript("/files/js/jquery.cpn-ec.js");
			$.getScript("/files/js/jquery.masonry.min.js");
			$.getScript("/files/js/jquery.infinitescroll.min.js");
			$.getScript("/files/js/jquery.ec.scrollingslideshow.js");
		
			if($("#vote_box").length > 0)	{
				$.getScript("/dwr/engine.js");
				$.getScript("/dwr/util.js"); 
				$.getScript("/editors/dwr/interface/VotingAjax.js");
			}
		}
				
		if ($("#tips_container").length > 0) {
			$.getScript("/files/js/jquery.tips.js");
		}

		//mouse over image change
		if ($(".illustrationOver").length > 0) {
			$(".illustrationOver").hover(function()	{
				$(this).closest("div.overview_list_body").find("li").removeClass("active");
				$(this).parent("li").addClass("active").closest("div").find("div.illustration > img").attr("src", $(this).attr("rel"));
			});
		}
		// new 2011 related links scroller
			  
		if($('body#editors_choice_3 #related_links dd').length > 0) {
			$('#related_links dd').append('<div id="arrow_prev"></div><div id="arrow_next"></div>');		
			$('#related_links ul').width($('#related_links li').length*($('#related_links li').width()+parseInt($('#related_links li').css('margin-right')) ));
			$('#related_links #arrow_next').click(function(){
				if($('#related_links ul:animated').length < 1){
					var newMargin = (parseInt($('#related_links ul').css('margin-left'))-792)*-1;
					if(newMargin < $('#related_links ul').width() )
						$('#related_links ul').animate({marginLeft: '-=792'}, 500, 'easeInOutExpo');
				}
			});
			
			$('#related_links #arrow_prev').click(function(){
				if($('#related_links ul:animated').length < 1){
					if(parseInt($('#related_links ul').css('margin-left'))+792 <= 0 )
						$('#related_links ul').animate({marginLeft: '+=792'}, 500, 'easeInOutExpo');
				}
			});
		}
	});
}

function playInDiv(divID)	{
	$("#"+divID).flash(
        { 
          src: './files/swf/videoplayer.swf?'+Date().valueOf(),
          width: 960,
          height: 540,
          wmode: 'transparent',
          allowFullScreen: true,
          flashvars: { videoUrl: '/files/yourspace/editors_choice/james_colton/Jim7.flv' }
        },
        { version: 9 }
    );
}

function playInDiv2(divID)	{
	$("#"+divID).flash(
        { 
          src: './files/swf/videoplayer.swf?'+Date().valueOf(),
          width: 960,
          height: 540,
          wmode: 'transparent',
          allowFullScreen: true,
          flashvars: { videoUrl: '/files/yourspace/editors_choice/Edit_8.flv' }
        },
        { version: 9 }
    );
}

function playInDiv3(divID)	{
	$("#"+divID).flash(
        { 
          src: './files/swf/videoplayer.swf?'+Date().valueOf(),
          width: 960,
          height: 540,
          wmode: 'transparent',
          allowFullScreen: true,
          flashvars: { videoUrl: '/files/news/editors_choice_judging/interviews_editors.flv' }
        },
        { version: 9 }
    );
}

function playInDiv4(divID)	{
	$("#"+divID).flash(
        { 
          src: './files/swf/videoplayer_loop.swf?'+Date().valueOf(),
          width: 960,
          height: 540,
          wmode: 'transparent',
          allowFullScreen: true,
          flashvars: { videoUrl: '/files/visa2010/visa2010.flv' }
        },
        { version: 9 }
    );
}

function executeThisAction(e,thisId, thisAction) {
	  var note = $(e).find('textarea').val();
	  $.ajax({
		type: "POST",
		data: {action: thisAction, note: note},
		url: '/admin/content/ajax/executeAction.do?content.contentId=' + thisId,
		dataType: "text",
		error: function() {
			alert('could not execute the request');
		},
		success: function(text) {
			if (text.indexOf('error')>-1) return alert('when denying a request a Remark is required');
			if ($(".admin_table").length==0) document.location.reload();
			else $(e).parent().parent().parent().hide();
		}
	  });
}
function popUpEcTerms(url, t_width, t_height) {
	  tb_show(' ',url+'?width=800&height=600',null,null);
}

