var Hint = {

    clock: 0,
    
    interval: 500,
    
    startClock: function(){
        this.stopClock();
        this.clock = setInterval("Hint.hideAll()", this.interval);
    },
    
    stopClock: function(){
        if (this.clock) 
            clearInterval(this.clock);
    },
    
    show: function(el){
        Point.stopClock();
        this.hideAll();
        $(el).find('.hint').show().parent().css('z-index', 1000);
    },
    
    showForColours: function(el){
        this.hideAll();
        $(el).find('.hint').show();
		var hintX=0;;
		if ($(el).position().left > $("#colours").width() - $(el).find('.hint').width()) {
			$(el).find('.hint').addClass('hint-rt');
			hintX=-($(el).find('.hint').width())-$(el).width()-15;
		}
		else {
			$(el).find('.hint').removeClass('hint-rt');
			hintX=0;
		}	
        $(el).find('.hint').css('left', hintX);

    },
    
    hide: function(el){
        Point.stopClock();
        this.startClock();
    },
    
    hideAll: function(){
        this.stopClock();
        $('.hint').hide().parent().css('z-index', 1);
    },
    
    showAll: function(){
        this.stopClock();
        $('.hint').show().parent().css('z-index', 1000);
    },
    
    init: function(){
        $('.point, .colour, .colors-box .p li').mouseout(function(){
            Hint.hide(this);
        });
		$('.colors-box .p li').mouseover(function(){
			Hint.show(this);
        });
		this.initForColours();
    },
    
    initForColours: function(){
		$('#colours .colour').mouseover(function(){
            Hint.showForColours(this);
        });
        $('#colours .colour .hint').mouseout(function(){
            Hint.hide(this);
        });
        $('#colours .colour').mouseout(function(){
            Hint.hide(this);
        });
    }
    
};

var Point = {

    imageWidth: 0,
    
    imageHeight: 0,
    
    preview: true,
    
    clock: 0,
    
    interval: 1000,
    
    startClock: function(){
        this.stopClock();
        this.clock = setInterval("Point.hideAll()", this.interval);
    },
    
    stopClock: function(){
        if (this.clock) 
            clearInterval(this.clock);
    },
    
    hide: function(){
        this.startClock();
    },
    
    show: function(){
        this.stopClock();
        this.showAll();
    },
    
    hideAll: function(){
        this.stopClock();
        Hint.hideAll();
        $('.point').fadeOut();
        $('#photo-colours').fadeOut();
    },
    
    showAll: function(){
        this.stopClock();
        $('.point').fadeIn();
        $('#photo-colours').fadeIn();
    },
    
    init: function(){
        this.stopClock();
        $('#photo .point').mouseover(function(){
            Hint.show(this);
        });
        $('#photo #photo-colours .colour').mouseover(function(){
            Hint.show(this);
        });
        $('#photo .hint').mouseout(function(){
            Hint.hide(this);
        });
        $('#photo img').mouseover(function(){
            Point.show();
        });
        $('#photo img').mouseout(function(){
            Point.hide();
        });
        Point.hide();
        Point.imageWidth = Photo.img.width;
        Point.imageHeight = Photo.img.height;
        
        $('#photo #points .point').each(function(){
            var pointX = parseInt($(this).css('left'));
            var pointY = parseInt($(this).css('top'));
            var pointW = parseInt($(this).width());
            var pointH = parseInt($(this).height());
            var hintW = $(this).find('.hint').outerWidth();
            var hintH = $(this).find('.hint').outerHeight();
            var cl = 'hint-';
            var hintX = 0;
            var hintY = 0;
            
            if ((pointX + pointW + hintW) > Point.imageWidth) {
                hintX = -hintW;
                cl += 'r';
            }
            else {
                hintX = pointW;
                cl += 'l';
            }
            if ((pointY + pointH + hintH) > Point.imageHeight) {
                hintY = -hintH;
                cl += 'b';
            }
            else {
                hintY = pointH;
                cl += 't';
            }
            $(this).find('.hint').css('left', hintX);
            $(this).find('.hint').css('top', hintY);
            $(this).find('.hint').addClass(cl);
        });
    }
    
};

var Photo = {

    html: '',
    
    img: new Image(),
    
    imgStatus: false,
    
    htmlStatus: false,
    
    load: function(el, id, img){
    
        if (!$(el).hasClass('active')) {
            $('#gallery a').removeClass('active');
            $(el).addClass('active');
            this.img.src = base + img;
            this.htmlStatus = false;
            this.imgStatus = false;
            $('#photo').spinner();
            url = base + 'photos/view/' + id;
            $.get(url, function(data){
                if (data.substr(0, 3) != 'err') {
                    Photo.html = data;
                    Photo.htmlLoaded();
                }
            });
        }
    },
    
    imgLoaded: function(){
        this.imgStatus = true;
        if (this.htmlStatus) 
            this.allLoaded();
    },
    
    htmlLoaded: function(){
        this.htmlStatus = true;
        if (this.imgStatus) 
            this.allLoaded();
    },
    
    allLoaded: function(){
        $('#photo').replaceWith(this.html).show();
        Point.init();
    }
    
};

$(Photo.img).load(function(){
    Photo.imgLoaded();
});

var Gallery = {

    galleryId: false,
    
    prevId: false,
    
    nextId: false,
    
    count: 0,
    
    current: 1,
    
    previous: 0,
    
    imgs: false,
    
    animates: false,
    
    width: 0,
    
    shown: 1,
	
	initalized: false,
    
    create: function(galleryId, prevId, nextId, width, shown){
        this.galleryId = galleryId;
        this.prevId = prevId;
        this.nextId = nextId;
        this.width = width;
        this.shown = shown;
        
        this.imgs = $("#" + this.galleryId + " li");
        
        this.count = this.imgs.length;

		if (!this.count)
		{
			$('#nogallery').show();
			$('#gallery-box').hide();
		} 
		else
		{
			$('#nogallery').hide();
			$('#gallery-box').show();
		} 
		
		$("#" + this.nextId).removeClass('hover');
		$("#" + this.nextId).removeClass('disabled');
		$("#" + this.prevId).removeClass('hover');
		$("#" + this.prevId).removeClass('disabled');		
		        
        $("#" + this.galleryId).css('overflow', 'hidden');
        $("#" + this.galleryId + " ul").css('width', this.count * this.width);
        
        if (this.current == (this.count - this.shown)) 
            $("#" + this.nextId).addClass('disabled');
        if (this.current == 1) 
            $("#" + this.prevId).addClass('disabled');
        
        if (this.count < this.shown) {
            $("#" + this.nextId).addClass('disabled');
            $("#" + this.prevId).addClass('disabled');
        }
        
        w = $("#" + this.galleryId).width();
        $("#" + this.galleryId).width(this.width * this.shown);
        
        this.show();
    },
    
    next: function(){
        if (!this.animates) {
            if ((this.current) < (this.count - this.shown + 1)) {
                this.animates = true;
                this.previous = this.current;
                this.current++;
                this.show();
                $("#" + this.nextId).removeClass('disabled');
            }
        }
        
        if (this.current == (this.count - this.shown + 1)) 
            $("#" + this.nextId).addClass('disabled');
        if (this.current != 1) 
            $("#" + this.prevId).removeClass('disabled');
    },
    
    prev: function(){
        if (!this.animates) {
            if ((this.current) > 1) {
                this.animates = true;
                this.previous = this.current;
                this.current--;
                this.show();
                $("#" + this.prevId).removeClass('disabled');
            }
        }
        
        if (this.current == 1) 
            $("#" + this.prevId).addClass('disabled');
        if (this.current != (this.count - this.shown + 1)) 
            $("#" + this.nextId).removeClass('disabled');
    },
    
    show: function(){
        $("#" + this.galleryId + " ul").animate({
            'marginLeft': (-((this.current - 1) * this.width))
        }, "def", "swing", function(){
            Gallery.animates = false;
        });
    },
    
    init: function(){
		this.current=1;

        this.create('gallery', 'gallery-prev', 'gallery-next', 68, 8);
		
		if (!this.initalized) {
			$("#" + this.prevId).click(function(){
				Gallery.prev();
				return false;
			});
			
			$("#" + this.nextId).click(function(){
				Gallery.next();
				return false;
			});
			
			$("#" + this.prevId).hover(function(){
				if (!$(this).hasClass('disabled')) 
					$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});
			
			$("#" + this.nextId).hover(function(){
				if (!$(this).hasClass('disabled')) 
					$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});
		}
    },
    
    load: function(colourId){
        $('#gallery').spinner();
        url = base + 'photos/index/' + colourId;
        $.get(url, function(data){
            if (data.substr(0, 3) != 'err') {
                $('#gallery').replaceWith(data);
                Gallery.init();
            }
        });
    }
    
};

var Colour = {

    el: '',
    
    id: 0,
    
    count: 0,
    
    categoryId: 0,
	
	initalized: false,
    
    add: function(id, el){
        var hint = $(el).parent().parent();
        $('.hint .action-' + id).hide();
        $(hint).find('.msg').show();
        $(hint).find('.wait').html('');
        url = base + 'colours/add/' + id;
        $.get(url, function(data){
            if (data.substr(0, 2) == 'ok') {
                $('#colours').spinner();
                url = base + 'colours/get/' + Colour.id;
                Colour.count++;
                $.get(url, function(data){
                    if (data.substr(0, 3) != 'err') {
                        $('#colours').replaceWith(data);
                        Hint.initForColours();
                        Colour.init();
                    }
                });
            }
        });
    },
    
    del: function(id, el){
        this.el = $(el).parent();
		$('#photo .hint .action-' + id).show().parent().find('.msg').hide();
        url = base + 'colours/del/' + id;
        $.get(url, function(data){
            if (data.substr(0, 2) == 'ok') {
                Colour.count--;
                if (!Colour.count) {
                    $('#case').hide();
                    $('#nocolours').show();
                }
				$('#colours').spinner();
                url = base + 'colours/get/' + Colour.id;
                Colour.count++;
                $.get(url, function(data){
                    if (data.substr(0, 3) != 'err') {
                        $('#colours').replaceWith(data);
                        Hint.initForColours();
                        Colour.init();
                    }
                });
            }
        });
    },
    
    click: function(colourId, categoryId, el){
        if (colourId) 
            this.id = colourId;
        if (categoryId !== false) 
            this.categoryId = categoryId;
        
        if (!$(el).hasClass('active')) {
        
            if (colourId) {
                $('#colours .colour').removeClass('active');
                $('#colours #colour-' + colourId).addClass('active');
            }
            
            if (categoryId) {
                $('#categories .category').removeClass('active');
                $('#categories #category-' + categoryId).addClass('active');
            }
            
            $('#gallery').spinner();
            this.el = $(el).parent();
            url = base + 'photos/index/' + this.id + '/' + this.categoryId;
            $.get(url, function(data){
                if (data.substr(0, 3) != 'err') {
                    $('#gallery').replaceWith(data);
                    Gallery.init();	
                    Colour.init();
                }
            });
        }
    },
	
    galleryId: false,
    
    prevId: false,
    
    nextId: false,
    
    count: 0,
    
    current: 1,
    
    previous: 0,
    
    imgs: false,
    
    animates: false,
    
    width: 0,
    
    shown: 1,
    
    create: function(galleryId, prevId, nextId, width, shown){
        this.galleryId = galleryId;
        this.prevId = prevId;
        this.nextId = nextId;
        this.width = width;
        this.shown = shown;
        
        this.imgs = $("#" + this.galleryId + " li");
        
        this.count = this.imgs.length;
          
        $("#" + this.galleryId + " ul").css('width', this.count * this.width);
        
		$("#" + this.nextId).removeClass('hover');
		$("#" + this.nextId).removeClass('disabled');
		$("#" + this.prevId).removeClass('hover');
		$("#" + this.prevId).removeClass('disabled');

		
        if (this.current == (this.count - this.shown)) 
            $("#" + this.nextId).addClass('disabled');
        if (this.current == 1) 
            $("#" + this.prevId).addClass('disabled');

        if (this.count < this.shown) {
            $("#" + this.nextId).addClass('disabled');
            $("#" + this.prevId).addClass('disabled');
        }
        
        w = $("#" + this.galleryId).width();
        $("#" + this.galleryId).width(this.width * this.shown);
        
        this.show();
    },
    
    next: function(){
        if (!this.animates) {
            if ((this.current) < (this.count - this.shown + 1)) {
                this.animates = true;
                this.previous = this.current;
                this.current++;
                this.show();
                $("#" + this.nextId).removeClass('disabled');
            }
        }
        
        if (this.current == (this.count - this.shown + 1)) 
            $("#" + this.nextId).addClass('disabled');
        if (this.current != 1) 
            $("#" + this.prevId).removeClass('disabled');
    },
    
    prev: function(){
        if (!this.animates) {
            if ((this.current) > 1) {
                this.animates = true;
                this.previous = this.current;
                this.current--;
                this.show();
                $("#" + this.prevId).removeClass('disabled');
            }
        }
        
        if (this.current == 1) 
            $("#" + this.prevId).addClass('disabled');
        if (this.current != (this.count - this.shown + 1)) 
            $("#" + this.nextId).removeClass('disabled');
    },
    
    show: function(){
        $("#" + this.galleryId + " ul").animate({
            'marginLeft': (-((this.current - 1) * this.width))
        }, "def", "swing", function(){
            Colour.animates = false;
        });
    },
    
    init: function(){
		this.current=1;

        this.create('colours', 'colour-prev', 'colour-next', 35, 16);
		
		if (this.count<this.shown)
		{
			$('.cbg').css('width', '554px');
		} 
		$('.cbg').width($('.cbg').width()+5);
		
		if (!this.initalized) {
			$("#" + this.prevId).click(function(){
				Colour.prev();
				return false;
			});
			
			$("#" + this.nextId).click(function(){
				Colour.next();
				return false;
			});
			
			$("#" + this.prevId).hover(function(){
				if (!$(this).hasClass('disabled')) 
					$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});
			
			$("#" + this.nextId).hover(function(){
				if (!$(this).hasClass('disabled')) 
					$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});
			this.initalized=true;
		}	
		
        $('#colours .action').click(function(e){
            e.stopPropagation();
        });
    }
    
};

var Newsletter = {

    add: function(id, msgErrEmail, msgErrAdded, msgOk, msgCheck){
        if (($("#" + id)) && ($("#" + id).val() != '') && (Validation.validates(id, 'email'))) {
            readers = $('#NewsletterReaders').attr('checked');
            press = $('#NewsletterPress').attr('checked');
			
			if (!readers && !press) {
				Validation.showMessage(id, msgCheck);
				return false;
			}
			e = $("#" + id).val().replace('.', '*');
            $("#" + id).val('');
            Validation.hideMessage(id);
            $("#" + id + 'Error').css('display', 'block').spinner();
            url = base + 'newsletter/add/' + encodeURIComponent(e) + '/' + press + '/' + readers;
            $.get(url, function(data){
                if (data.substr(0, 2) == 'ok') 
                    Validation.showMessage(id, msgOk);
                else 
                    if (data.substr(0, 5) == 'exist') 
                        Validation.showMessage(id, msgErrAdded);
                    else 
                        Validation.showMessage(id, msgErrAdded);
            });
        }
        else 
            Validation.showMessage(id, msgErrEmail);
    }
    
};

var User = {
	
	msg1: '',
	
	msg2: '',
	
	load: function(msgErr1, msgErr2) {
		var email = $("#LoadEmail").val();
		var password = $("#LoadPassword").val();
		this.msg1=msgErr2;
		if ((!email) || (!password))
			$('#i3').html(msgErr1);
		else
		{
			$('#i3').spinner();
			url = base + 'colours/load/';
			$.post(url, $("#dlg3 form").serialize(), function(data){
				if (data.substr(0, 2) == 'ok') 
					location.reload();
				else
					$('#i3').html(User.msg1);
			});
		}
	},
	
	save: function(msgErr1, msgErr2, msgErr3, msgOk) {
		var email = $("#SaveEmail").val();
		var password = $("#SavePassword").val();
		this.msg1=msgErr3;
		this.msg2=msgOk;
		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

		if ((!email) || (!password))
			$('#i1').html(msgErr1);
		else if (password.length<6)
			$('#i1').html(msgErr2);
		else if (!regex.test(email))
			$('#i1').html(msgErr3);
		else
		{
			$('#i1').spinner();
			url = base + 'colours/save/';
			$.post(url, $("#dlg1 form").serialize(), function(data){
				if (data.substr(0, 2) == 'ok') 
					$('#i1').html(User.msg2);
				else
					$('#i1').html(User.msg1);
			});
		}
	},
	
	forgot: function(msgErr1, msgErr2, msgOk) {
		var email = $("#ForgotEmail").val();
		this.msg1=msgErr2;
		this.msg2=msgOk;
		
		if ((!email))
			$('#i2').html(msgErr1);
		else
		{
			$('#i2').spinner();
			url = base + 'colours/forgot/';
			$.post(url, $("#dlg2 form").serialize(), function(data){
				if (data.substr(0, 2) == 'ok') 
					$('#i2').html(User.msg2);
				else
					$('#i2').html(User.msg1);
			});
		}
	}
	
};

$(document).ready(function(){
});

$(window).load(function(){
    Point.init();
    Hint.init();
    Gallery.init();
    Colour.init();
	if ($('.colors-box').length > 0) {
		$('#nogallery').hide();
	}
});

