//This hides the inline content if JavaScript is supported.
document.write("<style type='text/css'>.hidden{display:none;}<\/style>");

$(document).ready(function() {
	$.ajax({
		type: "GET",
		url: "/media/xml/galleryNav.xml",
		dataType: "xml",
		success: parseImagesXML
	});
	
	$.ajax({
		type: "GET",
		url: "/media/xml/galleryNav.xml",
		dataType: "xml",
		success: parseVideoXML
	});

						   
    //Examples of Global Changes
    $.fn.colorbox.settings.bgOpacity = "0.8";
    $('.checkBalance').click(function() {
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var phoneReg = /[\(\)\.\-\ ]/g;
        var rasins = false;

//        if ($('#ctl00_PageContent_txtPhone').val().length < 1) {
//        if (reg.test($('#ctl00_PageContent_txtEmail').val()) == false) {
//            rasins = true;
//            $('#ctl00_PageContent_txtEmail').css('background-color', '#efb3b5');
//        } else {
//            $('#ctl00_PageContent_txtEmail').css('background-color', '#FFF');
//            
//        }
//        }
////        if ($('#ctl00_PageContent_txtEmail').val().length < 1) {
//            if ($('#ctl00_PageContent_txtPhone').val().length < 10) {
//                rasins = true;
//                $('#ctl00_PageContent_txtPhone').css('border-color', '#efb3b5');
//            } else {
//                $('#ctl00_PageContent_txtPhone').css('border-color', '#FFF');
//            }
//        }

        if ($('#ctl00_PageContent_txtCardNum').val().length < 1) {
            rasins = true;
            $('#ctl00_PageContent_txtCardNum').css('background', '#efb3b5');
        } else {
            $('#ctl00_PageContent_txtCardNum').css('background', '#FFF');
        }

//        var phoneNumber = $('#ctl00_PageContent_txtPhone').val().replace(/[\(\)\.\-\ ]/g, '');
//        var myURL;
//        if (phoneNumber.length < 1) {
        myURL = "http://www.dealerrewards.net/dealerrewards/drnet?category=main&action=customerreport&dealerid=" + $('#ctl00_PageContent_hidDealerID').val() + "&type=html&email=" + escape($('#ctl00_PageContent_txtEmail').val()) + "&barcode=" + $('#ctl00_PageContent_txtCardNum').val() + "&show=Show+Report";
//        } else {
//            myURL = "http://www.dealerrewards.net/dealerrewards/drnet?category=main&action=customerreport&dealerid=" + $('#ctl00_PageContent_hidDealerID').val() + "&type=html&email=" + escape($('#ctl00_PageContent_txtEmail').val()) + "&phonenumber=" + phoneNumber + "&barcode=" + $('#ctl00_PageContent_txtCardNum').val() + "&show=Show+Report";
//        }
        if (rasins == false) {
            $.fn.colorbox({ width: '650', height: '600', iframe: true, href: myURL, open: true });
        }
    });
});

function loadVideoXML(){
}

function parseImagesXML(xml){
	$(xml).find('imageGalleries').each(function(){
		$(this).find('gal').each(function(){
			var fileName = "'"+$(this).attr("file")+"'";
			$('#images').append('<dd><a href="javascript:void(0)" onclick="loadSWF('+fileName+')">'+$(this).attr("title")+'</a></dd>');
		});
	});
	//$('#images').html(myHTML);
}

function parseVideoXML(xml){
	$(xml).find('videoGalleries').each(function(){
		$(this).find('gal').each(function(){
			var fileName = "'"+$(this).attr("file")+"'";
			$('#videos').append('<dd><a href="javascript:void(0)" onclick="loadSWF('+fileName+')">'+$(this).attr("title")+'</a></dd>');
		});
	});
}

function openModal() {
    $('#myFlashPopup').colorbox({ fixedWidth: '576', fixedHeight: '464', inline: true, open: true, href: '#myFlashPopup' });
}

function showColorbox(myURL) {
    $.fn.colorbox({width: '600', height: '500', iframe:true, href:myURL , open: true});
}

function showInline(div, myWidth, myHeight) {
    $.fn.colorbox({width: myWidth, height: myHeight, inline:true, href:div, open: true});
}

function openInIframe(myLink, myWidth, myHeight) {
    $.fn.colorbox({width: myWidth, height: myHeight, iframe:true, href:myLink, open: true});
}


//===================================================================
//image swap handler
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//===================================================================

function getTodaysDate() {
	var d = new Date();
	return ((d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear());
}

//preload handler
preloadArr= new Array();
preloadArr[0] = "images/nHome_over.gif";
preloadArr[0] = "images/nShop_over.gif";
preloadArr[0] = "images/nDrive_over.gif";
preloadArr[0] = "images/nSavings_over.gif";
preloadArr[0] = "images/nFinance_over.gif";
preloadArr[0] = "images/nWhy_over.gif";
preloadArr[0] = "images/nQuick_over.gif";
preloadArr[0] = "images/quickVisit_ovr.jpg";
preloadArr[0] = "images/quickHelp_ovr.jpg";
preloadArr[0] = "images/quickQuote_ovr.jpg";

function preload()
{
  if(document.images)
  {
    var i,j = preloadArr.length, args = preload.arguments;
    
    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.preloadArr[j] = new Image;
        document.preloadArr[j++].src = args[i];
      }
    }
  }
}

preload();

$(document).ready(function() {
    $("#mapContainer a").click(function() {
        var thisID = $(this).attr("id");
        var thisPage = $(this).attr("rev");
        $("#" + thisID).colorbox({ width: "600", height: "550", iframe: true, href: thisPage, open: true });
    });

    $(".vBubble").hoverIntent(function() {
        var pos = $(this).position();
        var posLeft = pos.left;
        var posTop = pos.top;
        var thisWidth = $(this).width();
        var popupDiv = $(this).attr('accesskey');
        var popupTop;
        var inHTML = $('#' + popupDiv).html();
        var topHTML;
        var botHTML;
        topHTML = "<div class='vBubbleContainer'><div class='vBubbleTop'></div><div class='vBubbleMiddle'><div class='vBubbleContent'>";
        botHTML = "</div></div><div class='vBubbleBottom'></div></div>";
       
        $('#' + popupDiv).html(topHTML + inHTML + botHTML);
		
        var popupHeight = $('#' + popupDiv).innerHeight();
		popupHeight = popupHeight + 10 + 14;
		popupTop = posTop - (popupHeight / 2) + 10;
		if (popupTop + popupHeight >= $(window).scrollTop() + $(window).height()) { //if off of the bottom of the screen
			popupTop = ($(window).scrollTop() + $(window).height()) - (popupHeight + 20);
		}
		if (popupTop <= $(window).scrollTop()) { //if off of the top of the screen
			popupTop = $(window).scrollTop() + 10;
		}
		
		$('#' + popupDiv).css('top', popupTop + 'px');
        $('#' + popupDiv).css('left', posLeft + thisWidth + 18 + 'px');
		
        $('#' + popupDiv).fadeIn('normal');
    },
        function() {
            var popupDiv = $(this).attr('accesskey');
            var inHTML = $('#' + popupDiv + ' .vBubbleContent').html();
            $('#' + popupDiv).html(inHTML);
            $('#' + popupDiv).css('display', 'none');
        }
    );
});

