function initContentVideos() {
	//Homepage
	$('#leftGrid a:eq(0)').click(function() {
		$(this).find('img').hide();
		$(this).find('span').remove();
		$('div#video-home-tilda').css({'display':'inline-block'});
		return false;
	});
	$('#leftGrid a:eq(3)').click(function() {
		$(this).find('img').hide();
		$(this).find('span').remove();
		$('div#video-home-shrigley').css({'display':'inline-block'});
		return false;
	});		
	
	//Collection Pages
	$('div#collection-list div#video-collection img').click(function() {
		$(this).hide();
		$('div#video').css({'display':'inline-block'});
		return false;
	});	
}

(function($) {
    $.fn.extend({
      modalPanel: function(mode) {

          //Create our overlay object
          if (mode=="popupvideo") {
        	  var modalWindow = $("<div id='modal-popupwindow'></div>");
          } else if (mode==" ") {
        	  var modalWindow = $("<div id='modal-window-wide'></div>");  
          } else {
        	  var modalWindow = $("<div id='modal-window'></div>");
          }
          var overlay = $("<div id='modal-overlay'></div>");
          
          var imageWidth;
          var imageHeight;
          
          return this.each(function() {

              //Listen for clicks on objects passed to the plugin
              $(this).click(function(e) {

                  //Append the overlay to the document body
                  $("body").append(overlay.click(function() { 
                      modalHide(); 
                  }))
                  $("body").append("<div id='modal-load'></div>");

                  //Set the css and fade in our overlay
                  overlay.css("opacity", 0.6);
                  overlay.fadeIn(150);

                  //Prevent the anchor link from loading
                  e.preventDefault();

                  //Activate a listener 
                  $(document).keydown(handleEscape);
                  
                  var name=this.name;
                  var title=this.title;
                  
                  if (mode=="popupvideo") {
                  	var videoDiv = $("<div id='modal-video'></div>");
                  	var closeDiv = $("<div id='cboxClose'></div>");
                  	var videourl = this.href;
                  	 

                  	var divWidth = 250;
                      var divHeight = 240;
                      var vidWidth = 470;
                      var vidHeight = 320;
                      imageWidth = divWidth - (vidWidth / 2) ;
                      imageHeight = divHeight - (vidHeight / 2) - 60;
                      modalWindow.css({
                      	"margin-left": -divWidth,
                      	"margin-top": -divHeight
                      });	
                      modalWindow.append("<h2>" + title + "</h2>");
                      modalWindow.append(closeDiv.click(function() {modalHide();}));
                      modalWindow.append(videoDiv);
                      videoDiv.css({"margin-left": imageWidth, "margin-top": imageHeight});
                      $("body").append(modalWindow);
                      modalWindow.append("<p class='name'>" + name + "</p>");
                      //modalWindow.append("<p class='title'>" + title + "</p>");
                      Cufon('div#modal-popupwindow h2');
                      //$.get(videourl, function(data){videoDiv.html(data); $("#modal-load").remove(); modalWindow.fadeIn(150);});
                      videoDiv.html("<div id='flash_container'><img src='/pws/client/images/homepage/content/hawick_to_hollywood.jpg'/><div class='no_flash'>Apologies for any inconvenience, to view this media you will need to have JavaScript enabled and Flash Player 9 or above.</div></div><script type='text/javascript'>var so = new SWFObject('/pws/jwplayer/player.swf','mpl', '470', '320','9');  so.addParam('allowfullscreen','true');  so.addParam('allowscriptaccess','always');  so.addParam('wmode','opaque');  so.addVariable('file','" + videourl + "');  so.addVariable('skin','/pws/client/video/stormtrooper.zip');  so.addVariable('autostart','true');  so.write('flash_container'); </script>");
                    		  
                      $("#modal-load").remove();
                      modalWindow.fadeIn(150);
                    }
                  
                  if (mode=="idvideo") {
                	var videoDiv = $("<div id='modal-video'></div>");
                	var closeDiv = $("<div id='cboxClose'></div>");
                	var videourl = this.href;
                	 

                	var divWidth = 250;
                    var divHeight = 240;
                    var vidWidth = 405;
                    var vidHeight = 255;
                    imageWidth = divWidth - (vidWidth / 2) ;
                    imageHeight = divHeight - (vidHeight / 2) - 60;
                    modalWindow.css({
                    	"margin-left": -divWidth,
                    	"margin-top": -divHeight
                    });	
                    modalWindow.append("<h2>i-D Magazine</h2>");
                    modalWindow.append(closeDiv.click(function() {modalHide();}));
                    modalWindow.append(videoDiv);
                    videoDiv.css({"margin-left": imageWidth, "margin-top": imageHeight});
                    $("body").append(modalWindow);
                    modalWindow.append("<p class='name'>" + name + "</p>");
                    modalWindow.append("<p class='title'>" + title + "</p>");
                    //Cufon('div#modal-window h2');
                    $.get(videourl, function(data){videoDiv.html(data); $("#modal-load").remove(); modalWindow.fadeIn(150);});
                  } else {
                  
                  var img = new Image();
                  $(img).load(function () {  
                	if (mode==" ") {
                		var divWidth = 450;
                	} else {
                		var divWidth = 300;
                	}
                	
                	var divHeight = 290;
                	imageWidth = divWidth - (img.width / 2) ;
                  	imageHeight = divHeight - (img.height / 2) - 40;
                  	modalWindow.css({
                  		"margin-left": -divWidth,
                  		"margin-top": -divHeight
                  	});	
                  	$("#modal-load").remove();
                  	modalWindow.append("<h2>" + mode + "</h2>");
                  	modalWindow.append(img);
                  	$(this).addClass("modal-image");
                  	$(this).css({"margin-left": imageWidth, "margin-top": imageHeight});
                  	$("body").append(modalWindow.click(function() {modalHide();}));
                  	modalWindow.append("<p class='name'>" + name + "</p>");
                  	modalWindow.append("<p class='title'>" + title + "</p>");
                  	Cufon('div#modal-window h2');
                  	modalWindow.fadeIn(150);
                  })
                  .attr({ src: this.href }).click(function() {
                  	modalHide();
                  });
                  }
              });
          });

          //Our function for hiding the modalbox
          function modalHide() {

              $(document).unbind("keydown", handleEscape)
              var remove = function() { 

                  $(this).remove(); 
                  $(this).empty();
              }
              overlay.fadeOut(remove);
              modalWindow.fadeOut(remove);
          }

          //Our function that listens for escape key.
          function handleEscape(e) {

              if (e.keyCode == 27) {

                  modalHide();
              }
          }
      }
  });
})(jQuery);

$(function() {
	$('a[rel*=modalPanel]').modalPanel("Chronology");
	$('a[rel*=archivePanel]').modalPanel("Pringle Archive");
	$('a[rel*=frPanel]').modalPanel("Front Row");
	$("a[rel='genPanel']").modalPanel("");
	$("a[rel='genPanelW']").modalPanel(" ");
	$("a[rel='retPanel']").modalPanel("Retail Events and Campaigns");
	$("a[rel='idvideo']").modalPanel("idvideo");
	$("a[rel='popupvideo']").modalPanel("popupvideo");
});


function initContentQuickbuy(productID,productColour) {
//    $('a#buy-now').click(function() {
	if ($('div#collection-popup').length)
	{
		//Popup already created.
	}
	else
	{
		$('body').append('<div id="collection-popup"><div class="container"></div></div>')
	}
	
	$('div#collection-popup div.container').load('/pws/AJProductDetails.ice?layout=quickbuy.pop.layout&ProductID=' + productID + '&colour=' + productColour + '&tabInfo=true', function() {
		var left = $(document).width();

		left = left - 552;
		left = left / 2;
		
		$('div#collection-popup').css({
			'position':'absolute',
			'left':left + 'px'
		});
		Cufon('div#collection-popup h3, div#collection-popup p.product_price');
		$('div#collection-popup').fadeIn();
		$('div#collection-popup span#quickbuy_close').click(function() {
		    $('div#collection-popup').hide();
		}); 	
	});
    return false;	
//    });
}

function initDeliveryReturns() {
	$('div#product_info a.delivery-returns-link').click(function() {
		var leftMargin = $(document).width();
		leftMargin = leftMargin - 525;
		leftMargin = leftMargin / 2;
		
		if ($('div#sizeguide-popup').length)
		{
			//Popup already created.
		}
		else
		{
			$('body').append('<div id="sizeguide-popup"><div class="container"></div></div>')
		}																												
		$('div#sizeguide-popup').load("/about-shipping/collection/fcp-content div#about-page div.container", function() {
			$('div#sizeguide-popup').show();
			$('<div class="close_btn_top close"><img src="/pws/images/popup_close.gif" alt="Close" /></div>').insertBefore('div#sizeguide-popup h1');
			$('select#select_size').hide();
			$('div#sizeguide-popup').css({'left': leftMargin + 'px'});
			
			$('div#sizeguide-popup div.close_btn_top img').click(function() {
				$('div#sizeguide-popup').hide();
				$('select#select_size').show();
			});			
		});	
		return false;
	});
}

$(document).ready(function(){
	initContentVideos();
	initDeliveryReturns();
	Cufon('div#grid div.container h2, span.look_info, div#about-page div.container h2, div#privacy_policy h1, div.archive_banner h2');
	$('body.paymentDetails a#terms_agreement').attr('href','/about-terms-and-conditions/collection/fcp-content');
});
