var description = null;
var printOptions = null;
var framingOptions = null;
var image = null;
var max_size = null;
var printSizesPrices = new Array();
var framingSizesPrices = new Array();
var print_names = new Array();
var	fixedPrice = null;
var	priceDiscount = null;
var	priceType = null;
var imageID = null;
var	Resin = null;
var	Fibre = null;
var	Lambda = null;
var	Action = null;
var code = null;
var categoryid = "";


// Script responsible for showing the image_details_canvas
function showCanvas(h,d,m,i,p_r,f_r,p,fp, pd,pt,LambdaV,FibreV,ResinV,type,EdgeProgram,_code, id) {
    //$('large_image').src = "";
    //$('large_image').src = "Images/balnk_image.gif";
	doReset();
	
	// Assign parameters to the global vars
	headline = h;
	description = d;
	max_size = m;
	image = i;
	farming_array = f_r;
	print_array = p_r;
	print_framing_names = p;
	fixedPrice = fp;
	priceDiscount = pd;
	priceType = pt;
	imageID = id;
	Resin = ResinV;
	Fibre = FibreV;
	Lambda = LambdaV;
	Action = type;
	Edge = EdgeProgram;
	code = _code;

	print_names = print_framing_names.split(':');
	
	printSizesPrices = print_array.split(':');
	
	framingSizesPrices = farming_array.split(':');
	
	// check if we need to add or remove the image
	if (Action == "Remove") {
	    $('add').style.display="none";
	    $('remove').style.display="block";
	}
	else if (Action == "Other"){
	    $('add').style.display="none";
	    $('remove').style.display="none";
	     
	} else {
	    $('remove').style.display="none";
	    $('add').style.display="block";
	}
	
	//
	//check if to show the edge request
	if (Edge == 'True'){
	   // alert(Edge);
	    $('edge').style.display="block";
        $('EdgeImagePreview').style.visibility="visible";
	} else {
	    //alert(Edge);
	    $('edge').style.display="none";
	   $('EdgeImagePreview').style.visibility="hidden";
	}
	
	// Before displaing the canvas we need to position it to the middle of the page
	var image_canvas_height = 522;
	var image_canvas_width = 749;
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - image_canvas_height) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - image_canvas_width) / 2);
	$('image_details_canvas').style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	$('image_details_canvas').style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";	
	
	$('overlay').style.height = (arrayPageSize[1] + 'px');
	$('overlay').style.display = "block";	
	$('large_image').src = image;
	$('image_details_canvas').style.display="block";
	showDescription();
}

function addEdgeItem(){ 
     //alert("addEdge");
	 window.location = "../Edge/Requests.aspx?ID=" + imageID + "&Action=Add";
	
}

function addItem(){
    // alert("add");
	 window.location = "SearchResults.aspx?ID=" + imageID + "&Action=Add&Source=LightBox";
}

function removeItem(){
 //alert("remove");
	 window.location = "SearchResults.aspx?ID=" + imageID + "&Action=Remove&Source=LightBox";
}

function addItemOne(){
    // alert("add");
	 window.location = "Default.aspx?ID=" + imageID + "&Action=Add&Source=LightBox";
}

function removeItemOne(){
 //alert("remove");
	 window.location = "Default.aspx?ID=" + imageID + "&Action=Remove&Source=LightBox";
}

function addItemTwo(){

	window.location = "SearchResults.aspx?ID=" + imageID + "&Action=Add&Source=LightBox&catid=" + $( $('add_remove_id').value).value;
}

function removeItemTwo(){

	 window.location = "SearchResults.aspx?ID=" + imageID + "&Action=Remove&Source=LightBox&catid=" + $($('add_remove_id').value ).value;
}


function removeItemSelection(){
	 window.location = "your-selection.aspx?ID=" + imageID + "&Action=Remove";
}

function hideCanvas() {
    //doReset();
    //$('large_image').src = "";
    $('large_image').src = "Images/balnk_image.gif";
	$('overlay').style.display = "none";
	$('image_details_canvas').style.display="none";
	hideBottom_div();
	
}

function hideBottom_div(){
	$('picture_sizes').style.display="none";
	$('picture_sizes_discount').style.display="none";
	$('picture_frames').style.display="none";
	$('picture_frames_discount').style.display="none";
	closeFrameMenu();
	close1Menu();
}

function hidePrintBottom_div(){
	$('picture_sizes').style.display="none";
	$('picture_sizes_discount').style.display="none";
}

function hideFrameBottom_div(){
	$('picture_frames').style.display="none";
	$('picture_frames_discount').style.display="none";
}

// Functions which shows the description of the image
function showDescription() {

    var print = "This Picture is available on: ";
    
    if ((Resin == "True") && (Fibre == "True") && (Lambda == "True")){
        print += "Resin, Fibre and Lambda";
        $('printing_type_options').style.display="block";
	    $('printing_type_options').innerHTML = print;
    }
    else if ((Resin == "True") && (Fibre == "True")){
        print += "Resin and Fibre";
        $('printing_type_options').style.display="block";
	    $('printing_type_options').innerHTML = print;
    }
    else if ((Resin == "True") && (Lambda == "True")){
        print += "Resin and Lambda";
        $('printing_type_options').style.display="block";
	    $('printing_type_options').innerHTML = print;
    }
    else if ((Fibre == "True") && (Lambda == "True")){
        print += "Fibre and Lambda"
        $('printing_type_options').style.display="block";
	    $('printing_type_options').innerHTML = print;
    }
    
     else if (Resin == "True"){
        print += "Resin"
        $('printing_type_options').style.display="block";
	    $('printing_type_options').innerHTML = print;
    }
    
     else if (Fibre == "True"){
        print += "Fibre"
        $('printing_type_options').style.display="block";
	    $('printing_type_options').innerHTML = print;
    }
    
     else if (Lambda == "True"){
        print += "Lambda"
        $('printing_type_options').style.display="block";
	    $('printing_type_options').innerHTML = print;
    }
    else{
        print= "";
        $('printing_type_options').style.display="none";
    }
    
	$('picture_title').style.display="block";
	$('picture_title').innerHTML = headline;
	$('picture_options').style.display="block";
	$('picture_options').innerHTML = description + "<br/><br/>" + "ref:" + code;
}

function showPrintOptions() {
	$('bottom_div').style.display="block";
	
	if ( priceType == "Standard" || priceType == ""  ){
		$('picture_sizes').style.display="block";
		if (max_size == 0)
			max_size = 9;
		doPrint();
	}
	else{
		$('picture_sizes_discount').style.display="block";
		doPrintDiscount();
	}
	$('picture_frames').style.display="none";
	$('picture_frames_discount').style.display="none";
}

function showFramingOptions() {
	$('bottom_div').style.display="block";
	
	if ( priceType == "Standard" || priceType == ""  ){
		$('picture_frames').style.display="block";
		if (max_size == 0)
			max_size = 9;
		doFraming();
	}
	else {
		$('picture_frames_discount').style.display="block";
		doFramingDiscount();
	}
	$('picture_sizes').style.display="none";
	$('picture_sizes_discount').style.display="none";
}	


function $(id) {
	return document.getElementById(id);
}

// do the print discount
function doPrintDiscount(){
	
	$('picture_sizes_discount').style.display="block";
	$('picture_frames_discount').style.display="none";
	
	if ( priceType == "Fixed" ){
		$('label_print').innerHTML = "Fixed Size Item";
		$('discount_size_print').innerHTML = print_names[max_size-1];
		$('price_label_print').innerHTML = "Price"
		$('price_no_discount_print').innerHTML = "&pound; " + fixedPrice;
		if ( priceDiscount != 0)
		{
			$('discount_label_print').innerHTML = "Discounted Price";
			$('price_after_discount_print').innerHTML = "was <s>&pound;" + fixedPrice + "</s> now &pound;" + 
														(fixedPrice -(fixedPrice*priceDiscount));
		}
	}
	else if ( priceType == "POA" ) {
		$('label_print').innerHTML = "Price On Application";
		if (max_size != "")
			$('discount_size_print').innerHTML = "max size "  + print_names[max_size-1];
		$('price_label_print').innerHTML = ""
		$('price_no_discount_print').innerHTML = "Please call or add to your selection, for more information about this picture"
		$('discount_label_print').innerHTML = ""
		$('price_after_discount_print').innerHTML = ""
	}
}

// do the frame discount
function doFramingDiscount(){
		
	$('picture_frames_discount').style.display="block";
	$('picture_sizes_discount').style.display="none";
	
	if ( priceType == "Fixed" ){
		$('label_frame').innerHTML = "Fixed Size Item";
		$('discount_size_frame').innerHTML = print_names[max_size-1];
		$('price_label_frame').innerHTML = "Price"
		$('price_no_discount_frame').innerHTML = "&pound; " + fixedPrice;
		if ( priceDiscount != 0)
		{
			$('discount_label_frame').innerHTML = "Discounted Price";
			$('price_after_discount_frame').innerHTML = "was <s>&pound;" + fixedPrice + "</s> now &pound;" + 
														(fixedPrice -(fixedPrice*priceDiscount));
		}
	}
	else if ( priceType == "POA" ) {
		$('label_frame').innerHTML = "Price On Application";
		if (max_size != "")
			$('discount_size_frame').innerHTML = "max size "  + print_names[max_size-1];
		$('price_label_frame').innerHTML = ""
		$('price_no_discount_frame').innerHTML = "Please call or add to your selection, for more information about this picture"
		$('discount_label_frame').innerHTML = ""
		$('price_after_discount_frame').innerHTML = ""
	}
}

// populate the print remember to do a delete
function doPrint(){
	for (i=0; i < 8; i++)
	{
		if (i < max_size){
			$('s'+ i).innerHTML = print_names[i];
			if ( printSizesPrices[i] == 0)
				$('r'+ i).innerHTML = "n/a";
			else
				$('r'+ i).innerHTML = "&pound " + printSizesPrices[i];
			if ( printSizesPrices[i+8] == 0)
				$('f'+ i).innerHTML = "n/a";
			else
				$('f'+ i).innerHTML = "&pound " + printSizesPrices[i+8];
			if ( printSizesPrices[i+16] == 0)
				$('c'+ i).innerHTML = "n/a";
			else
				$('c'+ i).innerHTML = "&pound " + printSizesPrices[i+16];
		}
	}
}

// clear the table
function doReset(){
	for (i=0; i < 8; i++)
	{
		$('s'+ i).innerHTML = "";
		$('r'+ i).innerHTML = "";
		$('f'+ i).innerHTML = "";
		$('c'+ i).innerHTML = "";
		$('fs'+ i).innerHTML = "";
		$('fsb'+ i).innerHTML = "";
		$('fmb'+ i).innerHTML = "";
		$('ftnb'+ i).innerHTML = "";
		$('fce'+ i).innerHTML = "";	
	}
	
	$('label_print').innerHTML = ""
	$('discount_size_print').innerHTML = ""
	$('price_label_print').innerHTML = ""
	$('price_no_discount_print').innerHTML = ""
	$('discount_label_print').innerHTML = ""
	$('price_after_discount_print').innerHTML = ""
	
	$('label_frame').innerHTML = ""
	$('discount_size_frame').innerHTML = ""
	$('price_label_frame').innerHTML = ""
	$('price_no_discount_frame').innerHTML = ""
	$('discount_label_frame').innerHTML = ""
	$('price_after_discount_frame').innerHTML = ""
}

// populate the framing table
function doFraming(){
	for (i=0; i < 8; i++)
	{
		if (i < max_size){
			$('fs'+ i).innerHTML = print_names[i];
			if ( framingSizesPrices[i] == 0 )
				$('fsb'+ i).innerHTML = "n/a";
			else
				$('fsb'+ i).innerHTML = "&pound " + framingSizesPrices[i];
			if ( framingSizesPrices[i+8] == 0 )
				$('fmb'+ i).innerHTML = "n/a";
			else
				$('fmb'+ i).innerHTML = "&pound " + framingSizesPrices[i+8];
			if ( framingSizesPrices[i+16] == 0)
				$('ftnb'+ i).innerHTML = "n/a";
			else
				$('ftnb'+ i).innerHTML = "&pound " + framingSizesPrices[i+16];
			if ( framingSizesPrices[i+24] == 0)
				$('fce'+ i).innerHTML = "n/a";
			else
				$('fce'+ i).innerHTML = "&pound " + framingSizesPrices[i+24];
		}
	}
}
	
	
// original lightbox functions 
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

