var rel_active;
var locked = false;


$(document).ready(function() {

    /***** fix selectboxes *****/
	
    $('select,input:checkbox,input:radio').uniform();

    /***** style switch *****/

    /*	$(document).keypress(function(e) {
		if (e.charCode == 43) {
			$('head').append('<link type="text/css" rel="stylesheet" href="styles/interface-black.css" media="screen" />');
		}
		if (e.charCode == 45) {
			$('head').append('<link type="text/css" rel="stylesheet" href="styles/interface.css" media="screen" />');
		}
	});*/
	
    /***** lightboxes *****/
	
    if ($('div.blog-image a.lightbox').length) {
        $('div.blog-image a.lightbox').lightBox();
    }
	
    /***** button submit *****/

    $('a.autosubmit, input.autosubmit').click(function() {
        $(this).closest('form').submit();
    });
	
    /***** button hover *****/
	
    $('a.button').hover(function() {
        $(this).addClass("button-active");
    }, function(){
        $(this).removeClass("button-active");
    });

    $('a.button-large').hover(function() {
        $(this).addClass("button-large-active");
    }, function(){
        $(this).removeClass("button-large-active");
    });

    $('a.color').hover(function() {
        $(this).addClass("color-active");
    }, function(){
        $(this).removeClass("color-active");
    });

    $('a.color-large').hover(function() {
        $(this).addClass("color-large-active");
    }, function(){
        $(this).removeClass("color-large-active");
    });
	
    /***** input focus *****/

    $('input.frm-txt, textarea').focus(function() {
        if ($(this).val() == $(this)[0].title + " *" || $(this).val() == $(this)[0].title) {
            $(this).val("");
            $(this).removeClass("titled");
        }
    });

    $('input.frm-txt, textarea').blur(function() {
        if (($(this).val() == "") || ($(this).val() == $(this)[0].title) || ($(this).val() == $(this)[0].title + " *")) {
            var mandatory = $(this).attr('mandatory') == 'true' ? ' *' : '';
            $(this).val($(this)[0].title + mandatory);
            $(this).addClass("titled");
        }
    });

    $('input.frm-txt, textarea').blur();
    $('option[mandatory=true]').html($('option[mandatory=true]').html() + ' *');
    $('option:selected[mandatory=true]').parent('select').prev('span').append(" *");

    /***** faq items expansion *****/
	
    var expand_animation = false;
    $('.faq-items h3').click(function() {
        if (!expand_animation) { // don't accept clicks during animation
            var expanded = false;
            expand_animation = true;
            if (!$(this).parent().hasClass('active')) {
                $(this).css('paddingTop',0);
                $(this).parent().find("div.faq-content").css('display','none');
                $(this).parent().addClass('active');
                expanded = true;
            }
            var padding = 0;
            /*			if (($(this).parent().prev().length) && (expanded)) {*/
            if (expanded) {
                padding = 18;
            }
            $(this).animate({
                paddingTop: padding
            }, 'slow', function() {
                // Animation complete.
                });
            $(this).parent().find("div.faq-content").animate({
                height: 'toggle'
            }, 'slow', function() {
                // Animation complete.
                if(!expanded) {
                    $(this).parent().removeClass('active');
                }
                expand_animation = false;
            });
        }
    });

    /***** home streamers *****/
	
    var streamers = $('div#streamer a.streamer-content');
    if (streamers.length) { // streamers found
        init_slide();
    }
	
    $('div#streamer-index div').click(function() {
        set_slide($(this).index());
    });
	
    /***** webshop highlight *****/
	
    $('div#webshop-categories a').hover(function() {
        $(this).find('span').stop(true, true).animate({
            opacity: 'toggle'
        }, 'fast', function() {
            // Animation complete.
            });
    }, function(){
        $(this).find('span').stop(true, true).animate({
            opacity: 'toggle'
        }, 'fast', function() {
            // Animation complete.
            });
    });
	
    /***** overview highlight *****/
	
    $('ul.overview-products li').hover(function() {
        $(this).find('span').stop(true, true).animate({
            opacity: 'toggle'
        }, 'fast', function() {
            // Animation complete.
            });
    }, function(){
        $(this).find('span').stop(true, true).animate({
            opacity: 'toggle'
        }, 'fast', function() {
            // Animation complete.
            });
    });
	
    /***** detail image zoom *****/
	
    $('div#detail-image').hover(function() {
        if (!$(this).hasClass('no-zoom')) {
            $('img#image-medium').stop(true, true).animate({
                opacity: 0
            }, 'fast', function() {
                // Animation complete.
                $('img#image-medium').hide();
            });
            $(this).find('span').stop(true, true).animate({
                opacity: 0
            }, 'fast', function() {
                // Animation complete.
                $(this).find('span').hide();
            });
        }
    }, function(){
        if (!$(this).hasClass('no-zoom')) {
            $('img#image-medium').stop(true, true).show();
            $('img#image-medium').stop(true, true).animate({
                opacity: 1
            }, 'fast', function() {
                // Animation complete.
                $('img#image-medium').show();
            });
            $(this).find('span').stop(true, true).show();
            $(this).find('span').stop(true, true).animate({
                opacity: 1
            }, 'fast', function() {
                // Animation complete.
                $(this).find('span').show();
            });
        }
    });

    $('div#detail-image').mousemove(function(e) {
        $('a#image-large').css('top',(e.pageY-$('div#detail-image').offset().top)*-2);
        $('a#image-large').css('left',(e.pageX-$('div#detail-image').offset().left)*-2);
    });
	
    $('a#image-large').lightBox();
	
    /***** detail image switch *****/
	
    $('div#detail-tumbs img').click(function() {
        $('div#detail-tumbs').find("img.active").removeClass('active');
        $(this).addClass('active');
        var src_url = $(this).attr('rel').split(';');
        $('img#image-medium').attr('src', src_url[0]);
        $('a#image-large img').attr('src', src_url[1]);
        $('a#image-large').attr('href', src_url[1]);	
    });
	
    /***** detail order form *****/
	
    $('select#order-size').change(function() {
        $('div#detail-price').html($(this).find("option:selected").attr('title'));
    });
	
    /***** detail related switch *****/
	
    $('div#detail-related-labels label').click(function() {
        $('div#detail-related-labels label').each(function() {
            $(this).removeClass('active');
            $('#'+$(this).attr('rel')).hide();
        });
        $(this).addClass('active');
        $('#'+$(this).attr('rel')).show();
    });

    $('div#detail-related-labels-wide label').click(function() {
        var expanded = true;
        var o = $(this);
        if (!$(this).hasClass('active')) {
            $(this).addClass('active');
            expanded = false;
        }
        $('ul#'+$(this).attr('rel')).animate({ // show next slide
            height: 'toggle'
        }, 'slow', function() {
            // Animation complete.
            if (expanded) {
                o.removeClass('active');
            } else {
                $('html, body').animate({
                    scrollTop: $(document).height()
                }, 1000, function() {
                    // Animation complete.
                    });
            }
        });
    });
		
    /***** blog images *****/
	
    $('div#content ul.blog-results li div.blog-image a').hover(function() {
        $(this).find('span').stop(true, true).animate({
            opacity: 'toggle'
        }, 'fast', function() {
            // Animation complete.
            });
    }, function(){
        $(this).find('span').stop(true, true).animate({
            opacity: 'toggle'
        }, 'fast', function() {
            // Animation complete.
            });
    });
	
    /***** twitter feed *****/
	
    if ($('div#twitter-feed').length) {
        $('div#twitter-feed').css('height', $('div#content-main-left').height()-45);
    }

    /***** account orders *****/
	
    $('tr.account-order').hover(function() {
        $(this).addClass("active");
    }, function(){
        $(this).removeClass("active");
    });

    /***** basket images *****/
	
    $('tr.basket-article').hover(function() {
		if ($('div#content-side-right a').length) {
			$('div#content-side-right a').replaceWith($('div#content-side-right img'));
			$('div#content-side-right img').addClass('image-side');
		}
        $('div#content-side-right img').attr('src', $(this).attr('rel'));
        $(this).addClass("active");
    }, function(){
        $(this).removeClass("active");
    });
    
    /****** address *****/
    
	$('a#deliveryCopy-true').click(function() {
//		if ($(this).is(':checked')) {
			$(this).closest('form').find('input, select').each(function() {
				if ($(this).attr('rel')) {
					if ($(this).is("input[type=text]")) {
						$(this).val($('input#'+$(this).attr('rel')).val());
						$(this).removeClass("titled");
						$(this).blur();
					} else if ($(this).is("input[type=radio]")) {
						$(this).attr('checked',$('input#'+$(this).attr('rel')).attr('checked'));
						$.uniform.update($(this));
					} else if ($(this).is("select")) {
						$(this).val($('select#'+$(this).attr('rel')).val());
						if (!$(this).val()) {
							$(this)[0].selectedIndex = 0;
						}
						$.uniform.update($(this));
						$(this).parent().find('span').removeClass('header');
						if (!$(this).val()) {
							$(this).parent().find('span').addClass('header');
						}
					}
				}
			});
//		}
    });

    /***** light boxes *****/


    $(window).resize(function() { // position updates on resize
        if ($('div#'+rel_active).length) { // download lightbox
            $('div#'+rel_active).css("top",parseInt(($(window).height()-$('div#'+rel_active).height())/2)+'px');
            $('div#overlay-bg').css("height",$(document).height());
        }
    });

    $('a.overlaybox').click(function() {
        if (!locked) {
            locked = true; // lock clicks until animation finished
            rel = ($(this).attr("rel"));
            if ($('div#'+rel).length != 0) {
                $('div#'+rel).css("top",parseInt(($(window).height()-$('div#'+rel).height())/2)+'px'); // center lightbox vertical
                $('div#overlay-bg').css("height",$(document).height()).show().css("opacity","0.7");
                $('div#'+rel).animate({ // show lightbox overlay
                    opacity: 'toggle'
                }, 'slow', function() {
                    // Animation complete.
                    locked = false;
                });
                rel_active = rel;
            }
        }
    });

    $('div.overlay a.overlay-close').click(function() {
        if (!locked) {
            $('div#overlay-bg').css("opacity","0").hide(); // hide lightbox
            $(this).parent().hide();
            rel_active = '';
        }
    });
    $('div.overlay a.button-close').click(function() {
        if (!locked) {
            $('div#overlay-bg').css("opacity","0").hide(); // hide lightbox
            $(this).closest('div.overlay').hide();
            rel_active = '';
        }
    });
    $('div#overlay-bg').click(function() {
        if (!locked) {
            $(this).css("opacity","0").hide(); // hide lightbox
            $('div#'+rel_active).hide();
            rel_active = '';
        }
    });
    
    /***** calendar *****/
    $('#calendarMonth').change(function(){
        var month = $(this).val();
        if(month > 0) {
            $('ul.agenda-results > li').hide();
            $('ul.agenda-results > li[month='+ month +']').show();
        } else {
            $('ul.agenda-results > li').show();
        }
        
        //agenda-no-results
        if($('ul.agenda-results > li:visible').length == 0) {
            $('#agenda-no-results').show();
        } else {
            $('#agenda-no-results').hide();
        }
    });
    $('#calendarMonth').change(); //set initial
});


/***** home streamers extension *****/

var active_slide = 0;
var streamers;
var streamer_idxs;
var timer;

function init_slide() {
    streamers = $('div#streamer a.streamer-content'); // get streamer elements
    active_slide = Math.floor(Math.random()*streamers.length);
    if (streamers.length > 1) {
		for (var i=0; i<streamers.length; i++) {
			if (i==active_slide) {
				$('<div class="active"></div>').appendTo('div#streamer-index');
			} else {
				$('<div></div>').appendTo('div#streamer-index');
			}
		}
	}
    streamer_idxs = $('div#streamer-index div'); // get streamer elements
    $(streamers[active_slide]).toggle();
    if (streamers.length > 1) {
        timer = setTimeout("next_slide()", 8000); // start animation
    }
}

function set_slide(s) {
    clearTimeout(timer);
    $(streamers[active_slide]).stop();
    $(streamers[active_slide]).hide();
    $(streamer_idxs[active_slide]).removeClass('active');
    active_slide = s;
    $(streamers[active_slide]).css({
        'opacity':1
    });
    $(streamers[active_slide]).show();
    $(streamer_idxs[active_slide]).addClass('active');
    if (streamers.length > 1) {
		timer = setTimeout("next_slide()", 8000);
	}
}

function next_slide() {

    var handled_slide = active_slide + 1;
    var new_slide = handled_slide;

    if (new_slide >= streamers.length) {
        handled_slide = active_slide;
        new_slide = 0;
        $(streamers[new_slide]).show();
    }

    $(streamer_idxs[active_slide]).removeClass('active');
    $(streamer_idxs[new_slide]).addClass('active');
	
    $(streamers[handled_slide]).animate({ // show next slide
        opacity: 'toggle'
    }, 'slow', function() {
        // Animation complete.
        $(streamers[active_slide]).hide();			
        active_slide = new_slide;		 
        // Animation complete.
        timer = setTimeout("next_slide()", 8000);
    });
}

function set_overlay(rel) {
	if (!locked) {
		locked = true; // lock clicks until animation finished
		if ($('div#'+rel).length != 0) {
			$('div#'+rel).css("top",parseInt(($(window).height()-$('div#'+rel).height())/2)+'px'); // center lightbox vertical
			$('div#overlay-bg').css("height",$(document).height()).show().css("opacity","0.7");
			$('div#'+rel).animate({ // show lightbox overlay
				opacity: 'toggle'
			}, 'slow', function() {
				// Animation complete.
				locked = false;
			});
			rel_active = rel;
		}
	}
}
