﻿var newsletterClass= new Class({
    Binds : ["_validate", "_errorFields", "_errorSent","_successSent"],
    initialize : function() {
        var sendBtn = $("sendNewsletter");
        sendBtn.addEvent("click", this._validate);
        this.mailreq = new Request.HTML({url : "/ajax/sendMail.aspx"});
        this.mailreq.addEvents({"success" : this._successSent, "error" : this._errorSent});
    },
    _validate : function(ev) {
        ev.stop();
        var first = $("firstname_field").get("value");
        var last = $("lastname_field").get("value");
        var email = $("email_field").get("value");
        if($chk(first) && $chk(last) && $chk(email)) {
            var company = $("company_field").get("value");
            var comments = $("comments_field").get("value");
            this.mailreq.post({
                first : first,
                last : last,
                email : email,
                company : company,
                comments : comments
            });
        } else 
            this._errorFields();
    },
    _errorFields : function() {
        if(!$chk($("firstname_field").get("value")))
            $("firstname_field").addClass("error");
        if(!$chk($("lastname_field").get("value")))
            $("lastname_field").addClass("error");
        if(!$chk($("email_field").get("value")))
            $("email_field").addClass("error");
    },
    _errorSent : function() {
        if(!$chk($("firstname_field").get("value")))
            $("firstname_field").addClass("error");
        if(!$chk($("lastname_field").get("value")))
            $("lastname_field").addClass("error");
        if(!$chk($("email_field").get("value")))
            $("email_field").addClass("error");
    },
    _successSent : function() {
       new Element("div", {"class" : "margintop", "html" :"your message has been sent successfully!"} ).replaces($("sendNewsletter"));
    }
});
var buyNowClass= new Class({
    Binds : ["_validate", "_errorFields", "_errorSent","_successSent"],
    initialize : function() {
        var sendBtn = $("sendBuyNow");
        sendBtn.addEvent("click", this._validate);
        this.mailreq = new Request.HTML({url : "/ajax/sendMailBuy.aspx"});
        this.mailreq.addEvents({"success" : this._successSent, "error" : this._errorSent});
    },
    _validate : function(ev) {
        ev.stop();
        var first = $("firstname_field").get("value");
        var last = $("lastname_field").get("value");
        var email = $("email_field").get("value");
        var street = $("street_field").get("value");
        var plz = $("plz_field").get("value");
        var city = $("city_field").get("value");
        var country = $("country_field").get("value");
        if($chk(first) && $chk(last) && $chk(email) && $chk(plz) && $chk(street) && $chk(city) && $chk(country)) {
            var company = $("company_field").get("value");
            var comments = $("comments_field").get("value");    
            var product = $("product_field").get("value");
            var telephone = $("telephone_field").get("value");
            this.mailreq.post({
                first : first,
                last : last,
                email : email,
                company : company,
                street : street,
                plz : plz,
                city : city,
                country : country,
                product : product,
                telephone : telephone,
                comments : comments
            });
        } else 
            this._errorFields();
    },
    _errorFields : function() {
        if(!$chk($("firstname_field").get("value")))
            $("firstname_field").addClass("error");
        if(!$chk($("lastname_field").get("value")))
            $("lastname_field").addClass("error");
        if(!$chk($("email_field").get("value")))
            $("email_field").addClass("error");
        if(!$chk($("street_field").get("value")))
            $("street_field").addClass("error");
        if(!$chk($("plz_field").get("value")))
            $("plz_field").addClass("error");
        if(!$chk($("city_field").get("value")))
            $("city_field").addClass("error");
        if(!$chk($("country_field").get("value")))
            $("country_field").addClass("error");
    },
    _errorSent : function() {
        if(!$chk($("firstname_field").get("value")))
            $("firstname_field").addClass("error");
        if(!$chk($("lastname_field").get("value")))
            $("lastname_field").addClass("error");
        if(!$chk($("email_field").get("value")))
            $("email_field").addClass("error");
        if(!$chk($("street_field").get("value")))
            $("street_field").addClass("error");
        if(!$chk($("plz_field").get("value")))
            $("plz_field").addClass("error");
        if(!$chk($("city_field").get("value")))
            $("city_field").addClass("error");
        if(!$chk($("country_field").get("value")))
            $("country_field").addClass("error");
    },
    _successSent : function() {
       new Element("div", {"class" : "margintop", "html" :"your message has been sent successfully!"} ).replaces($("sendBuyNow"));
    }
});

var linkLegend = new Class ({
    Binds : ["_legendHover", "_legendOut"],
    initialize : function() {
        $$(".r").each(function(elt){
            var rel = elt.get("rel");
            if($chk(rel))
                elt.addEvents({
                    "mouseenter" : this._legendHover.pass(rel),
                    "mouseleave" : this._legendOut
                });
        }, this);
    },
    _legendHover : function(text) {
        $("sublegend").set("html", text);
    },
    _legendOut : function() {
        $("sublegend").set("html", $("sublegend").get("rel"));
    }
});
var zoomClass = new Class ({
    Binds : ["_imageClicked", "_legendOut", "_imageOver", "_imageLoaded"],
    initialize : function() {
        $("center").addEvents({
                    "click" : this._imageClicked,
                    "mousemove" : this._imageOver
                });
        this.centerPos = $("center").getPosition();
        this.formerHTML = $("center").get("html");
    },
    _imageClicked : function(ev) {
        ev.stop();
        if($("center").hasClass("zoom")) {
            $("center").set("html", this.formerHTML);
            $("center").removeClass("zoom");
        } else {
            $("center").addClass("zoom");
            $("center").getElement("a").set('tween', {duration: 'short'});
            $("center").getElement("a").tween("opacity",0);
            new Asset.image($("center").get("rel"), {id: "detailImg", onload: this._imageLoaded.bindWithEvent(this,ev), styles : {opacity : 0, left : 0, top:0}});
        }
    },
    _imageLoaded: function(img,ev) {
        $("center").empty();
        img.inject($("center"));
        this.imageSize = img.getSize();
        var left = (this.imageSize.x-375) / 375 * (ev.page.x - this.centerPos.x);
        var top = (this.imageSize.y-470) / 470 * (ev.page.y - this.centerPos.y);
        img.setStyles({"left" : -left, "top" : -top});
        img.fade("in");
    },
    _imageOver : function(ev) {
        if($("center").hasClass("zoom") && this.imageSize) {
            var left = (this.imageSize.x-375) / 375 * (ev.page.x - this.centerPos.x);
            var top = (this.imageSize.y-470) / 470 * (ev.page.y - this.centerPos.y);
            $("detailImg").setStyles({"left" : -left, "top" : -top});
        }
    }
});
var startPage = new Class({
    initialize : function() {
        $("topmenu").setStyles({"opacity" : 0, "visibility": "visible"});
        $("startContent").setStyles({"opacity" : 0, "visibility": "visible"});
        var fx = new Fx.Tween($("topmenu"), {property : "opacity" , duration : "long"});
        var fx2 = new Fx.Tween($("startContent"), {property : "opacity" , duration : "long"});
        (function(){fx2.start(1).wait(1750).chain(function(){fx.start(1)})}).delay(2000);
    }
});
