/**
 * JavaScrits
 */

$(document).ready(function()
{
    $('a.fancybox').fancybox({
        titlePosition:'over'
    });

    //showErrorMessage('');
    
    $('#top a[href^=#]:not(#top a.fancybox)').bind('click', function(event) {            
        event.preventDefault();
        var name = $(this).attr('href').substr(1);
        
        if(!name)
        {
            return false;
        }
        
        if($('a[name='+name+']').length)
        {
            $.scrollTo('a[name='+name+']', 800, {offset: {top: -15}});
        }
        else if($('#'+name).length)
        {
            $.scrollTo('#'+name, 800, {offset: {top: -15}});
        }
    });
    //---------------------------------------------------------------
    
    $('form .text_field, form textarea').focus(function(){
        if($.trim($(this).val()) == $(this).attr('title'))
        {
            $(this).val('');
        }
    });
    //---------------------------------------------------------------
    
    $('form .text_field, form textarea').blur(function(){
        if($.trim($(this).val()) == '')
        {
            $(this).val($(this).attr('title'));
        }
    });
    //---------------------------------------------------------------
    
    if($('#slider').length)
    {
        var slider = $('#slider');
        slider.each(function(){
            var _slider = $(this);
            
            _slider.find('li:not(:first-child)').each(function(){
                $(this).hide().css({opacity: 0});
            });
            
            _slider.addClass('js_slider');
        });
        
        slider.cycle({ 
            fx:     'fade', 
            speed:   1000, 
            timeout: 4000,
            delay:   2000,
            pause:   1,
            //after:   updateSliderNav,
            before:  updateSliderNav
        });
        
        $("#slider_container .controls li").each(function() {
            var currentElement    = $(this);
            var currentElementCls = $(this).attr('class').split("_");
            var goToSlideNr  = parseInt(currentElementCls[1]) - 1; 
            currentElement.find('a').hover(function() {
                slider.cycle(goToSlideNr);
                slider.cycle('pause');
            },function() {
                slider.cycle('resume');
            });
        });
    }
    //---------------------------------------------------------------
    
    $('.submenu li.container > a').click(function(){
        var submenu = $(this).next();
        
        if(submenu.is(':visible'))
        {
            submenu.slideUp(250);
            $(this).removeClass('active');
        }
        else
        {
            submenu.slideDown(250);
            $(this).addClass('active');
        }
        
        return false;
    });
    //---------------------------------------------------------------
    
    if($('#product_gallery').length)
    {
        var working = 0;
        if($('#product_image_previewer').length)
        {
            $('#product_image_previewer').show();
        }
        
        $('#product_images li a').click(function(){
            var href = $(this).attr('href');
            var alt = $(this).attr('title');
            var id = $(this).attr('rel');
            var current = $('#product_image img');
            var currentSrc = current.attr('src');
            
            if(((currentSrc) && (currentSrc == href)) || (working == 1))
            {
                $(this).blur();
                return false;
            }
            
            $('#loading_box').show();
            working = 1;
            
            // Image preload process
            var objImagePreloader = new Image();
            objImagePreloader.onload = function() {
                $('#product_image img').attr({
                    'src': href,
                    'alt': alt,
                    'id': id
                });
                $('#loading_box').fadeOut('fast');
                working = 0;
            };
            objImagePreloader.src = href;
            
            $(this).blur();
            
            var holder = $(this).parent().parent();
            holder.find('a.active').removeClass('active');
            
            $(this).addClass('active');
            
            return false;
        });
        
        $('#product_image a').click(function(){
            var img = $(this).find('img');
            previewBig(img);
            return false;
        });
        
        previewBig = function(obj)
        {
            var id = obj.attr('id');
            var previewObj = $('#preview_'+id);
            if (previewObj && previewObj.length)
            {
                previewObj.trigger('click');
            }
        }
    }
});
//---------------------------------------------------------------

function updateSliderNav(curr, next, opts)
{
    //var current = $('#slider_container .controls li').get(opts.currSlide);
    var next = $('#slider_container .controls li').get(opts.nextSlide);
    
    $('#slider_container .controls a.active').removeClass('active');
    $(next).find('a').addClass('active');
}
//---------------------------------------------------------------

function showErrorMessage(text)
{
    if (text)
    {
        $('#error-message').html(text);
        $('#error-message-container a').trigger('click');
    }
    else
    {
        var html = '';
        
        var errMsgContainer = $('.page-messages-container');
        if (errMsgContainer.length)
        {
            if (errMsgContainer.length > 1)
            {
                errMsgContainer.each(function(){
                    html += $.trim($(this).html());
                });
            }
            else
            {
                html = $.trim(errMsgContainer.html());
            }
        }
        
        if(html)
        {
            $('#error-message').html(html);
            $('#error-message-container a').trigger('click');
        }
    }

}
//----------------------------------------------------------

function validateSearchForm()
{
    var jField = $('#qSearchField');
    var value = $.trim(jField.val());
    var defaultValue = $.trim(jField.attr('title'));
    
    if ((defaultValue) && (value == defaultValue))
    {
        value = '';
    }
    $('#qSearchButton').blur();
    
    return (value) ? true : false;
}
//----------------------------------------------------------

function validateNewslettersForm()
{
    var jField = $('#nEmailField');
    var value = $.trim(jField.val());
    var defaultValue = $.trim(jField.attr('title'));
    
    if ((defaultValue) && (value == defaultValue))
    {
        value = '';
    }
    $('#newslettersSubmitButton').blur();
    
    return (value) ? true : false;
}
//----------------------------------------------------------

function validateForm(selector)
{
	var formObj = $(selector);
	var result = true;
	
	formObj.find('input[type="text"], textarea').each(function(){
        $(this).removeClass('input_error');
        
		if (!$.trim($(this).val()) || ($(this).val() == $(this).attr('title')))
		{
            $(this).addClass('input_error');
			result = false;
		}
	});
	
	formObj.find('.button').blur();
	
	return result;
}
//----------------------------------------------------------

function validateOrderForm(selector)
{
	var formObj = $(selector);
	var result = true;
	
	formObj.find('input[type="text"], textarea').each(function(){
        $(this).removeClass('input_error');
        
        if ($(this).val() == $(this).attr('title'))
        {
            $(this).val('');
        }

        /*
		if (!$.trim($(this).val()) || ($(this).val() == $(this).attr('title')))
		{
            $(this).addClass('input_error');
			result = false;
		}
        */
	});
	
	formObj.find('.button').blur();
	
	return result;
}
//----------------------------------------------------------

