var pageid = 0;

//GetQueryString('nav');
function GetQueryString(key){
    var url = location.href,
    key = key.toLowerCase();
    val = "";
    var qString = "";
    if (url.indexOf("?") != -1){
        qString = url.split("?")[1];
    }
    
    if (qString != "" && key != ""){
        var arrQStringKeyValue = (qString.indexOf("&") != 1) ?  qString.split("&") : qString;
        if (arrQStringKeyValue.length > 1){
            for (i = 0; i < arrQStringKeyValue.length; i++){
                var temp = arrQStringKeyValue[i].toLowerCase();
                if (temp.indexOf(key) != -1){
                    if (arrQStringKeyValue[i].indexOf("=") != -1){
                        arrKeyVal = arrQStringKeyValue[i].split("=");
                        temp = arrKeyVal[0].toLowerCase();
                        val = (temp==key) ? arrKeyVal[1] : "";
                    }
                }
            }
        }else{            
            var temp = arrQStringKeyValue[0].toLowerCase();
            if (temp.indexOf(key) != -1){
                if (arrQStringKeyValue[0].indexOf("=") != -1){
                    arrKeyVal = arrQStringKeyValue[0].split("=");
                    temp = arrKeyVal[0].toLowerCase();
                    val = (temp==key) ? arrKeyVal[1] : "";
                }
            }
        }
    }
   
    // Remove anything after #
    val = (val.indexOf("#") != -1) ? val.split("#")[0] : val;
    return val;
}

$(document).ready(function () {
    $('#MainNav').addClass('NavActive');
    //Nav Menu Function
    $('.NavActive li').squeakyNavMenu();
    $('.NavActive li:first').addClass('NoBorder');
    $('#MainNav li a:first').css({ marginLeft: '0' });
    $('#MainNav li a:last').css({ marginRight: '0' });

    //Add This Drop Down
    $('.custom_button, .hover_menu').mouseenter(function () {
        $('.hover_menu').fadeIn('fast');
        $('.custom_button').addClass('active');
        $(this).data('in', true);
        $('.hover_menu').data('hidden', false);
    }).mouseleave(function () {
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });

    var delay = 400;
    function hideMenu() {
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden')) {
            $('.hover_menu').fadeOut('fast');
            $('.custom_button').removeClass('active');
            $('.hover_menu').data('hidden', true);
        }
    }

    //No Border on last Li for Add this Drop down menu
    $('.hover_menu ul li:last-child').addClass('NoBorder');

    //Side Bar Matching
    var strBodyClass = $("body").attr('class'),
    sideBarList = $('#LuxuryCallout li h5'),
    multiple = 0,
    arrClass = '',
    found = false,
    strBodyClass = $.trim(strBodyClass);
    if (strBodyClass.indexOf(" ") != -1) {
        multiple++;
        arrBodyClass = strBodyClass.split(" ");
    }

    if (strBodyClass != '') {
        if (multiple == 0) {
            for (j = 0; j < sideBarList.length; j++) {
                if ($(sideBarList[j]).attr('id') != ('Title' + strBodyClass)) {
                    $(sideBarList[j]).parent().css({ display: 'block' });
                }
            }
        } else {
            for (i = 0; i < arrBodyClass.length; i++) {
                for (j = 0; j < sideBarList.length; j++) {
                    if ($(sideBarList[j]).attr('id') != ('Title' + arrBodyClass[i])) {
                        $(sideBarList[j]).parent().css({ display: 'block' });
                    } else {
                        found = true;
                    }
                }
                if (found) { break; }
            }
        }
    }

    //Feature Photos Hover effect Hover function for "A" tag
    $('#FeaturePhotos li a.TransparentImg').css({ opacity: 0.7 });
    $('#FeaturePhotos li a.TransparentImg').hover(function () {
        $(this).stop().animate({ opacity: 1 }, '600');
    },
        function () {
            $(this).stop().animate({ opacity: 0.7 }, '400');
        });

    //Feature Photos Hover effect Hover function for nested "div" tag	
    $('#FeaturePhotos li div').hover(function () {
        $(this).prev('a.TransparentImg').stop().animate({ opacity: 1 }, '600');
    },
        function () {
            $(this).prev('a.TransparentImg').stop().animate({ opacity: 0.7 }, '400');
        });
    
    //Pop Up Window for Photos
    if ($('a.PopUp').length > 0) {
        $('a.PopUp').click(function () {
            CallSimpleModalPhotos($.trim($(this).attr('rel')));
        });
    }

    function CallSimpleModalPhotos(src) {
        $.modal('<iframe src="/photo-albums/' + src + '" height="520" width="720" style="border:0px;" frameborder="0" scrolling="no">', {
            closeHTML: "<a href='#'></a>",
            containerCss: {
                height: 520,
                padding: 0,
                width: 720
            }
        });
    }

    //Pop Up Window for Videos
    if ($('a.PopUpVideo').length > 0) {
        $('a.PopUpVideo').click(function () {
            CallSimpleModalVideo($.trim($(this).attr('rel')));
        });
    }

    function CallSimpleModalVideo(src) {
        $.modal('<iframe src="/video-albums/' + src + '" height="505" width="690" style="border:0px;" frameborder="0" scrolling="no">', {
            closeHTML: "<a href='#'></a>",
            containerCss: {
                height: 505,
                padding: 0,
                width: 690
            }
        });
    }

    //Pop Up for Music File
    if ($('a.PopUpMusic').length > 0) {
        $('a.PopUpMusic').click(function () {
            CallSimpleModalMusic($.trim($(this).attr('rel')));
        });
    }

    function CallSimpleModalMusic(src) {
        $.modal('<iframe src="' + src + '" height="380" width="500" style="border:0px;" frameborder="0" scrolling="no">', {
            closeHTML: "<a href='#'></a>",
            containerCss: {
                backgroundColor: '#292932',
                height: 400,
                paddingTop: 25,
                width: 520
            }
        });
    }

    //Leaving Site Message
   $(function ($) {
        $('a.LeavingSite').click(function (e) {
            e.preventDefault();
            fireTag(2247.1, {'<nav_method>':'Other'});            
            var urlToRedirect = $(this).attr('href');
            confirm("<img src='/images/Title-Your-are-leaving-Luxuryawaits.com.gif' width='314' height='19' alt='You are now leaving Luxur Awaits' />", function () {
                window.open(urlToRedirect);
            });
        });
    });

    function confirm(message, callback) {
        $('#confirm').modal({
            closeHTML: "<a href='#' title='Close'></a>",
            position: ["20%", ],
            overlayId: 'confirm-overlay',
            containerId: 'confirm-container',
            onShow: function (dialog) {
                $('.message', dialog.data[0]).append(message);
                $('.yes', dialog.data[0]).click(function () {
                    if ($.isFunction(callback)) {
                        callback.apply();
                    }
                    $.modal.close();
                });
            }
        });
    }

    //Gallery Slide Show Photos
    if ($('#PhotosSlideShow, #VideosSlideShow').length > 0) {
        $('#PhotosSlideShow').jcarousel({
            scroll: 1,
            initCallback: carouselphotos_initCallback,
            //isPagingOn: true,
            pagingSelector: '.jcarousel-control-photo a',
            pagingClass: 'On'
        });
        $('#VideosSlideShow').jcarousel({
            scroll: 1,
            initCallback: carouselvideos_initCallback,
            //isPagingOn: true,
            pagingSelector: '.jcarousel-control-videos a',
            pagingClass: 'On'
        });
    }

    //Content Slider For Lexus Lineup Section
    if ($('#coda-slider-1').length > 0) {
        $('#coda-slider-1').codaSlider({ isLexus: true, isFireTag: true, fireId:2242.1 });
    }
    
     //Content Slider For LifeStyle Section
    if ($('#coda-slider-2').length > 0) {
        $('#coda-slider-2').codaSlider({ isLexus: true, isFireTag: true, fireId:2244.1  });
    }

    //Content Scroll Bar for Life Style Section
    if ($('#pane1').length > 0) {
        $('#pane1').jScrollPane({ topCapHeight: 25 });
    }

    //Content Scroll for Access Graned Section
    if ($('#pane2').length > 0) {
        $('#pane2').jScrollPane();
    }

    //Content Scroll for Anchor Tags
    if ($('#pane3').length > 0) {
        var $pane3 = $('#pane3');
        $pane3.jScrollPane({ topCapHeight: 25, animateTo: true, animateInterval: 50 });
        $('a.scroll-to-element').bind(
			'click',
			function () {
			    var targetElementSelectorString = $(this).attr('rel');
			    $pane3[0].scrollTo(targetElementSelectorString);
			    return false;
			}
		);
    }

    //Last Schedule Item
    $('#ScheduleList li:first').css({ paddingTop: '0' });
    $('#ScheduleList li:last').addClass('Last');

    //Copy Right
    var current_date = new Date();
    year_value = current_date.getFullYear();
    $('#CopyRight').html("&copy; " + year_value + " Lexus, A Division of Toyota Motor Sales, U.S.A, Inc.");

    //Footer No Border for Last Li of each Ul
    $('#Footer ul').each(function () {
        $(this).find('li:last-child').addClass('NoBorder').css({ marginBottom: '0' });
    });
});

/* 
-- Jquery Functions End --
--Add this Functions Start Below--
*/

//Add This Click Back Functions
var addthis_config = {
    data_track_clickback: true
}

//Twitter Add this Functions
function GetFirstSentence(str) {
    var strFirstSentence = "";
    if (str.indexOf(".") != -1) {
        strFirstSentence = str.substr(0, str.indexOf(".")) + ".";
    } else {
        strFirstSentence = str + ".";
    }
    return strFirstSentence;
}

function GetMetaDesc() {
    var arrMetaDesc = $('meta[name="description"]');
    var metaDesc = '';
    if (arrMetaDesc.length > 0) {
        metaDesc = GetFirstSentence(arrMetaDesc[0].getAttribute('content'));
    }
    return metaDesc;
}
var twitterTempDesc = GetMetaDesc() + ' {{url}} from luxuryawaits.com';
var addthis_share = 
{ 
    templates: {
        twitter: twitterTempDesc
    }
}

function callTwitterFireTag(){
    // ** following lines are commented out because 
    // it is handled by addThis event at the bottom of the page **
    //var twContent = GetMetaDesc();
    //fireTag(2240.2,{'<share_type>':'Twitter','<share_content>':twContent });    
    return true;
}

function callFacebookFireTag(){
    var fbContent = $.trim($('title').text());
    fireTag(2240.2,{'<share_type>':'Facebook','<share_content>':fbContent });
    return true;
}


// Alert a message when the user shares somewhere
function addThisEventHandler(evt) {
    switch (evt.type) {
        case "addthis.menu.share":
            if (evt.data.service == "twitter") {
                var twContent = GetMetaDesc();
                fireTag(2240.2, { '<share_type>': 'Twitter', '<share_content>': twContent });
            }
            break;
        default:
            //console.log('received an unexpected event', evt);
    }
}

// Listen to various events
addthis.addEventListener('addthis.menu.share', addThisEventHandler);
