$(function () {
    $('#search .field, #login .field').each(function () {
        $(this).focus(function () {
            $(this).parent().find('input[type=submit]').addClass('active');
        }).blur(function () {
            $(this).parent().find('input[type=submit]').removeClass('active');
        });
    });
    $('#clients ul li').hover(function () {
        $('#clients ul li').not($(this)).stop(true, true).animate({
            'opacity': 0.2
        });
    }, function () {
        $('#clients ul li').stop(true, true).animate({
            'opacity': 1.0
        });
    });

    $('ul.productnav li ul li a, .secondproductnav li a, .prodfeatures ul li a').each(function () {
        if ($(this).attr('href') == '#') {
            var thist = $(this).text();
            thist = $.trim(thist);
            thist = thist.replace(/ /g, '_');
            thist = 'product-' + thist + '.php';
            //thist = thist.toLowerCase();
            $(this).attr('href', thist);
        }
    });


    //$.fn.colorbox({href:'/href.html', open: true, iframe: true, width:300, height:400});


    $('form').submit(function () {
        if ($(this).attr('action') == '#') {
            return false;
        }
    });
    $('a[href=#]').click(function () {
		return false;
    });

    var dro_products = ['EDM Wire', 'EDM Sinker', 'EDM Drill', 'Laser', 'Milling Vertical', 'Milling Horizontal', 'Press Brakes', 'Profile Grinding', 'Gun Drilling', 'Surface Grinding', 'Die Spotting'];

	
    var edm_wire_searchform = {
		'product-FA_10s_Advance.php':'FA 10S Advance', 
		'product-FA_20S_Advance.php':'FA 20S Advance', 
		'product-FA_10PS_Advance.php':'FA 10PS Advance', 
		'product-FA_20PS_Advance.php':'FA 20PS Advance', 
		'product-FA_30V.php':'FA 30V', 
		'product-FA_40VM.php':'FA 40VM', 
		"product-FA_50VM.php":"FA 50VM", 
		'product-BA8.php':'BA 8', 
		'product-BA24.php':'BA 24', 
		'product-NA_1200.php':'NA 1200', 
		'product-NA_2400.php':'NA 2400'
	};
    var edm_sinker_searchform = {
		'product-EA_8.php':'EA 8', 
		'product-EA8PV_Advance.php':'EA8PV Advance', 
		'product-EA_12V.php':'EA 12V', 
		'product-EA_28V.php':'EA 28V', 
		'product-EA_30.php':'EA 30', 
		'product-EA_40.php':'EA 40', 
		'product-EA_50.php':'EA 50'
	};
    var edm_drill_searchform = {
		'product-ED_2000M.php':'ED 2000M',
		'product-ed_2000m2.php':'ED 2000M2',
		'product-ED_2000NC.php':'ED 2000NC'
		};
    var laser_searchform = {
		'product-LV_PLUS.php':'LV PLUS',
		 'product-HV_PLUS.php':'HV PLUS',
		  'product-ex.php':'eX SERIES',
		   'product-nx.php':'NX SERIES',
		    'product-vz10.php':'VZ SERIES'
		};
    var milling_vertical_searchform = {
	'product-vms.php':'VMS',
	 'product-vp400d.php':'VP400D'
	 };

    var milling_horizontal_searchform = {
		'product-hm_series.php':'HM Series',
		 'product-mch.php':'MCH'
		  };
    var press_brakes_searchform = {
		'product-diamond_ultara.php':'Diamond Ultra',
		'product-diamond_elite.php':'Diamond Elite', 
		'product-diamond_smart.php':'Diamond Smart'
	};
    var die_spotting_searchform = {
		'product-die_spotting.php':'Die Spotting'
		};
    var profile_grinding_searchform = {
		'product-profile_grinding.php':'Profile Grinding'
		};
    var surface_grinding_searchform = {
		'product-surface_grinding.php':'Surface Grinding'
		};
    var gun_drilling_searchform = {
		'product-gun_drilling.php':'Gun Drilling'
		};
	/*var curarray = eval($('#findproduct #value1').val());
	var opt = '';
	for(var i=0; i<curarray.length; i++){
		opt += '<option>'+curarray[i]+'</option>';
	}
	$('#findproduct #value2').html(opt);*/
    var newopt = '';
    for (var i = 0; i < dro_products.length; i++) {
        var newop = $.trim(dro_products[i]);
        newop = newop.replace(/ /g, '_');
        newop = newop.toLowerCase();
        newopt += '<option value="' + newop + '">' + dro_products[i] + '</option>';
    }
    //alert(newopt);
    $('#findproduct #value1').html('<option value="">Select</option>' + newopt);


    var hamidval = $('#findproduct #value1').val();
    if (hamidval != '' && hamidval != null) {
        var curarray = eval(hamidval + '_searchform');
        var opt = '';
        for (var i = 0; i < curarray.length; i++) {
            opt += '<option>' + curarray[i] + '</option>';
        }
    } else {
        var opt = '';
    }
    $('#findproduct #value2').html(opt);
	
    $('#findproduct #value1').live('change', function () {
        var hamidval = $(this).val();
        if (hamidval != '' && hamidval != null) {
            var curarray = eval(hamidval + '_searchform');
            var opt = '';
			$.each(curarray, function(a,b){
				opt += '<option value="'+a+'">' + b + '</option>';
			});
            /*for (var i = 0; i < curarray.length; i++) {
                opt += '<option>' + curarray[i] + '</option>';
            }*/
        } else {
            var opt = '';
        }
        $('#findproduct #value2').html(opt);
    });

    $('#findproduct').submit(function () {
        var pagename = $('#findproduct #value2').val();
        if (pagename != '' && pagename != null) {
            //pagename = $.trim(pagename);
            //pagename = pagename.replace(/ /g, '_');
            //pagename = pagename.toLowerCase();
            //pagename = 'product-' + pagename + '.php';
            window.location = pagename;
        } else {

        }
        return false;
    });



    var curcat = 'edm_wire';
    $('.products ul li').live('click', function () {
        $('.prod ul li').removeClass('selected');
        var prodname = $(this).text();
        $('.curproduct').text(prodname);
        $('.curproduct').append('<span class="darrow">');
        prodname = $.trim(prodname);
        prodname = prodname.replace(/ /g, '_');
        //prodname = prodname.toLowerCase();
        curcat = prodname;
        $('.prod').hide(0);
        $('.' + prodname).fadeIn();
    });


	/*$('.chproduct').hover(function(){
		$('.products').stop(true, true).slideDown('fast');
	}, function(){
		$('.products').stop(true, true).slideUp('fast');
	});*/
    $(".chproduct").live({
        mouseleave: function (e) {
            $('.products').stop(true, true).slideUp('fast');
        },
        mouseenter: function (e) {
            $('.products').stop(true, true).slideDown('fast');
        }
    });

    $('body').append('<div style="position:absolute; display:none; padding:5px 10px; margin-top:-5px; margin-left:0px;" class="popup">PopUp</div>');
	/*$("[title]").live({
		mouseleave:function(e){
			$(this).attr('title',$('.popup').text());
			$('.popup').stop(true, true).fadeOut();
		},
		mouseenter:function(e){
			var thttle = $(this);
			$('.popup').css({
				top:e.pageY-30,
				left:e.pageX-26
			});
			$(this).mousemove(function(e){
				$('.popup').css({
					top:e.pageY-30,
					left:e.pageX-26
				});
			});
			$('.popup').text($(this).attr('title'));
			$('.popup').stop(true, true).fadeIn();
			$(this).attr('title',' ');
		}
	});*/


    $('div#fixcol div, .comparetable tr').live({
        mouseenter: function () {
            var thidx = $(this).index() + 1;
            $('div#fixcol div:nth-child(' + thidx + '), .comparetable tr:nth-child(' + thidx + ')').addClass('hamid');
        },
        mouseleave: function () {
            $('div#fixcol div, .comparetable tr').removeClass('hamid');
        }
    });
    $('div#fixcol div, .comparetable tr').live('click', function (e) {
        var thidx = $(this).index() + 1;
        if (!e.ctrlKey) $('div#fixcol div, .comparetable tr').removeClass('clicked');
        $('div#fixcol div:nth-child(' + thidx + '), .comparetable tr:nth-child(' + thidx + ')').addClass('clicked');
        return false;
    });


    function displaycomparetable() {
        //alert(curcat);
        var headercolar = eval(curcat + '_headercol');
        var comparearray = [];
        //comparearray.push(edm_wire_headercol);
        $('.prod ul li').each(function () {
            if ($(this).hasClass('selected')) {
                comparearray.push(eval($(this).attr('id')));
            }
        });
        comparearray = comparearray.transpose();
        var tablestr = '<table class="comparetable">';
        for (var i = 0; i < comparearray.length; i++) {
            tablestr += '<tr>';
            for (var j = 0; j < comparearray[i].length; j++) {
                tablestr += '<td>' + comparearray[i][j] + '</td>';
            }
            tablestr += '</tr>';
        }
        tablestr += '</table>';
        $('#comparetable').html(tablestr);
        var fixedcol = '';
        var count = 0;
        $('.comparetable tr td:first-child').each(function () {
            var dht = $(this).outerHeight() - 11;
            var dwd = 130;
            var newtxt = headercolar[count];
            fixedcol += '<div style="height:' + dht + 'px; width:atuo; padding:5px 10px; border:1px solid #CCCCCC;border-bottom:none;border-right:none; overflow:hidden;" title="' + newtxt + '">' + newtxt + '</div>';
            count++;
        });
     	
    }
    displaycomparetable();
    $('.prod ul li').live('click', function () {
        var removingclass = 0;
        if ($(this).hasClass('selected')) {
            $(this).removeClass('selected');
            removingclass = 1;
        } else {
            $(this).addClass('selected');
            removingclass = 0;
        }
        if ($('.prod ul li.selected').length <= '1' && removingclass == 1) {
            //displaycomparetable();
            $(this).addClass('selected');
            $('#errorprod').text('Alteast 2 products must remain selected.');
        } else {
            displaycomparetable();
            $('#errorprod').text('');
        }
    });

    $('body').append('<div class="compareloaddiv" />');
    $('.compareloaddiv').append('<div class="close" />');
    $('.compareloaddiv .close').live('click', function () {
        $('.compareloaddiv').html('').fadeOut('slow');
        $('#wrap:hidden').fadeIn('fast');
    });
    $('.comparebut').click(function () {
        var url = '';
        if ($(this).hasClass('edm_sinker_prod')) {
            url = 'compare.php?prod=edm_sinker';
        } else {
            url = 'compare.php';
        }
        $('.compareloaddiv').fadeIn(function () {
            $('.compareloaddiv').load(url + ' #content', function () {
                displaycomparetable();
                $('#wrap:visible').fadeOut('fast');
                $('.compareloaddiv').append('<div class="close" />');
            });
        });
    });

    $('.enqbutton').click(function () {
        location.href = 'enquiry.php';
    });

    $('.enquire').css('float', 'right').wrap('<div style="overflow:auto; margin-bottom:20px;" />');
    $('#Map area').click(function () {
        var addressurl = $(this).attr('href');
        $('.addressovl .cityadd').load(addressurl, function () {
            $('.addressovl').fadeIn();
        });
        return false;
    });
    $('.addcolose').click(function () {
        $('.addressovl').fadeOut();
    });
    
	$(".datemask").mask("99/99/9999");
    $(".mobilemask").mask("(99) 99999-99999");
    $('.landlinemask').mask('(99) (9999) 9999-9999');
    $('.extensionmask').mask("9999");
    $('.zipcodemask').mask("999999");
	
});
$(document).ready(function () {
    $('#slider').nivoSlider({
        directionNav: false,
        captionOpacity: 1.0,
        pauseTime: 8000,
        animSpeed: 500,
        afterChange: function () {
            $('.nivo-caption > p p').hide();
            $('.nivo-caption > p p:first-child').fadeIn().delay(3500).fadeOut(function () {
                $('.nivo-caption > p p:nth-child(2)').fadeIn();
            });
        },
        afterLoad: function () {
            $('.nivo-caption > p p').hide();
            $('.nivo-caption > p p:first-child').fadeIn().delay(3500).fadeOut(function () {
                $('.nivo-caption > p p:nth-child(2)').fadeIn();
            });
        },
        beforeChange: function () {
            $('.nivo-caption').animate({
                width: '100%'
            }, 1500, '', function () {
                $(this).animate({
                    width: '30%'
                }, 500);
            });
        }
    });
    $("a.prettyphoto").prettyPhoto({
        social_tools: false
    });
    //$.prettyPhoto();
    /*form*/
    $('form').not('#searchform').HVal({
        errorClass: 'error',
        errorMessage: 'Send Error, Pls send a direct mail to <a href="mailto:krish@intiti.com">krish@intiti.com</a>',
        successMessage: 'Thank You, We&acute;ll try to reach asap.',
        displayOutput: false
    });

});
