var newsletterInputSlide = null;
var newsletterOutputSlide = null;

function processForm(f, title, description)
{
    if(title != undefined && description != undefined) {
        Roar.alert(title, description);
    }
    var button = f.elements['submit'];
    button.value = "Processing...";
    button.disabled=true;
    return true;
}

function displayCoupon(couponId, promotionId)
{
    url = '/specials';

    if(couponId) {
        url += '?couponId=' + couponId;
    }

    if(promotionId) {
        url += '&promotionId=' + promotionId;
    }

    var movie = getFlashMovie("CouponSheet");

    if(movie == undefined) {
        url += '&noFlash=true';
    } else {
        try {
            movie.isReady();
        } catch(err) {
            url += '&noFlash=true';
        }
    }
    
    url += "&format=html";

    track_urchin(couponId,'/specials/view/');

    SqueezeBox.open(url,{
        size: {
            x: 370,
            y: 550
        }
    });
}

function displayNotice()
{
}

function track_urchin(tag, type)
{
    var str;
    
    if(!type) {
        type = '/outgoinglink/';
    }

    if(tag.href) {
        if(tag.href.indexOf(location.host) == -1)
        {
            var url = tag.href.replace('http://', '').replace('/', '');
            str = '/outgoinglink/' + url;

            try
            {
                pageTracker._trackPageview(str);
            }
            catch(err)
            {
                //alert("Failed");
            }
        }
    } else {
        str = type + tag;
        try
        {
            pageTracker._trackPageview(str);
        }
        catch(err)
        {
            //alert("Failed");
        }
    }
}

function rememberLocation()
{
//setCookie('location', locationTitlesCarousel.atScreen );
}

function setCookie(c_name, value)
{
/*
var expiredays = 365;
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
*/
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

/*  ----------------------| FLASH INTEGRATION |----------------------  */	

function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}
 
function printPromotion(couponId) {
    track_urchin(couponId,'/specials/print/');
    try {
        var movie = getFlashMovie("CouponSheet");
        movie.printPromotion(couponId);
    } catch(err) {
        //alert(err);
    }
}

/*  ----------------------| NEWSLETTERSUBSCRIPTION INLINE |----------------------  */	

window.addEvent("domready", function() {
    if($('newsletterform') == null ) return;

    newsletterInputSlide = new Fx.Slide('newsletter-input');
    newsletterOutputSlide = new Fx.Slide('newsletter-output');

    $('newsletterform').addEvent('submit', function(e) {
        /**
			 * Prevent the submit event
			 */
        new Event(e).stop();


			
			
        /**
			 * This empties the log and shows the spinning indicator
			 */
        var log = $('newsletter-output').empty().addClass('ajax-loading');
        newsletterOutputSlide.show();
        newsletterInputSlide.hide();
					 
        /**
			 * send takes care of encoding and returns the Ajax instance.
			 * onComplete removes the spinner from the log.
			 */
        this.send({
            update: log,
            onComplete: function() {
                newsletterOutputSlide.show();
                log.removeClass('ajax-loading');
            }
        });
    });
});

function showNewsletterInput()
{
    newsletterInputSlide.show();
    newsletterOutputSlide.hide();
}

/*  ----------------------| BLABLAH |----------------------  */	

window.addEvent('domready', function () {

    $$(".fade").forEach(function(element){
        element.onmouseover =  function(){
            element.firstChild.firstChild.src = element.firstChild.firstChild.src.replace(".jpg","_f2.jpg");
        }
			
        element.onmouseout =  function(){
            element.firstChild.firstChild.src = element.firstChild.firstChild.src.replace("_f2.jpg",".jpg");
        }

    });

});

var locationTitlesCarousel = null;
	
window.addEvent("domready", function() {
    if($('LocationDetailsCarousel') == null ) return;
		
    locationTitlesCarousel = new iCarousel("LocationTitlesCarouselContent", {
        idPrevious: "btn_prev_location",
        idNext: "btn_next_location",
        idToggle: "undefined",
        onNext: function() {
            rememberLocation();
        },
        onPrevious: function() {
            rememberLocation();
        },
        onGoTo: function() {
            rememberLocation();
        },
        item: {
            klass: "LocationTitlesCarouselItem",
            size: 150
        },
        animation: {
            type: "scroll",
            duration: 600,
            amount: 1
        }
			
    });
		
    var loc=getCookie('location');
    if (loc!=null && loc!="") locationTitlesCarousel.goTo(loc);
		
    var fade = new Fx.Style('LocationTitlesCarouselContent', 'opacity',{
        duration:800
    });
    fade.start(0,1);
});
	
var locationDetailsCarousel = null;
	
window.addEvent("domready", function() {
    if($('LocationDetailsCarousel') == null ) return;
			
    locationDetailsCarousel = new iCarousel("LocationDetailsCarouselContent", {
        idPrevious: "btn_prev_location",
        idNext: "btn_next_location",
        idToggle: "undefined",
        item: {
            klass: "LocationDetailsCarouselItem",
            size: 800
        },
        animation: {
            type: "scroll",
            duration: 600,
            amount: 1
        }
			
    });
		
    var fade = new Fx.Style('LocationDetailsCarouselContent', 'opacity',{
        duration:800
    });
    fade.start(0,1);
		
    loc=getCookie('location');
    if (loc!=null && loc!="") locationDetailsCarousel.goTo(loc);
		
});

/* NEWSLETTER SUBSCRIPTION AJAX FORM */

window.addEvent("domready", function() {
    if($('myForm') == null ) return;

    $('myForm').addEvent('submit', function(e) {
        /**
			 * Prevent the submit event
			 */
        new Event(e).stop();
			
        /**
			 * This empties the log and shows the spinning indicator
			 */
        var log = $('log_res').empty().addClass('ajax-loading');
		 
        /**
			 * send takes care of encoding and returns the Ajax instance.
			 * onComplete removes the spinner from the log.
			 */
        this.send({
            update: log,
            onComplete: function() {
                log.removeClass('ajax-loading');
                $('myForm').empty();
            }
        });
    });
});

window.addEvent("!domready", function() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(39.480991, -87.271614), 11);
			
        var point = new GLatLng(39.414208,-87.406805);
        var marker = new GMarker(point,{
            title:'yo'
        });
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml('<p>test</p>');
        })
        map.addOverlay(marker);
			
        point = new GLatLng(39.44201,-87.413836);
        map.addOverlay(new GMarker(point));

        point = new GLatLng(39.492235,-87.360165);
        map.addOverlay(new GMarker(point));

        point = new GLatLng(39.526636,-87.112334);
        map.addOverlay(new GMarker(point));
			

    }
});
