// JavaScript Document
$(document).ready(function(){
    if ($.browser.msie){
       $('#menu ul.navmenu li:last-child .menutop').css('border-width','0 1px 1px;');
    }

    $('.toggle:not(.toggle-open)') .addClass('toggle-closed') .parents('li') .children('ul') .hide();

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

        var selected_obj = this;
        $(selected_obj).parents('div').attr("id")

        $('ul.navmenu li div').each(function(index) {
            if($(this).attr("id") != $(selected_obj).parents('div').attr("id") && $(this) .hasClass('menutop-open')){
                $(this) .children('a').removeClass('toggle-open') .addClass('toggle-closed') .parents('li') .children('ul') .slideUp(250);
                $(this) .removeClass('menutop-open') .addClass('menutop-closed');
            }
        });


        if ($(this) .hasClass('toggle-open')) {
             $(this) .removeClass('toggle-open') .addClass('toggle-closed') .parents('li') .children('ul') .slideUp(250);
             $(this) .parent('.menutop') .removeClass('menutop-open') .addClass('menutop-closed');
        }else{
             $(this) .parent('.menutop') .removeClass('menutop-closed') .addClass('menutop-open');
             $(this) .removeClass('toggle-closed') .addClass('toggle-open') .parents('li') .children('ul') .slideDown(250);
        }
    }); 

    /* product list - preview */
    imagePreview();

    /* read more / less buttons */
    $(".read-more").click(function()
    {
        if($('.content_box_text_hidden').length != 0){
            $(".content_box_text_hidden").slideDown(1000);
        }

        if($('.category_description_hidden').length != 0){
            $(".category_description_hidden").slideDown(1000);
        } 
    });
    
    $(".read-less").click(function()
    {
        if($('.content_box_text_hidden').length != 0){
            $(".content_box_text_hidden").slideUp(1000);
        }

        if($('.category_description_hidden').length != 0){
            $(".category_description_hidden").slideUp(1000);
        }
    });

});

function initialize(zoom_value) {
   
    var marker, i;
    if(zoom_value == 2){
        var myOptions = {
            zoom: 2,
            center: new google.maps.LatLng(locations[0][1], locations[0][2]),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    } else {
        var myOptions = {
            zoom: 16,
            center: new google.maps.LatLng(locations[0][1], locations[0][2]),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    }
/*
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

var contentString = '<div id="content">'+
    '<div id="siteNotice">'+
    '</div>'+
    '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
    '<div id="bodyContent">'+
    '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
    'sandstone rock formation in the southern part of the '+
    'Northern Territory, central Australia. It lies 335 km (208 mi) '+
    'south west of the nearest large town, Alice Springs; 450 km '+
    '(280 mi) by road. Kata Tjuta and Uluru are the two major '+
    'features of the Uluru - Kata Tjuta National Park. Uluru is '+
    'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
    'Aboriginal people of the area. It has many springs, waterholes, '+
    'rock caves and ancient paintings. Uluru is listed as a World '+
    'Heritage Site.</p>'+
    '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
    'http://en.wikipedia.org/w/index.php?title=Uluru</a> (last visited June 22, 2009).</p>'+
    '</div>'+
    '</div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[0][1], locations[0][2]),
    map: map
});

google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map,marker);
});
*/


    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    var marker = new google.maps.Marker({
      map: map,
      position: new google.maps.LatLng(locations[0][1], locations[0][2])
    });

    var infowindow = new google.maps.InfoWindow();

    google.maps.event.addListener(marker, "click", (function(marker) {
    return function() {
      infowindow.setContent(locations[0][0], {maxWidth:'500px'});
      infowindow.open(map, marker);
    }
    })(marker));

    for (i = 1; i < locations.length; i++) {
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0], {maxWidth:'500px'});
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
    
}

function slideShow(obj, speed) {


	//append a LI item to the UL list for displaying caption
	$('ul.slideshow'+obj).append('<li id="slideshow'+obj+'-caption" class="caption"><div class="slideshow-caption-container" onclick="document.location.href=\''+$('ul.slideshow'+obj+' a:first').attr('href')+'\'"><h3></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul.slideshow'+obj+' li').css({opacity: 0.0});

	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow'+obj+' li:first').css({opacity: 1.0});

	//Get the caption of the first image from REL attribute and display it
	$('#slideshow'+obj+'-caption h3').html($('ul.slideshow'+obj+' a:first').find('img').attr('title'));
	$('#slideshow'+obj+'-caption p').html($('ul.slideshow'+obj+' a:first').find('img').attr('alt'));

	//Display the caption
	$('#slideshow'+obj+'-caption').css({opacity: 0.7, bottom:0});

	//Call the gallery function to run the slideshow
	var timer = setInterval('gallery("'+obj+'")',speed);

	//pause the slideshow on mouse over
	$('ul.slideshow'+obj).hover(
		function () {
			clearInterval(timer);
		},
		function () {
			timer = setInterval('gallery("'+obj+'")',speed);
		}
	);

}

function gallery(obj) {


	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow'+obj+' li.show')?  $('ul.slideshow'+obj+' li.show') : $('#ul.slideshow'+obj+' li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow'+obj+'-caption')? $('ul.slideshow'+obj+' li:first') :current.next()) : $('ul.slideshow'+obj+' li:first'));

	//Get next image caption
	var title = next.find('img').attr('title');
	var desc = next.find('img').attr('alt');

	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);

	//Hide the caption first, and then set and display the caption
	$('#slideshow'+obj+'-caption').slideToggle(300, function () {
		$('#slideshow'+obj+'-caption h3').html(title);
		$('#slideshow'+obj+'-caption p').html(desc);
		$('#slideshow'+obj+'-caption').slideToggle(500);
	});

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');

}

this.imagePreview = function(){	
    /* CONFIG */
    xOffset = 10;
    yOffset = 30;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result
    /* END CONFIG */
    
    $("a.preview").hover(function(e){

            this.t = this.title;
            this.title = "";

            this.t_style = this.style;

            this.t1 = $(this).find('img').attr('title');
            this.title1 = "";

            this.d = $(this).find('img').attr('alt');
            this.desc = "";

            var year = (this.t != "") ? "<br /><span class='year'>" + this.t + "</span>" : "";
            var title = (this.t1 != "") ? "<br /><span class='title'>" + this.t1 + "</span>" : "";
            var desc = (this.d != "") ? "<br /><span class='desc'>" + this.d + "</span>" : "";
            $("body").append("<p id='preview'><img src='"+ this.name +"' style='height:"+ this.t_style.height +";' alt='Image preview' />"+ year + title + desc +"</p>");
            $("#preview")
                    .css("top",(e.pageY - xOffset) + "px")
                    .css("left",(e.pageX + yOffset) + "px")
                    .fadeIn("fast");						
    }, function(){
        this.title = this.t;
        this.title1 = this.t1;
        this.desc = this.d;
        $("#preview").remove();
    });
    
    $("a.preview").mousemove(function(e){
        $("#preview").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
    });
    $("a.preview").mouseout(function(e){
        $("#preview").remove();
    });
};


function product_search_items_per_page(){
    document.getElementById('search_form_items').submit();
}

function check_contact_form(){

    if(document.contact_form.firstname.value == ''){
        alert("Enter your first name.");
    }else if(document.contact_form.lastname.value == ''){
        alert("Enter your last name.");
    } else if(document.contact_form.email.value == '') {
        alert("Enter your email address.");
    } else if(checkemail(document.contact_form.email.value) == false) {
        alert("Enter valid email address.");
    } else if(document.contact_form.subject.value == '') {
        alert("Enter subject of your message.");
    } else if(document.contact_form.message.value == '') {
        alert("Enter your message.");
    } else {
        document.contact_form.submit();
    }

}

function checkemail(str){
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
        return true;
    else{
        return false;
    }
}

function read_more(){


}

function read_less(){

}
