/**
 * @author NetHunter
 */

function openWin(url) {
	window.open (url, 'email_friend', 'height=825, width=803, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
}

function printTable() {
	var clrs = new Array("#660000", "maroon",  "#990000", "#CC0000", "red",     "#FF6666", "#FF9999", "#FFCCCC", "#3366FF", "#CC33FF", "fuchsia", "#999999",
			     "#663300", "#993300", "#CC6600", "#FF6600", "#FF6666", "#FF9900", "#FF9966", "#FFCC99", "#0033CC", "#CC00FF", "#FF00CC", "gray",
			     "#006600", "green",   "#009900", "#00CC00", "lime",    "#66FF66", "#99FF99", "#CCFFCC", "#003399", "#9900CC", "#CC0099", "#666666",
			     "#003333", "#006666", "teal",    "#009999", "#00CCCC", "#66CCCC", "#66FFCC", "#99FFCC", "navy",    "#660099", "#990066", "#333333",
			     "#000099", "blue",    "#0066ff", "#0099FF", "#00CCFF", "#66FFFF", "#99FFFF", "#FFFFFF", "#000066", "#330066", "#660033", "black");
	var x; x = 0;

	clrs.each(function(clr) {
		var el = new Element('span', {
			'styles': {
				'background-color': clr
				},
			'events': {
				'mouseover': function(e) {
					var ev = new Event(e);
					ev.stop();

					$('Testing').setStyle('borderColor', ev.target.getStyle('background-color'));
				}}
		});
		el.injectInside($('colorSelect'));
	});
}

function initCustomSize() {
    var parseLength = function(strLen) {
        var feetPos = strLen.indexOf("\'");
        var inchPos = strLen.indexOf("\"");
        
        var feet = parseInt(strLen.slice(0, feetPos));
        
        if (inchPos == -1) {
            inchPos = strLen.length;
        }
        
        var inch = parseInt(strLen.slice(feetPos + 1, inchPos));
        console.log({feetPos: feetPos, inchPos: inchPos, feet: feet, inch: inch});
        
        if (isNaN(inch)) {
            return feet;
        }
        
        return feet + (inch / 12);
    }
    
    var getCustomPrice = function() {
        var customPrice = $E('.customPricePerFt').getValue();
        var customHeight = $E('#TB_ajaxContent .customHeight').getValue();
        var customWidth = $E('#TB_ajaxContent .customWidth').getValue();
        
        if (isNaN(customHeight)) {
            customHeight = parseLength(customHeight);
        }

        if (isNaN(customWidth)) {
            customWidth = parseLength(customWidth);
        }
       
        var customTotalPrice = customHeight * customWidth * customPrice;
        
        return customTotalPrice;               
    };
    
    var updateCustomPrice = function() {
        var customPrice = getCustomPrice();
        $E('#TB_ajaxContent .customPrice').setHTML(customPrice + "$");
    }
    
    var addCustomPrice = function(e) {
        var ev = new Event(e);
        ev.stop();
        
        var customPrice = getCustomPrice();
        var customHeight = $E('#TB_ajaxContent .customHeight').getValue();
        var customWidth = $E('#TB_ajaxContent .customWidth').getValue();

        if (!isNaN(customHeight)) {
            customHeight = customHeight + "'";
        }

        if (!isNaN(customWidth)) {
            customWidth = customWidth + "'";
        }
        
        var customLine = $('customSizeOption').clone().injectBefore(
            'customSizeOption'
        );
        $E('.sizeBox', customLine).setHTML(customHeight + ' x ' + customWidth);
        $E('font', $E('.sizeRadioSelect', customLine).getParent()).setHTML(
            '<b>$' + customPrice + '</b> (Custom)'
        );
        $E('.sizeRadioSelect', customLine).setProperty('checked', true);
        $E('.sizeRadioSelect', customLine).setProperty(
            'value', 'custom#' + customHeight + ' x ' + customWidth + '#'
            + customPrice
        );
        
        TB_remove();
    }
    
    $ES('#TB_ajaxContent .customHeight').addEvent('keyup', updateCustomPrice);
    $ES('#TB_ajaxContent .customWidth').addEvent('keyup', updateCustomPrice);    
    $ES('#TB_ajaxContent .customAdd input').addEvent('click', addCustomPrice);
}

function initItemPage() {
	window.addEvent('domready', function(e) {
		printTable();
		$('addToFavorites').addEvent('click', function(e) {
			var ev = new Event(e);
			ev.stop();

			var photoID = $(ev.target).getParent().getProperty('rel');
			var urlPrefix = "/cgi-bin/show_favorites.pl?";
			var sizeSelect = null;

			$ES('.sizeRadioSelect').each(function(el) {
				if (el.getValue()) {
					sizeSelect = el.getProperty('rel');					
					sizeSelectValue = el.getProperty('value');
				}
			});
			var urlParams = new Hash();
			urlParams.set('action', 'add');
			urlParams.set('photo_id', photoID);
			urlParams.set('size', sizeSelect);
			urlParams.set('option_value', sizeSelectValue);

			var queryString = Object.toQueryString(urlParams.obj);
			var resultUrl = urlPrefix + queryString;
			window.location = resultUrl;
		});
		
		/*$('customAddPrice').addEvent('click', initCustomSize);
		$E('#customSizeOption input').addEvent('click', function(e) {
			var ev = new Event(e);
			ev.stop();
			
			var el = $E('#customSizeOption a');
			elProps = el.getProperties('title', 'href', 'rel');

			console.log(elProps);
			console.log(elProps);
			TB_show(elProps.title, elProps.href, elProps.rel); 
		});*/
	});
}

