
jQuery.fn.exists = function(){
    return jQuery(this).length>0;
}

function adjustHeight() {

    var total_height = $('#wrapper').height();
    var curr_height, space_left;
	
    // header = 137, path = 35, title = 53, footer = 110, content_padding = 36 ... total = 371
	
    if ( $('.brandsHolder').exists() ) {
	
        return;
	
    } else if ( $('#widePage').exists() ) {
	
        curr_height = $('#widePage .padd').height();
        space_left = total_height - 371;
        if ( curr_height < space_left ) {
            $('#widePage .padd').height(space_left);
        }
		
    }
    else if ( $('#concepts').exists() ) {
	
        curr_height = $('#concepts .padd').height();
        space_left = total_height - 371;
        if ( curr_height < space_left ) {
            $('#concepts .padd').height(space_left);
        }
		
    } else if ( $('#middle').exists() ) {
	
        curr_height = $('#middle').height();
        space_left = total_height - 371;
        if ( curr_height < space_left ) {
            $('#middle').height(space_left);
        }
		
    }

}


var menuflag = 0;
var gflag = 0;
var to = null;
var prev = 0;
var curID = 0;

function showMenu(m) {
    if (prev != 0) tmp = prev.substring(0,4);
    $("#"+tmp).removeClass('hover');
    if ($("#"+m).length == 0) {
        return true;
    }
    hideMenu(prev);
    prev = m;
    $("#"+curID).addClass('hover');
	
    if (menuflag === 0) {
        $("#"+m).fadeIn(200); 
    } else if (menuflag === 1) {
        $('body').removeClass('hm_0');
	
        $("#"+m).show();
		
        if ( $('#homeBg').attr('class') != curID ) {
            $('#homeBg').fadeOut('normal', function() {
                $('#homeBg').attr('class',curID).fadeIn('normal');
                $('body').attr('class',curID);
                $('#home_clock').hide();
            });
        }
    }
    gflag = 1;
}

function hideMenu(m) {
    if ($("#"+m).length == 0) {
        return true;
    }
    if (gflag==0) {
	
        if (menuflag === 0) {
            $("#"+m).fadeOut(200);
        } else if (menuflag === 1) {
            $("#"+m).hide(); 
        }

        $("#"+curID).removeClass('hover');
        clearTimeout(to);
        gflag = 0;
    }
}

function hideMenuTimed(m) {
    gflag = 0;
    to = setTimeout(function() {
        hideMenu(m);
    }, 1); 
}

function fadeOutMenu(m) {
    $("#"+m).fadeOut('100');
    $('#signUpBox').removeClass('act');
    clearTimeout(to);
}

(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

$(document).ready(function(){
    
    // adjustHeight();
	
    $("#slider").easySlider({
        auto: false,
        continuous: true
    });

    /* Top Menu */
    $('#topMenu a').hoverIntent(
        function() {
            curID = $(this).attr("id");
            curPos = $(this).position();
            tmp = curID + '_sm';
            $("#"+tmp).css("left", curPos.left+"px");
			
            menuflag = 0;
            showMenu(tmp);
        },	
        function() {}	
        );
	
    $('#topMenu a').hover(
        function() {
		
            if( $('#wrapper').hasClass('home') ) {
                if ( $('#homeBg').attr('class') != 'hm_0' ) {
                    $('#homeBg').fadeOut('normal', function() {
                        $('#homeBg').attr('class','hm_0').fadeIn('normal');
                        $('body').attr('class','hm_0');
                        $('#home_clock').fadeIn('slow');
                    });
                }
            }
		
        },
        function() {
            curID = $(this).attr("id");
            tmp = curID + '_sm';
            menuflag = 0;
            hideMenuTimed(tmp);
        }
	
        );
	
    $(".subMenu").hover(
        function() {					
            tmp = curID + '_sm';
            clearTimeout(to);				
        },
        function() {		
            tmp = curID + '_sm';
            hideMenuTimed(tmp);			
        }
        );
	
	
    /* Home Menu */
	
    // jQuery.preLoadImages("../img/home/watches.jpg", "../img/home/accessories.jpg", "../img/home/eyewear.jpg", "../img/home/fashion.jpg");	
	
    $('#homeMenu a').click(function(e) {
        e.preventDefault();
    });
	
    $('#homeMenu a').hover(
        function() {
            curID = $(this).attr("id");
            curPos = $(this).position();
            tmp = curID + '_sm';
            $("#"+tmp).css("top", curPos.top+"px");
            menuflag = 1;
            showMenu(tmp);
        },	
        function() {
            curID = $(this).attr("id");
            tmp = curID + '_sm';
            menuflag = 1;
            hideMenuTimed(tmp);
            $('#homeBg').css("opacity", 1);
        }
	
        );
	
    $(".homeSubMenu").hover(
        function() {					
            tmp = curID + '_sm';
            clearTimeout(to);				
        },
        function() {		
            tmp = curID + '_sm';
            menuflag = 1;
            hideMenuTimed(tmp);			
        }
        );

    $('#topSearch .inpt').click(function() {
        if ($(this).attr("value")=="Search by Keyword...") $(this).attr("value", "");
    });
    $("#topSearch .inpt").blur(function() {
        if ($(this).attr("value")=="") $(this).attr("value", "Search by Keyword...");
    });

	
    /* Sign up for offers */	
    $('#signUp').click(function(e) {
        e.preventDefault();
        $('#signUpBox').fadeToggle();
        $('#signUpBox').toggleClass('act');
    });
	
    $("#signUp, #signUpBox.act").hover(
        function() {
            clearTimeout(to);
        },
        function() {
            menuflag = 0;
            to = setTimeout(function() {
                fadeOutMenu('signUpBox')
            }, 1000);
        }
        );
	
    $("#signUpBox").hover(
        function() {
            clearTimeout(to);
        },
        function() {
            menuflag = 0;
            to = setTimeout(function() {
                fadeOutMenu('signUpBox')
            }, 1000);
        }
        );
	
    $('#signUpBox #s_mob').click(function() {
        if ($(this).attr("value")=="Insert Mob. No.") $(this).attr("value", "");
    });
    $('#signUpBox #s_mob').blur(function() {
        if ($(this).attr("value")=="") $(this).attr("value", "Insert Mob. No.");
    });
	
    $('#signUpBox #s_name').click(function() {
        if ($(this).attr("value")=="Insert Name") $(this).attr("value", "");
    });
    $('#signUpBox #s_name').blur(function() {
        if ($(this).attr("value")=="") $(this).attr("value", "Insert Name");
    });
	
    $('#signUpBox #s_mail').click(function() {
        if ($(this).attr("value")=="Insert Your Email") $(this).attr("value", "");
    });
    $('#signUpBox #s_mail').blur(function() {
        if ($(this).attr("value")=="") $(this).attr("value", "Insert Your Email");
    });
        
        
    //Contact Form Validation
    $('#contact_us_form').submit(function(){
            
        var c_n = $('#contact_name').val();
        var c_e = $('#contact_email').val();
        var c_p = $('#contact_phone').val();
        var c_s = $('#contact_subject').val();
        var c_c = $('#contact_comment').val();
            
        var regEmail = /^([-a-zA-Z0-9._]+@[-a-zA-Z0-9.]+(\.[-a-zA-Z0-9]+)+)$/;
        var c_flag = true;
        var c_tofocus = false;
            
        if(c_n == ''){
            $('#error_name').show();
            $('#contact_name').focus();
            c_tofocus = true;
            c_flag = false;
        }
        else if(!isNaN(parseFloat(c_n))){
                    $('#error_name').html('Please input name in alphabets');
                    $('#error_name').show();
                    $('#contact_name').focus();
                    c_tofocus = true;
                    c_flag = false;
                }
        else{
            $('#error_name').html('Please input name');
            $('#error_name').hide();
        }
            
        if(c_e == ''){
            $('#error_email').html('Please input email');
            $('#error_email').show();
            if(!c_tofocus){
                $('#contact_email').focus();
                c_tofocus = true;
            }
            c_flag = false;
        }else if(!regEmail.test(c_e)){
            $('#error_email').html('Please input proper email');
            $('#error_email').show();
            if(!c_tofocus){
                $('#contact_email').focus();
                c_tofocus = true;
            }
            c_flag = false;
        }
        else{
            $('#error_email').hide();
        }
        
        if(c_p == ''){
            $('#error_phone').show();
            if(!c_tofocus){
                $('#contact_phone').focus();
                c_tofocus = true;
            }
            c_flag = false;
        }else{
            $('#error_phone').hide();
        }
        
        if(c_s == ''){
            $('#error_subject').show();
            if(!c_tofocus){
                $('#contact_subject').focus();
                c_tofocus = true;
            }
            c_flag = false;
        }else{
            $('#error_subject').hide();
        }
        
        if(c_c == ''){
            $('#error_comment').show();
            if(!c_tofocus){
                $('#contact_comment').focus();
                c_tofocus = true;
            }
            c_flag = false;
        }else{
            $('#error_comment').hide();
        }
        
        return c_flag;
            
    });
    //END Contact Form Validation
	
});
