// JavaScript Document

$(document).ready(function () {

    Shadowbox.init({
        overlayOpacity: .7
    });


    $('.modal').colorbox({ iframe: true, innerWidth: 940, innerHeight: 600, opacity: 0.8 });

    $('.drawer h2').click(function () {
        $(this).siblings('p').slideToggle('fast');
        $(this).siblings('ul').slideToggle('fast');
    });


    /* stuff for main menu 8*/

    $('.sf-menu li ').mouseenter(function () {
        $(this).children('ul').show();
    }).mouseleave(function () {
        $(this).children('ul').hide();
    });

    /* -----------------------------------------
    controls to show/hide search boxed
    -------------------------------------------*/
    $('#search-colour').mouseenter(function () {
        $('#filter-colour').show();
    }).mouseleave(function () {
        $('#filter-colour').hide();
    });

    $('#search-series').mouseenter(function () {
        $('#filter-series').show();
    }).mouseleave(function () {
        $('#filter-series').hide();
    });

    $('#search-eco').mouseenter(function () {
        $('#filter-eco').show();
    }).mouseleave(function () {
        $('#filter-eco').hide();
    });

    $('#search-surface').mouseenter(function () {
        $('#filter-surface').show();
    }).mouseleave(function () {
        $('#filter-surface').hide();
    });

   


    /*------------------------------------------
    SWATCH ROLLOVER ACTIONS TO SHOW TITLES
    -------------------------------------------*/

    $('#filter-colour a').mouseenter(function () {
        $(this).children('h3').show();
    }).mouseleave(function () {
        $(this).children('h3').hide();
    });


    $('.tilesample').mouseenter(function () {
        $(this).children('h3').show();
    }).mouseleave(function () {
        $(this).children('h3').hide();
    });


    /*--------------------------------------------
    LOCATIONS ACTIONS
    -------------------------------------------------*/

    $('.loc-map').click(function () {

        openLocationMap(this);
    });

    $('.loc-showroom').click(function () {
        closeLocInfo();
        $(this).siblings('.marker').html('<img src="/images/layout/marker-arrow-down.gif" />');
        $(this).parent().parent().parent().siblings('.loc-showroom-container').slideDown('fast');
    });

    $('.loc-hours').click(function () {
        closeLocInfo();
        $(this).siblings('.marker').html('<img src="/images/layout/marker-arrow-down.gif" />');
        $(this).parent().parent().parent().siblings('.loc-hours-container').slideDown('fast');
    });

   /* $('.marker').click(function () {
        closeLocInfo();
        $('.marker').html('<img src="/images/layout/marker-arrow.gif" />');

    });
	*/
	
	 $('.marker').click(function () {
		var imgname = $(this).children('img').attr('src');
		if (imgname == "/images/layout/marker-arrow-down.gif"){
       		 closeLocInfo();
        	$('.marker').html('<img src="/images/layout/marker-arrow.gif" />');
		}else{
			var themap = $(this).siblings('.loc-map');
			openLocationMap(themap);
		}
		

    });



    $('.galleryLauncher').click(function () {

        var id = $(this).attr('gallery');
        //  alert(id);
		
		var firstimage = $(this).parent().parent().siblings('.gall-img-container').children('a:first').attr('href');
		
       //var content = Shadowbox.cache[1].content; this is version 9
	   var content = firstimage;
        var options = {

    };
    Shadowbox.open({
        content: content,
        player: "img",
        gallery: id,
        options: options
    });

    return false;
});


/// add shading to alternating table rows in locations
$(".loc-hours-container tr:odd").addClass("alt");





/*----------------------------------------------
SCROLL TO ACTIONS FOR TILE RESULTS
-------------------------------------------------*/
$('.tilesample').click(function () {

    var myclicked = $(this);

    opentile(myclicked);
    //$.scrollTo(myclicked, 500);

});

$('.closebut').click(function () {
    $('.loaded-tile').slideUp(500);
    $('.tilesample').removeClass('active');
});



$('.productSwitcher').click(function () {
    switchProduct(this);
});

var deepLinkTest = typeof deepLinked;

$(".tileLink").click(function () {

    var id = $(this).attr("id");
    if (deepLinkTest == "undefined") {

        document.location = "#" + id;
        return false;

        return false;
    } else {
        //hmmm...
        var url = window.location.pathname + "?" + GetTileQueryString() + "#" + id; ;
        document.location = url;
        return false;
    }
});


var hash = window.location.hash;

var location = $(hash);

if (location.length) {
    //we have an object
    //find the map
    var map = location.find('.loc-map');
    if (map.length) {
        openLocationMap(map);

    } 

    //location.children('.loc-map-container').slideDown();

}

//extra _ to stop the tags from matching on click, and jumping the window around.
var product = $(hash+"_");
if (product.length && product.hasClass("productSwitcher")) {
    //its a product
    switchProduct(product);
}


hash = hash.replace("#", "");
openTileByHash(window.location.hash);
$(window).hashchange(function () {

    var hash = window.location.hash;
    // Alerts every time the hash changes!
    openTileByHash(hash);

    var product = $(hash + "_");
    if (product.length && product.hasClass("productSwitcher")) {
        //its a product
        switchProduct(product);
    }
})


});


function switchProduct(that) {
    var type = $(that).attr("type");
    $(".productDiv").hide();
    //special character problems
    $("#" + type).show();

    //set active
    $(".productSwitcher").removeClass("active");
    $(that).addClass("active");
}

function openLocationMap(that) {

    closeLocInfo();
    $(that).siblings('.marker').html('<img src="/images/layout/marker-arrow-down.gif" />');
    $(that).parent().parent().parent().siblings('.loc-map-container').slideDown('fast');
    mapClick($(that));

}

    function openTileByHash(hash) {
        hash = hash.replace("#", "");
        var ob = $("#TileId" + hash);
        var deepLinkTest = typeof deepLinked;

        if (ob.length > 0 && deepLinkTest == "undefined") {
            opentile(ob);
        }
    }

    function GetTileQueryString() {
        var params = {},
    q = window.location.search;
        var stringVal = "";
        if (q) {

            var urlParams = {};
            (function () {
                var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

                while (e = r.exec(q)) {
                    if (e[1] == "tile") {
                        continue;
                    }

                    urlParams[d(e[1])] = d(e[2]);
                }
            })();


        }

        for (var i in urlParams) {

            if (stringVal.length > 0) {
                stringVal += "&";
            }

            stringVal += i + "=" + urlParams[i];


        }

        return stringVal;



    }

    function opentile(myclicked) {
        var deepLinkTest = typeof deepLinked;
        if (deepLinkTest != "undefined") {
            return false;
        }



        var id = myclicked.attr("tileid");

        var rowid = myclicked.attr("rowid");

        var details = $("#tile-details" + rowid);


        $(".loaded-tile").slideUp(100);

        $('.tilesample').removeClass('active');
        $(myclicked).addClass('active');



        details.load("/tile/" + id, function () {
            details.slideDown(500, function () {
                var o = myclicked.parent();
                var x = o.parent();
                $.scrollTo(myclicked, 500);

            });
        });







        //$(this).siblings('.loaded-tile').load('tilepage.html'); 
        /* 
    
        $(this).delay(500).siblings('.loaded-tile').slideDown('slow', function () {
        $.scrollTo(myclicked, 500);

        init();
        });

        */
    }

    function closeTile() {
        $('.loaded-tile').slideUp(500);
        $('.tilesample').removeClass('active');
    }


/// function to close any open location info windows before opening others.
function closeLocInfo(){
	$('.loc-map-container').slideUp('fast');
	$('.loc-showroom-container').slideUp('fast');
	$('.loc-hours-container').slideUp('fast');
}

function init(){
	
	$("tr:odd").addClass("alt");
	
	$('.closebut').click(function(){
		$('.loaded-tile').slideUp(500);	
	});
		
}




function initializeMap(lat, long, id, text) {
    var ll = new google.maps.LatLng(lat, long);
    var myOptions = {
        zoom: 15,
        center: ll,
        mapTypeControl: false,
        scaleControl: false,
        streetViewControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map_canvas' + id), myOptions);

    var marker = new google.maps.Marker({
        position: ll,
        map: map,
        title: text
    });
}

function mapClick(that) {
    var lat = that.attr("lat");
    var long = that.attr("long");
    var id = that.attr("id");
    var text = that.attr("textVal");
    initializeMap(parseFloat(lat), parseFloat(long), id, text);
}

function fireClick(path) {
    var ob = $(path);
    if (ob != null) {
        ob.trigger('click');
    }
}

function closeTile() {
        $('.loaded-tile').slideUp(500);
        $('.tilesample').removeClass('active');
}
