var explorer = navigator.userAgent.indexOf("MSIE") != -1;
var explorer6 = navigator.userAgent.indexOf("MSIE 6.0") != -1;
var explorer7 = navigator.userAgent.indexOf("MSIE 7.0") != -1;
var explorer8 = navigator.userAgent.indexOf("MSIE 8.0") != -1;
var firefox = navigator.userAgent.indexOf("Firefox") != -1;
var safari = navigator.userAgent.indexOf("Safari") != -1;
var opera = navigator.userAgent.indexOf("Opera") != -1;
var chrome = navigator.userAgent.indexOf("Chrome") != -1;

$(function(){
	$('body').removeClass('noJs');
	$('.lightbox .overlay').css({'width':$(window).width(),'height':$('body').height()});

    // Show & Hide the submenu under the main nav
	$('.nav li').hover(function(){			 
		$(this).find('ul.subMenu').removeClass('accessAlt');
	},function(){
		$(this).find('ul.subMenu').addClass('accessAlt');
	});
    // END

    // Content tabs
	$('ul.tabs li').each(function(){
		if($(this).hasClass('active')){
		    $(this).prev().addClass('prev');
		    $(this).next().addClass('next');
		    var id = $(this).find('a').attr('href');
			$(this).parents('.box').find('.content .'+id).removeClass('accessAlt').addClass('active');
		}
	});
	$('ul.tabs li a').click(function(){
	    if($(this).parent('li').hasClass('external')){
	        return true;
	    } else {
		    $(this).parents('ul.tabs').find('li').removeClass('active next prev');
		    $(this).parent('li').addClass('active');
		    $(this).parents('ul.tabs').find('li.active').prev().addClass('prev').removeClass('active next');
		    $(this).parents('ul.tabs').find('li.active').next().addClass('next').removeClass('active prev');
		    $(this).parents('.box').find('.content .active').removeClass('active').addClass('accessAlt');
		    $(this).parents('.box').find('.content .'+$(this).attr('href')).removeClass('accessAlt').addClass('active');
		    return false;
		}
	});
	// END
	//tabs calculator
	$('ul.tabs_calc li').each(function(){
		if($(this).hasClass('active')){
		    $(this).prev().addClass('prev');
		    $(this).next().addClass('next');
		    var id = $(this).find('a').attr('href');
			$(this).parents('.box').find('.content .'+id).removeClass('hide').addClass('active');
		}
	});
	$('ul.tabs_calc li a').click(function(){
	    if($(this).parent('li').hasClass('external')){
	        return true;
	    } else {
		    $(this).parents('ul.tabs_calc').find('li').removeClass('active next prev');
		    $(this).parent('li').addClass('active');
		    $(this).parents('ul.tabs_calc').find('li.active').prev().addClass('prev').removeClass('active next');
		    $(this).parents('ul.tabs_calc').find('li.active').next().addClass('next').removeClass('active prev');
		    $(this).parents('.box').find('.content .active').removeClass('active').addClass('hide');
		    $(this).parents('.box').find('.content .'+$(this).attr('href')).removeClass('hide').addClass('active');
		    return false;
		}
	});
	//

    

    $('.projectLibrary .item h4 span').each(function(){
        var p = $(this).parents('h4').height();
        var h = $(this).height();
        if(h > 15){
            $(this).parents('h4').css({'height':'30px','padding-top':'5px'});
        }
    });

	$('.lowesServices .item').hover(function(){			 
		$(this).find('.expand').removeClass('accessAlt');
	},function(){
		$(this).find('.expand').addClass('accessAlt');
	});
	$('.lowesforpros .library .item').hover(function(){			 
		$(this).find('.expand').removeClass('accessAlt');
	},function(){
		$(this).find('.expand').addClass('accessAlt');
	});

    $('.locator a.showDetails').click(function(){
        $(this).parents('.item').toggleClass('opened');
        $(this).toggleClass('showDetails').toggleClass('hideDetails');
        return false;
    });

    // HowTo Videos
    $('.videoLibrary .content .list .item').click(function(){
        var i = $(this).attr('id');
        var h = $(this).find('h3').text();
        var t = $(this).find('p.hide').text();
        $('.currentVideo .video').html(returnFlash('video','../flash/'+i+'.swf','400','338',''));
        $('.currentVideo .infos h3').html(h);
        $('.currentVideo .infos p').html(t);
        $('.currentVideo .infos .instructions').find('a').attr('href','/pdf/'+i+'.pdf');
    });
    // END

    // Products Carousel
    $('.products .carousel ul').each(function(){
        $(this).css({'width':($(this).find('li').size()*159)});
    });
    $('.products .carousel ul li img').each(function(){
        var h = Math.ceil((155-$(this).height())/2);
        $(this).css({'padding-top':h});
    });
    $('.products .carousel .arrows a').click(function(){
        var el = $('.products .carousel ul');
        var max = eval('-'+((el.find('li').size()-4)*159));
        var min = 0;
        var p = el.position();
        var l = p.left;
        var c = eval($('.products .carousel .arrows .count').text());
        if ($(this).attr('href') == '#next'){
            if(l > (max+1)){
                var q = c + 1;
                el.animate({left:'-'+(q*636)},{duration:400});
                $('.products .carousel .arrows .count').text(q);
            }
        }else{
            if(l < 0){
                var q = c - 1;
                el.animate({left:'-'+(q*636)},{duration:400});
                $('.products .carousel .arrows .count').text(q);
            }
        }
    });
    // END

    // Code for Custom Dropdowns
	$('.drp').each(function(){
	    var id = $(this).attr('id');
	    var css = $(this).attr('class');
	    $(this).addClass('accessAlt');
	    $(this).before('<div class="drpCustom '+css+'"><div class="current">'+$('#'+id +' option:selected').text()+'</div><ul id="new_'+id+'" class="drpList accessAlt"></ul></div>');
	    $(this).find('option').each(function(){
	        if($(this).text() == $(this).parents('select').find('option:selected').text()){
	            $('#new_'+id).append('<li class="active"><a href="" option="'+$(this).val()+'" title="'+$(this).text()+'" rel="'+$(this).parents("select").attr("id")+'">'+$(this).text()+'</a></li>');
	        }else{
	            $('#new_'+id).append('<li><a href="" option="'+$(this).val()+'" title="'+$(this).text()+'" rel="'+$(this).parents("select").attr("id")+'">'+$(this).text()+'</a></li>');
	        }
	    });
	});
	$('.drpCustom .current').click(function(){
        $(this).parents('.drpCustom').addClass('drpOpened').find('ul.accessAlt').removeClass('accessAlt');
    });
    $('.drpCustom').hover(function(){			 
		// nothing to do on hover, triggered on click
	},function(){
		$(this).removeClass('drpOpened').find('ul').addClass('accessAlt');
	});
	$('.drpList li a').live('click', function(){
	    $('#'+$(this).attr("rel")).val($(this).attr("option")).change();
	    $(this).parents('ul').find('li.active').removeClass('active');
	    $(this).parent('li').addClass('active').change();
	    $(this).parents('.drpCustom').find('.current').text($(this).text());
	    $(this).parents('.drpList').addClass('accessAlt');
	    return false;
	});
	// END

	//Code for the dropdownlist event of the small selector
	var drpProvince = $("#drpProvince");
	if(drpProvince.length){
	    drpProvince.change(function(){ 
            StoreWidget.province_selected(drpProvince.val());
        }); 
    }
    
    var drpProvinceOverlay = $("#drpProvinceOverlay");
	if(drpProvinceOverlay.length){
	    drpProvinceOverlay.change(function(){ 
            StoreWidget.overlay_province_selected(drpProvinceOverlay.val());
        }); 
    }
	//END
	
	//Watermark handler
	$('.watermarked').each(function() {
        $(this).watermark('watermark', $(this).attr('title'));
    });
	//end
});

function returnFlash(id,path,width,height,flashvars,description){
	var myFlash = '<object id="'+id+'" width="'+width+'" height="'+height+'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">';
		myFlash = myFlash +'<![CDATA[<p>'+description+'</p>]]>';
		myFlash = myFlash +'<param name="allowScriptAccess" value="always" />';
		myFlash = myFlash +'<param name="movie" value="'+path+'" />';
		myFlash = myFlash +'<param name="menu" value="false" />';
		myFlash = myFlash +'<param name="allowFullScreen" value="true" />';
		myFlash = myFlash +'<param name="flashvars" value="'+flashvars+'">';
		myFlash = myFlash +'<param name="quality" value="high" />';
		myFlash = myFlash +'<param name="bgcolor" value="#ffffff" />';
		myFlash = myFlash +'<param name="wmode" value="transparent" />';
		myFlash = myFlash +'<embed name="'+id+'" width="'+width+'" height="'+height+'" src="'+path+'" flashvars="'+flashvars+'" wmode="transparent" menu="false" quality="high" allowFullScreen="true" bgcolor="#ffffff" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	return myFlash;
}

String.format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
    }
    return text;
};

function alertPop(url,message,w,h,pageView) {
	var prompt = confirm(message)
	if (prompt){
	    pageTracker._trackPageview(pageView);
    	popPage(url,w,h);
	}
}

function popPage(page,w,h) {
 eval('window.open(page, "popname", "height='+h+',width='+w+',location=no,scrollbars=yes,menubars=yes,toolbars=no,resizable=yes");');
}

//Store Widget Namespace
var StoreWidget = new function() {
    var province_code = "";
    function LoadCity(drpCityId) {
        var webMethod = '/_ws/StoreLocator.asmx/GetCityListByProvinceCode'
        var parameters = String.format("{'provinceCode':'{0}'}", province_code);
        $.ajax({
            type: "POST",
            url: webMethod,
            data: parameters,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                ClearCityDropdown(drpCityId);
                for (var i in msg.d) {
                    $("#" + drpCityId).append(String.format("<option value='{0}'>{1}</option>", msg.d[i].PostalCode, msg.d[i].Name));
                }
                updateList(drpCityId);
            }
        });
    }
    function ClearCityDropdown(drpCityId) {
        $("#" + drpCityId).html("");
        //Add default items
        $("#" + drpCityId).append("<option value='0'>City</option>");
        updateList(drpCityId);
    }
    function ProvinceSelected(provinceCode, drpCityId) {
        if (provinceCode != "0") {
            province_code = provinceCode;
            LoadCity(drpCityId);
        } else {
            ClearCityDropdown(drpCityId);
        }
        //setTimeout("updateList('"+drpCityId+"');",500);
    }
    return {
        province_selected: function(provinceCode) {
            ProvinceSelected(provinceCode, "drpCity");
        },
        overlay_province_selected: function(provinceCode) {
            ProvinceSelected(provinceCode, "drpCityOverlay");
        },
        ShowOverlay: function(redirectTo) {
            $("#divStoreLocator").fadeIn(400);
            $("#hidRedirect").val(redirectTo);
        },
        HideOverlay: function() {
            $("#divStoreLocator").fadeOut(400);
        }
    };
}

function doSearch(){
    document.location.href="/products/search.aspx?querytext=" + escape($("#txtSearch").val()); 
}

function updateList(id){
    $('#new_'+id).html('');
    $('#'+id).find('option').each(function(){
        if($(this).text() == $(this).parents('select').find('option:selected').text()){
            $('#new_'+id).prev('.current').html($(this).text());
            $('#new_'+id).append('<li class="active"><a href="" option="'+$(this).val()+'"title="'+$(this).text()+'" rel="'+$(this).parents("select").attr("id")+'">'+$(this).text()+'</a></li>');
        }else{
            $('#new_'+id).append('<li><a href="" option="'+$(this).val()+'"title="'+$(this).text()+'" rel="'+$(this).parents("select").attr("id")+'">'+$(this).text()+'</a></li>');
        }
    });
}
