// JavaScript Document

function downloadImage() {
	// search for cookie
	// if one exists, do MC check and allow download if all ok
	
	// otherwise show image form
	//toggleImageForm();
	toggleForm($('#imageQuickForm'), $('#downloadbtn'));
}

function toggleForm(form, title) {
	//
	//var form = $('#imageQuickForm');
	if (form.css("display") == "block") {
		// hide form
		form.css("display", "none");
		if (title) {
			title.attr("class", "");
		}
	} else {
		// show form
		form.css("display", "block");
		if (title) {
			title.attr("class", "white");
		}
	}
}

function showPage() {
	//
	//Cufon.replace('h4');
	updateText();
	$('body').fadeIn(600);
	//fleXenv.fleXcrollMain("thumb-holder");
	//fleXenv.updateScrollBars();
	//
}

function updateText() {
	//
	Cufon('a', {
	hover: {
	color: '#111111'
	}
	});
	Cufon.replace('p');
}

// resize functions
// specialised for jason bell
  function fitArea(target, wid, hei, getWid, getHei) {
    var originalWid = target.width();
	if (getWid > 0) {
		originalWid = getWid;
	}
	var originalHei = target.height() ? target.height() : 800;
	//var originalHei = 800;
	//var originalHei = imageArray[currentImage].hei > 0 ? imageArray[currentImage].hei : 800;
	if (getHei > 0) {
		originalHei = getHei;
	}
	target.width(wid);
	var scaleRatio = wid/originalWid;
	target.height(Math.round(originalHei*scaleRatio));
	if (target.height() > hei) {
	  scaleRatio = hei/originalHei;
	  target.height(hei);
	  target.width(Math.round(originalWid*scaleRatio));
	}
}
  
function fillArea(target, wid, hei, getWid) {
    var originalWid = target.width();
	if (getWid > 0) {
		//originalWid = getWid;
	}
	//var originalHei = target.height();
	var originalHei = thumbHeight;
	target.width(wid);
	var scaleRatio = wid/originalWid;
	target.height(Math.round(originalHei*scaleRatio));
	if (target.height() < hei) {
	  scaleRatio = hei/originalHei;
	  target.height(hei);
	  target.width(Math.round(originalWid*scaleRatio));
	}
}

function position(target, container, align) {
  
	if (align=="center") {
		//alert(target.width());
	   target.css("margin-left", (container.width()/2)-(target.width()/2));
	   //target.css("margin-top", (container.height()/2)-(target.height()/2));
	}
}
  
    
  function togglethumbs() {
  	//
	$("#thumbnails").slideToggle(600);
	fleXenv.updateScrollBars();
  }
  //
  function toggleNav(className) {
  	//
	//$("."+className).slideToggle(600);
	$("."+className).css("display", "block");
  }


function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/in_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}

function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/nl2br
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin\nvan\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '\nOne\nTwo\n\nThree\n'
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
 
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
