/*
 * Project: Playfair Mining Ltd.
 * Author: Rudy Affandi
 * Created: 01/03/2011
 * File name: functions.js
 */

// Fix jQuery rounded corners in IE
$.uicornerfix = function(r){
  DD_roundies.addRule('.ui-corner-all', r);
  DD_roundies.addRule('.ui-corner-top', r+' '+r+' 0 0');
  DD_roundies.addRule('.ui-corner-bottom', '0 0 '+r+' '+r);
  DD_roundies.addRule('.ui-corner-right', '0 '+r+' '+r+' 0');
  DD_roundies.addRule('.ui-corner-left', r+' 0 0 '+r);
  DD_roundies.addRule('.ui-corner-tl', r+' 0 0 0');
  DD_roundies.addRule('.ui-corner-tr', '0 '+r+' 0 0');
  DD_roundies.addRule('.ui-corner-br', '0 0 '+r+' 0');
  DD_roundies.addRule('.ui-corner-bl', '0 0 0 '+r);
};
// Initiate IE rounded corners
$.uicornerfix('8px');

// jQuery initialization and CSS settings
$(function(){
	$( 'body' ).addClass('main_bg');
	$( '.body' ).addClass('ui-corner-all');
	$( '.footer_bg' ).addClass('ui-corner-all');
	
	// Activate "class='active'" on current URL
	var path = location.pathname.substring(1);
   if ( path )
	{
     $('.menu_col ul li a[href$="' + path + '"]').attr('class', 'active');
     $('#nav li a[href$="' + path + '"]').attr('class', 'active');
     $('.footer_nav li a[href$="' + path + '"]').attr('class', 'active');
	}
	// For URL with parameters
	var path2 = location.search.substring(1);
   if ( path2 )
	{
     $('.menu_col ul li a[href$="' + path2 + '"]').attr('class', 'active');
     $('#nav li a[href$="' + path2 + '"]').attr('class', 'active');
     $('.footer_nav li a[href$="' + path2 + '"]').attr('class', 'active');
	}

	// add dynamically generated drop shadow and redraw after every window resize	
	$( '.gallery_grid ul li a img' ).dropShadow({left: 1, top: 1, opacity: 0.1, color: '#333'});
	$(document).ready(function(){
		$(window).bind("resize", resizeWindow);
		function resizeWindow() {
			$( '.gallery_grid ul li a img' ).redrawShadow({left: 1, top: 1, opacity: 0.1, color: '#333'});
		}
	});

	// Add map highlight to the Property Portfolio
	$('.map').maphilight();

	// Initiate front page slide show
	rotator();
	
});

function rotator() {
	$('.front_image div').css('opacity', 0)
   .eq(0).css('opacity', 1).addClass('active');
   setInterval('rotateIt()', 5000);
}

function rotateIt() {
    // Get the first image
    var currentImg = $('.front_image div.active') ? $('.front_image div.active') : $('.front_image div:first');

	// Get next image, when it reaches the end, rotate it back to the first image
    var nextImg = (currentImg.next().length) ? (currentImg.next().hasClass('active')) ? $('.front_image div:first') : currentImg.next() : $('.front_image div:first');	
	
	// Set the fade in effect for the next image, the active class has higher z-index
	nextImg.css('opacity', 0)
	       .addClass('active')
	       .animate({opacity: 1}, 1500);

	// Hide the current image
	currentImg.animate({opacity: 0}, 1500)
	          .removeClass('active');
}

// Main navigation, please use #nav for your main UL
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// Side navigation accordion settings
jQuery.fn.initMenu = function() {  
    return this.each(function(){
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        // The following 3 lines add stickiness to the accordion menu
	  	  $('ul.acitem li a.active').parent().parent().parent().addClass('expand');
	  	  $('ul.acitem li a.active').parent().parent().css('display','block');
	  	  $('ul.menu li.expand').children('a').addClass('active');
        $('li a', this).click(
            function(e) {
                e.stopImmediatePropagation();
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
                if($(parent).hasClass('noaccordion')) {
                    if(theElement[0] === undefined) {
                        window.location.href = this.href;
                    }
                    $(theElement).slideToggle('normal', function() {
                        if ($(this).is(':visible')) {
                            $(this).prev().addClass('active');
                        }
                        else {
                            $(this).prev().removeClass('active');
                        }    
                    });
						  if (!typeof $(this).closest(ÔaÕ).attr(ÔhrefÕ) == ÔundefinedÕ) {
						     window.location.href = $(this).closest(ÔaÕ).attr(ÔhrefÕ);
						  }
                    return false;
                }
                else {
                    if(theElement.hasClass('acitem') && theElement.is(':visible')) {
                        if($(parent).hasClass('collapsible')) {
                            $('.acitem:visible', parent).first().slideUp('normal', 
                            function() {
                                $(this).prev().removeClass('active');
                            }
                        );
								if (!typeof $(this).closest(ÔaÕ).attr(ÔhrefÕ) == ÔundefinedÕ) {
									window.location.href = $(this).closest(ÔaÕ).attr(ÔhrefÕ);
								}
                        return false;  
                    }
							if (!typeof $(this).closest(ÔaÕ).attr(ÔhrefÕ) == ÔundefinedÕ) {
							window.location.href = $(this).closest(ÔaÕ).attr(ÔhrefÕ);
							}
                    return false;
                }
                if(theElement.hasClass('acitem') && !theElement.is(':visible')) {         
                    $('.acitem:visible', parent).first().slideUp('normal', function() {
                        $(this).prev().removeClass('active');
                    });
                    theElement.slideDown('normal', function() {
                        $(this).prev().addClass('active');
                    });
							if (!typeof $(this).closest(ÔaÕ).attr(ÔhrefÕ) == ÔundefinedÕ) {
								window.location.href = $(this).closest(ÔaÕ).attr(ÔhrefÕ);
							}
                    return false;
                }
            }
        }
    );
});
};

$(document).ready(function() {$('.menu').initMenu();});


