var OnLoadOccurred = false;
var Initialized = 0;
var DynamicSize = true;

window.addEvent('domready', DomReady);
window.addEvent('load', OnLoad);
window.addEvent('resize',OnResize);

function OnLoad() {
    DomReady();
    resizeImages();
}

function DomReady() {
    if(Initialized!=0) return;
    Initialized = 1;

    $$(".navigation_item").each(function(item) {
        $(item).addEvent('click', NavigateLink);
    });

    $$(".left_arrow").each(function(item) {
       $(item).addEvent('click', shiftThumbsRight);
    });

    $$(".right_arrow").each(function(item) {
       $(item).addEvent('click', shiftThumbsLeft);
    });

    $$(".thumbnail").each(function(item) {
        $(item).addEvent('click', showThumb);
    });
    
    OnResize();
}

function OnResize() {
    var size = window.getSize();
    if(size.x > 1280 && DynamicSize == true) {
        $("Page").setStyle("width","1280px");
        DynamicSize = false;
    }
    else if(size.x <= 1280 && DynamicSize == false) {
        $("Page").setStyle("width","auto");
        DynamicSize = true;
    }
}

function NavigateLink(el) {
    el = $(el.target);
    var as = el.getChildren("a");
    if(as.length>0) {
        var curURL = window.location.href;
        curURL = curURL.replace(/[^\/]+\.php/,'');
        curURL += $(as[0]).get('href');
        window.location = curURL;
    }
}

function showThumb(event) {
    event.stop();
    var el = $(event.target);
    while(el && el.get('tag') != 'a') {
        el = el.getParent();
    }
    if(!el) return;

    var featurePhoto = $("FeaturePhoto");
    var photoDescription = $("PhotoDescription");

    var patt = /photo=(\d+)/;
    var photoPart = patt.exec(el.href);
    if(!photoPart) photoPart = 0;
    featurePhoto.src = PhotoSetSrc[photoPart[1]];
    photoDescription.innerHTML = PhotoSetDesc[photoPart[1]];
}

function resizeImages() {
    var i, j, item, images, img, size, size2, x, y, width, height;
    var photoSets = $$(".photo_set");
    for(i = 0; i < photoSets.length; ++i) {
        item = photoSets[i];
    
        images = item.getElements("img");
        for(j = 0; j < images.length; ++j) {
            img = images[j];
            size = img.getSize();

            if(size.x > size.y) {
                height = 40;
                width = size.x * 40 / size.y;
                y = 0;
                x = (40 - width) / 2;
            }
            else {
                width = 40;
                height = size.y * 40 / size.x;
                x = 0;
                y = (40 - height) / 2;
            }

            img.setStyles({
               width: width,
               height: height,
               left: x,
               top: y,
               position: 'relative'
            });
        }
    }

    var rightImages = $$(".listing_right_image");
    for(i = 0; i < rightImages.length; ++i) {
        item = rightImages[i];
        images = item.getElements("img");
        for(j = 0; j < images.length; ++j) {
            img = images[j];
            size = img.getSize();
            size2 = item.getSize();
            if(size.x > size2.x) {
                y = (size.y-4) * (size2.x-4) / (size.x-4);
                img.setStyles({
                    width: size2.x-4,
                    height: y
                });
            }
        }
    }
}

function shiftThumbsLeft(el) {
    var photoParent = $(el.target).getNext(".photo_set");
    if(photoParent != null) {
        var photoSetSize = photoParent.getSize();
        photoSetSize = parseInt(photoSetSize.x / 46,10);
        var photoSet = photoParent.getChildren();
        if(photoSet.length>0) {
            photoSet = $(photoSet[0]);
            var photos = photoSet.getChildren();
            var position = photoSet.getPosition(photoParent);
            position.x -= 46;
            position.x = parseInt(position.x / 46,10);
            
            if(photos.length + position.x > photoSetSize) {
                position.x *= 46;
                photoSet.morph({left : position.x + "px"});
            }
        }
    }
}

function shiftThumbsRight(el) {
    var photoParent = $(el.target).getNext(".photo_set");
    if(photoParent != null) {
        var photoSet = photoParent.getChildren();
        if(photoSet.length>0) {
            photoSet = $(photoSet[0]);
            var position = photoSet.getPosition(photoParent);
            position.x += 46;
            position.x = parseInt(position.x / 46,10) * 46;
            if(position.x <= 0) {
                photoSet.morph({left : position.x + "px"});
            }
        }
    }
}

function Recalculate() {
	A = $("PValue");
	B = $("PValue6");
	C = $("PValue5");
	D = $("PValue4");
	E = $("PValue3");
	if(A && B && C && D && E) {
		Str = A.value;
		Str = Str.replace(/\$/,"");
		Str = Str.replace(/ /,"");
		Str = Str.replace(/,/,"");
		if(Str*1==0) {
			B.innerHTML = "";
			C.innerHTML = "";
			D.innerHTML = "";
			E.innerHTML = "";
		}
		else {
			B.innerHTML = Dollar(Str * 0.06) + " commission";
			C.innerHTML = Dollar(Str * 0.05) + " commission";
			D.innerHTML = Dollar(Str * 0.04) + " commission";
			E.innerHTML = Dollar(Str * 0.03) + " commission";
		}
	}
}

function Dollar(Value) {
	Value = Value.toFixed(0);
	Value += '';
	x = Value.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return "$" + x1 + x2;
}
