﻿jQuery(document).ready(function() {

	jQuery(document).bind('cbox_open', function(){
	   addLightboxButtons();
	});
	
	jQuery(document).bind('cbox_load', function(){
	   checkForExtras();
	});
});
    
    
    function initLightbox(selector) {
    	jQuery(selector).each(function(index) {
	    	$link = jQuery(this);
	
	        lightboxHref = "/db_DBImageDisplay.image?id" + getParameterByName('id' ,$link.attr('href')) + "&width=800&height=800&fit=false&percent=0&vm=1&quality=100&mntwdh=true";
    	    	
	    	$link.colorbox({ href: lightboxHref, slideshow: true, slideshowSpeed: 3500, rel: true, slideshowAuto: false, initialWidth: 600 });
    	});
    }
    
    function initGalleryLightbox(selector) {
    	jQuery(selector).each(function(index) {
	    	$link = jQuery(this);
	        
            var imgID = getParameterByName('id' ,$link.attr('href'));
            imgID = imgID.substr(1);
            lightboxHref = "#lbImg" + imgID;
            //alert(lightboxHref)
            
            // lightboxHref = "/db_DBImageDisplay.image?id" + getParameterByName('id' ,$link.attr('href')) + "&width=800&height=800&fit=false&percent=0&vm=1&quality=100&mntwdh=true";
		
	    	$link.colorbox({ href: lightboxHref, inline:true, slideshow: true, slideshowSpeed: 3500, rel: true, slideshowAuto: false, initialWidth: 600 });
    	});
    }
    

    function getWallpaper() { 
    	var $element = jQuery.fn.colorbox.element();
    	var imgID = getParameterByName('id' ,$element.attr('href'));
    	var size = jQuery('#wallpaperSize').val();
    	
    	
    	wallpaperHref = "/pages/multimedia/Wallpaper.aspx?id" + imgID + "&sz=" + size;
    	
    	window.location = wallpaperHref;
    }
    
    function getHighRes() {  
    	var $element = jQuery.fn.colorbox.element();
    	var imgID = getParameterByName('id' ,$element.attr('href'));
    	highResHref = "/db_DBImageDisplay.image?id" + imgID + "&width=800&height=800&fit=false&percent=100&vm=3&quality=100";
    	
    	window.open(highResHref);
    }
    
    function addLightboxButtons() {
    // HTML for the wallpaper and high res buttons
    		var wallpaperHTML = "<div id='cBoxWallpaper'>" +
    				"<select name='wallpaperSize' id='wallpaperSize'>" +
    				"<optgroup label='Normal'>" +
    					"<option value='1'>800 x 600</option>" +
    					"<option value='2'>1024 x 768</option>" +
    					"<option value='3'>1280 x 1024</option>" +
    					"<option value='4'>1600 x 1200</option>" +
    					"<option value='5'>1920 x 1080</option>" +
    				"</optgroup>" +
    
    				"<optgroup label='Widescreen'>" +
    					"<option value='6'>1280 x 800</option>" +
    					"<option value='7'>1440 x 900</option>" +
    					"<option value='8'>1680 x 1050</option>" +
    					"<option value='9'>1920 x 1200</option>" +
    				"</optgroup>" +
    			"</select>" +
    
    			"<a href='#getWallpaper' title='Get your wallpaper'>Get your wallpaper</a>" +
    		"</div>";
    	   
    	   var highresHTML = "<div id='cBoxHighRes'>Download High Resolution</div>";
    	   
    	   var highresloginHTML = "<a href='/pages/account/login.aspx' id='cBoxHighResLogin'>Login to download in high resolution</a>";
    	   
    	   var wallpaperloginHTML = "<a href='/pages/account/login.aspx' id='cBoxWallpaperLogin'>Login to generate as a desktop wallpaper</a>";
    	   
    	   // if each has not already been added then append them to the controls div
    	   if ($('#cBoxWallpaper').length == 0) { 
    	   	$('#colorbox #cboxWrapper #cboxContent').append(wallpaperHTML);
    	   	$('#colorbox #cboxWrapper #cboxContent #cBoxWallpaper').hide();
    	   	$('#cBoxWallpaper a').bind("click", function() { getWallpaper() });
    	   }
    	   
    	   if ($('#cBoxHighRes').length == 0) { 
    	   	$('#colorbox #cboxWrapper #cboxContent').append(highresHTML);
    	   	$('#colorbox #cboxWrapper #cboxContent #cBoxHighRes').hide();
    	   	$('#cBoxHighRes').bind("click", function() { getHighRes() });
	   }

	   if ($('#cBoxWallpaperLogin').length == 0) { 
		$('#colorbox #cboxWrapper #cboxContent').append(wallpaperloginHTML);
		$('#colorbox #cboxWrapper #cboxContent #cBoxWallpaperLogin').hide();
	   }
	   
	   if ($('#cBoxHighResLogin').length == 0) { 
		$('#colorbox #cboxWrapper #cboxContent').append(highresloginHTML);
		$('#colorbox #cboxWrapper #cboxContent #cBoxHighResLogin').hide();
	   }
    }
    
    function checkForExtras() {
    	var $element = jQuery.fn.colorbox.element();
		   
	   if ($element.hasClass("highres"))
	   { 
		$('#colorbox #cboxWrapper #cboxContent #cBoxHighRes').show(); 
	   }
	   else
	   {
		$('#colorbox #cboxWrapper #cboxContent #cBoxHighRes').hide();
	   }

	   if ($element.hasClass("wallpaper"))
	   { 
		$('#colorbox #cboxWrapper #cboxContent #cBoxWallpaper').show(); 
	   }
	   else
	   {
		$('#colorbox #cboxWrapper #cboxContent #cBoxWallpaper').hide();
	   }
	   
	   if ($element.hasClass("wallpaperNoAccess"))
	   { 
	   	$('#colorbox #cboxWrapper #cboxContent #cBoxWallpaperLogin').show(); 
	   }
	   else
	   {
	   	$('#colorbox #cboxWrapper #cboxContent #cBoxWallpaperLogin').hide();
	   }
	   
	   if ($element.hasClass("highresNoAccess"))
	   { 
		$('#colorbox #cboxWrapper #cboxContent #cBoxHighResLogin').show(); 
	   }
	   else
	   {
		$('#colorbox #cboxWrapper #cboxContent #cBoxHighResLogin').hide();
	   }
    }
