var J=jQuery.noConflict();
var curent = 0;
var slideshowTimer = null;
var controlElem = [];
var controlElem2 = [];
var confirm = '';
J(document).ready(function () {

    if (J('#rotator LI').size() > 0){
        J('#rotator LI:last').addClass('current');
        doSlideshow('rotator', true, 5000, '');
    }
    
    if (J('#rotator2 LI').size() > 0){
        J('#rotator2 LI:last').addClass('current');
        doSlideshow('rotator2', true, 5000, '');
    }
    J('#Form_Form_EditableEmailField41, #Form_Form_EditableEmailField48, #Form_Form_EditableEmailField81, #Form_Form_EditableEmailField103').focus(function(){confirm = '';});
    J('#Form_Form_EditableEmailField41, #Form_Form_EditableEmailField48, #Form_Form_EditableEmailField81, #Form_Form_EditableEmailField103').keyup(function(){
        if ((J(this).val().length - confirm.length) > 2){
            J(this).val(confirm);
        } else {
            confirm = J(this).val();
        }
    });
    J('#Form_Form_EditableEmailField41, #Form_Form_EditableEmailField48, #Form_Form_EditableEmailField81, #Form_Form_EditableEmailField103').change(function(){
        if ((J(this).val().length - confirm.length) > 2){
            J(this).val(confirm);
        } else {
            confirm = J(this).val();
        }
    });
    
    
    if (J('#Form_Form_EditableDropdown57').val() == 'Bridal' || 
        J('#Form_Form_EditableDropdown57').val() == 'Bridal and evening wear' ) {
            J('#EditableDateField53').show();
            J('#EditableTextField54').show();
    } else {
        J('#EditableDateField53').hide();
        J('#EditableTextField54').hide();
    }
    
    if (J('#Form_Form_EditableDropdown71').val() == 'Wedding' ) {
        J('#EditableDateField72').show();
        J('#EditableTextField73').show();
        J('#EditableDropdown74').show();
        J('#EditableDateField75').hide();
        J('#EditableDropdown76').hide();
        J('#EditableDropdown77').hide();
    } else {
        J('#EditableDateField72').hide();
        J('#EditableTextField73').hide();
        J('#EditableDropdown74').show();
        J('#EditableDateField75').show();
        J('#EditableDropdown76').show();
        J('#EditableDropdown77').show();
    }    
    
    J('#Form_Form_EditableDropdown57').change(function(){
        if (J(this).val() == 'Bridal' || 
            J(this).val() == 'Bridal and evening wear' ) {
            J('#EditableDateField53').show();
            J('#EditableTextField54').show();
        } else {
            J('#EditableDateField53').hide();
            J('#EditableTextField54').hide();
        }
    });
    
    J('#Form_Form_EditableDropdown71').change(function(){
        if (J(this).val() == 'Wedding' ) {
            J('#EditableDateField72').show();
            J('#EditableTextField73').show();
            J('#EditableDropdown74').show();
            J('#EditableDateField75').hide();
            J('#EditableDropdown76').hide();
            J('#EditableDropdown77').hide();
        } else {
            J('#EditableDateField72').hide();
            J('#EditableTextField73').hide();
            J('#EditableDropdown74').show();
            J('#EditableDateField75').show();
            J('#EditableDropdown76').show();
            J('#EditableDropdown77').show();
        }    
    });
    
    if (J(".content .carousel li").size() > 4 ){
        J(".content .carousel").jCarouselLite({
            btnNext: ".content .arrow.top A",
            btnPrev: ".content .arrow.bottom A",
            visible:4,
            speed: 1000,
            scroll: 2,
            nowrap: 1,
            //auto:2000,
            vertical: true
        });
    } else {
        J('.content .arrow.top A, .content .arrow.bottom A').hide();
    }
    
    J('#SearchForm_GlobalSearchForm_Search').focus(function(){
        if (J(this).val() == 'Search'){J(this).val('');}
    });
    
    J('#SearchForm_GlobalSearchForm_Search').blur(function(){
        if (J(this).val() == ''){J(this).val('Search');}
    });
    
    initSpotlightPager();
    
    J(".content .picture #carousel-pics").jCarouselLite({
        btnNext: ".content .right A",
        btnPrev: ".content .left A",
        visible:1,
        speed: 1000
    });
    
    /*J(".content .main-area .elem").hover(function(){
        J(this).find('h2').hide();
        var overlay = J(this).find('.overlay');
        var box = J(this).find('.box');
        overlay.width(J(this).width()).height(J(this).height());
        box.width(J(this).width()).height(J(this).height());
        box.show();
        overlay.show();    
    }, function(){
        J(this).find('h2').show();
        J(this).find('.overlay').hide();
        J(this).find('.box').hide();
    });
        */
    
    J('#pager LI A').each(function(){
        if ( !J(this).hasClass('prev') && !J(this).hasClass('next') ){
            controlElem.push('#pager .' + J(this).attr("rel"));
        }        
    });    

    
    J(".main-content .craft-carousel").jCarouselLite({
        visible: 1,
        btnNext: ".pages .next",
        btnPrev: ".pages .prev",
        speed: 1000,
        btnGo:controlElem,
        afterEnd: function(a) {
            J('.pages li').removeClass('selected');
            J('.pages li A.' + J(a).attr('class')).parent().addClass('selected');
        }
    });
    
    J('.pages.top-indent LI A').each(function(){
        if ( !J(this).hasClass('prev') && !J(this).hasClass('next') ){
            controlElem2.push('.top-indent .' + J(this).attr("class"));
        }                
    });    
    
    J(".scroll-bar").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible:1,
        speed: 1000,
        btnGo: controlElem2,
        beforeStart: function(a){            
            J('.scroll-bar LI.' + J(a).attr('class')).children().removeClass('active');            
        },
        afterEnd: function(a) {
            J('.pages LI').removeClass('selected');
            J('.pages LI').removeClass('block');
            J('.pages LI').removeClass('last');
            J('.pages LI A.' + J(a).attr('class')).parent().addClass('selected');  
            if(J('.pages LI A.' + J(a).attr('class')).parent().next().next().children().hasClass('next')){
                                J('.pages LI A.' + J(a).attr('class')).parent().prev().prev().prev().addClass('block');
                        }
                        else{
                                J('.pages LI A.' + J(a).attr('class')).parent().next().next().addClass('block last');
                                J('.pages LI A.' + J(a).attr('class')).parent().next().removeClass('last');
                        } 
                         
                        if(J('.pages LI A.' + J(a).attr('class')).parent().next().children().hasClass('next')){
                                J('.pages LI A.' + J(a).attr('class')).parent().prev().prev().prev().prev().addClass('block');
                                J('.pages LI A.' + J(a).attr('class')).parent().prev().prev().prev().addClass('block');
                        }
                        else{
                                J('.pages LI A.' + J(a).attr('class')).parent().next().addClass('block');
                        }
                        
                        if(J('.pages LI A.' + J(a).attr('class')).parent().prev().prev().children().hasClass('prev')){
                                J('.pages LI A.' + J(a).attr('class')).parent().next().next().next().addClass('block last');
                                J('.pages LI A.' + J(a).attr('class')).parent().next().next().removeClass('last');
                                J('.pages LI A.' + J(a).attr('class')).parent().next().removeClass('last');
                        }
                        else{
                                J('.pages LI A.' + J(a).attr('class')).parent().prev().prev().addClass('block');
                        }
                        
                        if(J('.pages LI A.' + J(a).attr('class')).parent().prev().children().hasClass('prev')){
                                J('.pages LI A.' + J(a).attr('class')).parent().next().next().next().next().addClass('block last');
                                J('.pages LI A.' + J(a).attr('class')).parent().next().next().next().addClass('block');
                                J('.pages LI A.' + J(a).attr('class')).parent().next().next().next().removeClass('last');
                                J('.pages LI A.' + J(a).attr('class')).parent().next().next().removeClass('last');
                                J('.pages LI A.' + J(a).attr('class')).parent().next().removeClass('last');
                        }
                        else{
                                J('.pages LI A.' + J(a).attr('class')).parent().prev().addClass('block');
                        }
                        J(".pages.top-indent li:has(a.prev)").addClass('block');
                        J(".pages.top-indent li:has(a.next)").addClass('block');
                        
            J(a).children().addClass('active');
                        var img_id = J(a).find('img').get(0).id;
                        var content = J('#titles .'+img_id).html();             
            J('#img-title').html(content);
                        title = "";
                        content = "";
            var name = J(a).find('img').attr('alt');
            J('#img-name').html(name);
        } 
    });
    J(".pages.top-indent li:has(a.prev)").addClass('block');
    J(".pages.top-indent li:has(a.next)").addClass('block');
        J(".pages.top-indent li.selected").next().addClass('block');
        J(".pages.top-indent li.selected").next().next().addClass('block');
        J(".pages.top-indent li.selected").next().next().next().addClass('block');
        J(".pages.top-indent li.selected").next().next().next().next().addClass('block last');

        
    //narayan :: validate the press registration form
        if(J("#PressMemberSignupForm_PressMemberSignupForm").length > 0){
                J("#PressMemberSignupForm_PressMemberSignupForm").validate({
                        rules: {
                                FirstName: {
                                        required: true
                                },
                                Surname: {
                                        required: true
                                },
                                Email: {
                                        required: true,
                                        email: true
                                },
                                ConfirmEmail: {
                                        required: true,
                                        email: true
                                },
                                Password: {
                                        required: true
                                },
                                AddressLine1: {
                                        required: true
                                },
                                Message: {
                                        required: true
                                },
                                City: {
                                        required: true
                                },
                                Country: {
                                        required: true
                                },
                                ConfirmEmail: {
                                        required: true,
                                        equalTo: "#PressMemberSignupForm_PressMemberSignupForm_Email"
                                }
                        },
                        messages: {

                                ConfirmEmail: {
                                        equalTo: "The email addresses provided have to match."
                                }
                        }
                });
                }
    
        if(J("#AppointmentForm_AppointmentForm").length > 0){
                J("#AppointmentForm_AppointmentForm").validate({
                        rules: {
                                FirstName: {
                                        required: true
                                },
                                Surname: {
                                        required: true
                                },
                                Telephone: {
                                        required: true
                                },                              
                                Email: {
                                        required: true,
                                        email: true
                                },
                                ConfirmEmail: {
                                        required: true,
                                        email: true
                                },
                                AddressLine1: {
                                        required: true
                                },
                                Message: {
                                        required: true
                                },
                                City: {
                                        required: true
                                },
                                Country: {
                                        required: true
                                },
                                ConfirmEmail: {
                                        required: true,
                                        equalTo: "#AppointmentForm_AppointmentForm_Email"
                                }
                        },
                        messages: {

                                ConfirmEmail: {
                                        equalTo: "The email addresses provided have to match."
                                }
                        }
                });
                }    
        
        /* J('#gallery_slide_prev').hover(function () {
                   J('#gallery_slide_prev div').animate({width: 'show'});
                     },
                     function () {
                   J('#gallery_slide_prev div').animate({width: 'hide'});
                   });
                   
        J('#gallery_slide_next').hover(function () {
                   J('#gallery_slide_next div').animate({width: 'show'});
                     },
                     function () {
                   J('#gallery_slide_next div').animate({width: 'hide'});
                   });
        */   
        J('#gallery_slide_prev div').click(function(){
                J('.content .left A').trigger('click');
        });
        
        J('#gallery_slide_next div').click(function(){
                J('.content .right A').trigger('click');
        });     
        
        
    J(".content .small-carousel .lists").jCarouselLite({
        btnNext: ".content .right A, #gallery_slide_next span",
        btnPrev: ".content .left A, #gallery_slide_prev span",
        visible:1,
        speed: 1000,
        beforeStart: function(a){
            curent = J(a).find('a').attr('rel');  
        },
        afterEnd: function(a) {
            var next = J(a).find('a').attr('rel');
            //J('#descr').html(J(a).find('.hide').html());
            var title = J(a).find('img').attr('title');
            J('#img-title').html(title);
                        title = "";             
            var name = J(a).find('img').attr('alt');
            J('#img-name').html(name);            
            
           var size = J('.all-images a').size();
            if ( next > curent ){            
                if (curent == 1 && next == size ) {
                    prevPics(curent, next, size);
                } else {
                    nextPics(curent, next, size);
                }
            } else {
                if (curent == size && next == 1 ) {
                    nextPics(curent, next, size);
                } else {
                    prevPics(curent, next, size);
                }
            }
        } 
        
    });
    
    /*J(".content .picture .lists").jCarouselLite({
        btnNext: ".content .right A",
        btnPrev: ".content .left A",
        visible:1,
        speed: 1000,
        beforeStart: function(a){
            curent = J(a).find('a').attr('rel');  
        },
        afterEnd: function(a) {
            var next = J(a).find('a').attr('rel');
            //J('#descr').html(J(a).find('.hide').html());
            var title = J(a).find('img').attr('title');
            J('#img-title').html(title);
                        title = "";             
            var name = J(a).find('img').attr('alt');
            J('#img-name').html(name);            
            
           var size = J('.all-images a').size();
            if ( next > curent ){            
                if (curent == 1 && next == size ) {
                    prevPics(curent, next, size);
                } else {
                    nextPics(curent, next, size);
                }
            } else {
                if (curent == size && next == 1 ) {
                    nextPics(curent, next, size);
                } else {
                    prevPics(curent, next, size);
                }
            }
        } 
        
    });*/

    //J('.stylish-select').customStyle();
});

function nextPics(cur, next, size ){
    var prevEl = J('.pics IMG.last');
    var curEl = J('.pics IMG.first');
    var nextEl = J('.pics IMG.next');
    //curEl.hide().addClass('next').removeClass('first');
    //nextEl.show().addClass('first').removeClass('next');
    //prevEl.attr('src', curEl.attr('src'));
    var loadEl = parseInt(next) + 1;
    if ( loadEl > size){ loadEl = 1;}                
    //curEl.attr('src', J('.all-images a.' + loadEl).attr('href'));        
    var el = J('#carousel-pics LI.' + loadEl+ ' IMG');
    if (el.attr('src') == ''){
        el.attr('src', J('.all-images a.' + loadEl).attr('href'));
    }
}

function initSpotlightPager(){
    J("#rotator3 LI A").click(function(){
        J('#pager').load(J(this).attr('href') + '&ajax=1', function(){initSpotlightPager();});
        var curr = J('.rotator LI.current');
        var next = J('.rotator LI.'+J(this).attr('rel'));
        curr.animate({opacity:0}, 2000, function(){ J(this).removeClass('current').css('z-index','1') });
        next.animate({opacity:1}, 2000, function(){ J(this).addClass('current').css('z-index','2') });        
        
        return false;
    });
}

function prevPics(cur, next, size ){
    var prevEl = J('.pics IMG.next');
    var curEl = J('.pics IMG.first');
    var nextEl = J('.pics IMG.last');
    //curEl.hide().addClass('last').removeClass('first');
    //nextEl.show().addClass('first').removeClass('last');
    //prevEl.attr('src', curEl.attr('src'));
    var loadEl = parseInt(next) - 1;    
    if ( loadEl == 0){ loadEl = size;}                
    //curEl.attr('src', J('.all-images a.' + loadEl).attr('href'));    
    var el = J('#carousel-pics LI.' + loadEl+ ' IMG');
    if (el.attr('src') == ''){
        el.attr('src', J('.all-images a.' + loadEl).attr('href'));
    }
}

function isVisible(obj) { return (obj.css('display') == 'block'); } 

function doSlideshow(objid, start, time, navId){
        if(!J('#' + objid) || !isVisible(J('#' + objid))){
                if(slideshowTimer != null){
                        clearTimeout(slideshowTimer);
                        slideshowTimer = null;
                }
                return;
        }
        
        if(start){
                clearTimeout(slideshowTimer);
                slideshowTimer = null;
        }
        
        var currObj = J('#' + objid + ' li.current');
        var nextObj = currObj.next();
        if(J('#' + objid + ' li:last').hasClass('current')){ 
                nextObj = J('#' + objid + ' li:first');
        }
    
    nextObj.addClass('current');
        nextObj.css('opacity', 0).css('z-index','2');
        currObj.removeClass('current').css('z-index','1');
        currObj.animate({opacity:0}, 2000);
        nextObj.animate({opacity:1}, 2000);
    J('.button a').attr('href', nextObj.find('.hide').attr('href'));
    
    if(navId != ''){        
        J('#' + navId + ' .'+currObj.attr('id')).removeClass('active');
        J('#' + navId + ' .'+nextObj.attr('id')).addClass('active');
    }
        
        slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ',"' + navId + '");', time);
}

(function($){
    $.fn.extend({
        customStyle : function(options){
            if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
                return this.each(function(){
                    $(this).wrap('<div class="customStyleSelectCont" />');
                    var currentSelected = $(this).find(':selected');
                    $(this).after('<div class="customStyleSelectBox"><div class="customStyleSelectBoxInner">'+currentSelected.text()+'</div></div>').css({opacity:0});
                    var selectBoxSpan = $(this).next();
                    var selectBoxSpanInner = selectBoxSpan.find(':first-child');
                    $(this).change(function(){
                        selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
                    });
                });
            }
        }
    });
})(jQuery);


/*Trade detail>>*/

jQuery(document).ready(function($) {
    jQuery('.karysel_5 ul').jcarousel({
                scroll:5,
                visible: 5,
                initCallback: function(carousel){
                    $('.prev_1').hide();
                    $('.next_1').hide();
                        $('.prev_5').click(function(){
                                carousel.prev();
                        });
                        $('.next_5').click(function(){
                                carousel.next();
                        });
                        $('.karysel_5 li').click(function(){
                                var li = $(this),
                                        index = li.attr('jcarouselindex'),
                                        t = $('.karysel_1 ul').data('jcarousel');
                                        t.scroll(parseInt(index));
                                a = li.find('a');
                                if(a.attr('rel') == '#'){
                                        $('.viewback').hide();
                                } else { $('.viewback').show(); };
                                //console.log(a.attr('rel'));
                                        $('.karysel_5').fadeOut("slow");
                                        $('.content_tradegallery').hide();
                                        getContent(li.attr('id'));                      
                                $('.prev_1').show();
                                $('.next_1').show();
                        })
                }
                
    });
    
    jQuery('.karysel_1 ul').jcarousel({
                scroll:1,
                visible:1,
                initCallback: function(carousel){
                        var showContent = function(i){
                                var index = i.index(i.first),
                                        opt = $('.karysel_1 li[jcarouselindex="'+index+'"]');
                                        $('.content_tradegallery').hide();
                                        getContent(opt.attr('id'));     
                                };
                                                                
                        $('.prev_1').click(function(){
                                carousel.prev();
                                showContent(carousel);
                        });
                        $('.next_1').click(function(){
                                carousel.next();
                                showContent(carousel);
                        });
                }
    });

    jQuery('.viewback').click(function(){
                var cont = $('.karysel_1 ul').data('jcarousel'),
                        index = cont.index(cont.first),
                        opt = $('.karysel_1 li[jcarouselindex="'+index+'"]'),
                        a = opt.children('a').attr('rel'),
                        aa = opt.children('a').attr('href'),
                        img = opt.find('img'),
                                text = $(".viewback").text();
                        if( text == "View Back" ) {
                                img.attr('src', a);
                                $(".viewback").text("View Front");
                        } else {
                                img.attr('src', aa);
                                $(".viewback").text("View Back");
                        }
        });
    
    jQuery('#click').click(function(){
        $('.karysel_5').fadeIn(2000);
        clearBack();
        $('.viewback').hide();
    });
        
    jQuery('.prev_1').click(function(){
        clearBack();
    });

    jQuery('.next_1').click(function(){
        clearBack();
    });
        
    function getContent(id) {
        id = id.split('_')[1],
        $('#cont_'+id).show();
    }
        
        function clearBack() {
        var cont = $('.karysel_1 ul').data('jcarousel'),
                index = cont.index(cont.first),
                opt = $('.karysel_1 li[jcarouselindex="'+index+'"]'),
        
                a = opt.children('a').attr('rel');
                if(a == "#"){
                        $('.viewback').hide();
                } else {  
                        aa = opt.children('a').attr('href'),
                        img = opt.find('img').attr('src', aa);
                        $('.viewback').show();
                        $(".viewback").text("View Back");
                }
        }

        $('.karysel_1 a').click(function() {
                var $this = $(this),
                        imagesContainer = $('.zoom-images'),
                        src = $this.find('img').attr('src'),
                        isBack = false;

                
                if (src == $this.attr('rel')) {
                        isBack = true;
                }
                
                var id = $(this).parent().attr('id').split('_')[1];
                
                var selected = imagesContainer.find('#zoomimg_' + id);
                imagesContainer.find('li.zoomed-image-container').hide();
                
                var img = selected.find('img'),
                        imgHref = img.parent();

                var zoomImgSrc = isBack ? imgHref.attr('href') : imgHref.attr('rel');

                img.attr('src', zoomImgSrc);
                
                selected.show();
                
                imagesContainer.css('visibility', 'visible');
        });

        $('.zoom-images').click(function() {
                $(this).css('visibility', 'hidden');
        });

        if ($.fn['hoverscroll']) {
                $('.zoom-images .zoomed-image').hoverscroll({
                        vertical: true,
                        fixedArrows: true,
                        height: 721,
                        width: 1200
                });
        }
        $('.lup').click(function(){
            
            //var $this = $(this),
            var imagesContainer = $('.zoom-images'),isBack = false;
            
            var id = $('.karysel_1 a').parent().attr('id').split('_')[1];
                        
            var selected = imagesContainer.find('#zoomimg_' + id);
                imagesContainer.find('li.zoomed-image-container').hide();
                
                var img = selected.find('img'),
                        imgHref = img.parent();

                var zoomImgSrc = isBack ? imgHref.attr('href') : imgHref.attr('rel');

                img.attr('src', zoomImgSrc);
                
                selected.show();
                
                imagesContainer.css('visibility', 'visible');
        });
});

/*<<Trade detail*/


/* === International Stockists Page List === */

function showData(data){
    jQuery(function($){
        $('.stylish-select').customStyle();
        $("#country").live('change', function(){
            $("#city").html( getOptionsData(getCities($(this).val())) );
	    updateSelect($("#city"));
	    //$("#city [value='']").attr('selected','selected');
        });
        $("#city").live('change',function(){
            var country = getContries($(this).val());
            if( $(this).val() ){
		$('#country option').removeAttr('selected');
                $("#country [value='"+country+"']").attr('selected','selected');
                updateSelect($("#country"));
            }
        });
        function updateSelect(select){
            var upd = select.parent();
	    var p = upd.parent();
	    var c = select.clone();
            //console.log(p);
                upd.remove();
                p.append(c);
		c.customStyle();
        }
        function getCities(country){
            return data[country];
        }
        function getContries(city){
            var c;
            for(var i in data){
                for(var j in data[i]){
                    if( city == data[i][j]){
                        c = i;
                    }
                }
            }
            return c;
        }
        function getOptionsData(data){
            var options = '';
                options += '<option value="" selected="selected">Select City...</option>';
                for (var i in data) {
                    options += '<option>' + data[i] + '</option>';
                }
            //console.log(data);
            return options;
        }

    });
}

/* === END ===*/

