//WP sIFR Current Item
/*
var Infinity = { src: "/wp-content/plugins/wp-sifr/fonts/Infinity.swf" };
sIFR.useStyleCheck = true;
sIFR.activate(Infinity);
sIFR.replace(Infinity, {
	selector: "#navigation li.current_page_item div",
	css: [
		'.sIFR-root { font-size:14px; font-weight:normal; color:#ffffff; }',
		'a { text-decoration:none; color:#ffffff; }',
		'a:hover{ color:#ffffff;}'
	],
	wmode: 'transparent'
});

//WP sIFR navigation
var Infinity = { src: "/wp-content/plugins/wp-sifr/fonts/Infinity.swf" };
sIFR.useStyleCheck = true;
sIFR.activate(Infinity);
sIFR.replace(Infinity, {
	selector: "#navigation li div",
	css: [
		'.sIFR-root { font-size:14px; font-weight:normal; }',
		'a { text-decoration:none; }',
		'a:link { color: #006699; }',
		'a:hover{ color:#00ffff;}',			
	],
	wmode: 'transparent'
});
// WP sIFR hide navigation
var Infinity = { src: "wp-content/plugins/wp-sifr/fonts/Infinity.swf" };
sIFR.useStyleCheck = true;
sIFR.activate(Infinity);
sIFR.replace(Infinity, {
	selector: ".show-text div",
	css: [
		'.sIFR-root { font-size:14px; font-weight:normal; color:#00ffff; }',
		'a { text-decoration:none; color:#00ffff; }',
		'a:hover{ color:#00ffff;}'
	],
	wmode: 'transparent'
});
*/
function mainMenu()
{
//	jQuery("#navigation ul ul").css({"height":"0","overflow":"hidden"});
	jQuery("#navigation ul ul").css({"display":"none"});
	
	jQuery("#navigation ul li").click(function(){
		//jQuery("#navigation ul ul").css({"display":"none"});
		if(jQuery(this).find("ul:first").css("display") == "none") {
			jQuery("#navigation ul ul").hide(500);			
			jQuery(this).find('ul:first').show(500);
			
		} else {
			jQuery(this).find('ul:first').hide(500);
		}
		
		//jQuery("#navigation ul ul").hide("slow");
	});

/*
	jQuery("#navigation ul li").toggle(function(){
		
		//jQuery(this).find('ul:first').css({visibility: "visible","height" :"auto","overflow":"visible"});
//		jQuery("#navigation ul ul").hide();
		
		jQuery(this).find('ul:first').show("slow");
			},function(){
				//jQuery(this).find('ul:first').css({"overflow": "hidden", "height": "0"});
				
				jQuery(this).find('ul:first').hide("slow");
		});
*/		
	
	
}
function IsValidEmail(e)
{
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    
    for(i=0; i < e.length ;i++)
    {
        if(ok.indexOf(e.charAt(i))<0)
        {    
            return false;
        }	
    } 
        
    if (!e.match(re) && e.match(re_two))
    {
        return true;		
    }
    else
    {
        return false;
    }    
}
function checkField(labelId, textboxId, value, error)
{
	if(value == "")
	{
		jQuery('#' +labelId).addClass("error");
		jQuery('#' +textboxId).addClass("error");
		jQuery('#' +labelId).html(error);		
		return false;
	}
	else
	{
		jQuery('#' +labelId).removeClass("error");
		jQuery('#' +textboxId).removeClass("error");
		jQuery('#' +labelId).html("");
	}	
	return true;
}
function checkEmailField(labelId, textboxId,value, error)
{
	if(value != "" && !IsValidEmail(value))
	{
		jQuery('#' +labelId).addClass("error");
		jQuery('#' +textboxId).addClass("error");
		jQuery('#' +labelId).html(error);
		return false;
	}
	else
	{
		jQuery('#' +labelId).removeClass("error");
		jQuery('#' +textboxId).removeClass("error");
		jQuery('#' +labelId).html("");
	}
	return true;
}
function checkFieldNewsletter(id, value, error)
{
	if(value == "")
	{
		jQuery('#' +id).addClass("error");	
		jQuery('#' +id).html(error);		
		return false;
	}
	else
	{
		jQuery('#' +id).removeClass("error");	
		jQuery('#' +id).html("");
	}	
	return true;
}
function checkEmailFieldNewsletter(id, value, error)
{
	if(value != "" && !IsValidEmail(value))
	{
		jQuery('#' +id).addClass("error");		
		jQuery('#' +id).html(error);
		return false;
	}
	else
	{
		jQuery('#' +id).removeClass("error");	
		jQuery('#' +id).html("");
	}
	return true;
}
function validateContactForm(form)
{
	
	var isValid = true;	

	isValid = isValid && checkField('lbl_name','ec_name',form.ec_name.value ,"Please enter your name.");
	isValid = isValid && checkField('lbl_email', 'ec_email',form.ec_email.value ,"Please enter your email");
	isValid = isValid && checkEmailField('lbl_email', 'ec_email',form.ec_email.value ,"Please enter a valid email address.");	
	isValid = isValid && checkField('lbl_phone' , 'ec_phone',form.ec_phone.value ,"Please enter your phone");
	isValid = isValid && checkField('lbl_message', 'ec_message',form.ec_message.value ,"Please enter your message");
	
	return isValid;	
}
function validateNewsletterForm(form)
{
	
	var isValid = true;	

	isValid = isValid && checkFieldNewsletter('txtemail',form.txtemail.value ,"");
	isValid = isValid && checkEmailFieldNewsletter('txtemail',form.txtemail.value ,"");
	
	return isValid;	
}
function validateCareersForm(form)
{	
	var isValid = true;
	
	isValid = isValid && checkEmailField('ecr_email', 'ecr_email',form.ecr_email.value ,"");		
	return isValid;
}
function showHideContent()
{
	//hide content 
	jQuery(".hide-text").click(function(e)
	{
		jQuery(this).fadeOut();		
		jQuery("#navigation").slideUp("slow");
		jQuery("#contentpane").hide("slow");		
		jQuery(".contact-spotlight").fadeOut();
		
		if(valuesArrayImage.length > 1) 
		{
			jQuery(".play-control").fadeIn();
		}
		//jQuery(".show-text").fadeIn();
		jQuery(".show-text").css({"overflow":"visible","padding-top":"9px","padding-bottom":"6px","padding-left":"23px"});
		
//		alert(jQuery("#page_id").val());
		
		var pageId = jQuery("#page_id").attr('title');
		var categoryId = jQuery("#category_id").attr('title');
		if(categoryId != '') {
			pageId = categoryId;
		}
		
		jQuery(".show-title").attr('id', 'title-'+pageId);
//		jQuery(".show-text").attr('id', 'show-text-'+jQuery("#page_id").val());
		jQuery(".show-text").fadeIn();
		jQuery(".show-text").css('height', jQuery(".show-title").height());
		
		jQuery(".click-show-text").css({"display":"block","opacity":"0"});
		jQuery(".click-show-text").attr('title',jQuery("h1").text());
		
		
	});
	// show content
	jQuery(".click-show-text").click(function()
	{
		jQuery("#navigation ul ul").css("display", "none");
		//jQuery(this).fadeOut();		
		jQuery(".click-show-text").css({"display":"none"});
		jQuery(".play-control").fadeOut();
		jQuery("#contentpane").show("slow");
		
		if(jQuery(this).attr('title') != 'CONTACT US') {
			jQuery("#navigation").slideDown("slow");			
			jQuery(".contact-spotlight").slideUp("slow");
		} else {
			//jQuery(".contact-content").show();
			jQuery(".contact-spotlight").slideDown("slow");
		}
			
		jQuery(".hide-text").fadeIn();
		jQuery(".show-text").css({"height":"0","overflow":"hidden","padding":"0"});
		
		
/*		jQuery(".contact-spotlight").fadeIn();*/		
	});
}
function topMenuAction()
{
	// Show and Hide Newsletter Box http://www.webresourcesdepot.com/sliding-top-menu-with-jquery/
	jQuery("#topMenuAction").click( function()
	{	
        
    }); 
}
function Navigation()
{
	// Navigaton Slide Out
	if (jQuery('#navigation li').hasClass("current_hover"))
	{
		jQuery('#navigation .current_hover ul').addClass("display-list"); 
	}	
	
	// Add a class to the parent li IF it has sub UL's
	jQuery("#navigation li:has(ul)").addClass("nav-parent");
	
	// Add a class to sub-UL if it has a parent LI
	jQuery("#navigation li ul").addClass("nav-child");
	
	jQuery("#navigation li.nav-parent").each(function(index)
	{
		if(!(jQuery(this).hasClass('current_page_item')) && !(jQuery(this).hasClass('current_page_parent')))
		{
			// Hide the submenus
			//jQuery(this).find("ul").hide();
			jQuery(this).find("ul").css({"overflow":"hidden","height":"0"});
		}
	
		jQuery(this).hover(function ()
		{
			if(!(jQuery(this).hasClass('current_page_item')) && !(jQuery(this).hasClass('current_page_parent')))
			{
				
				jQuery(this).addClass("current_hover");
				jQuery(this).addClass("current_item");
				jQuery(this).find("ul").css("height","auto");
				jQuery(this).children('.nav-child').stop(true,true).slideDown(400).removeClass("display-list"); // fired on mouseover
			}
			
		}, 
		function ()
		{
				if(!(jQuery(this).hasClass('current_page_item')) && !(jQuery(this).hasClass('current_page_parent'))){
					
					jQuery(this).children('.nav-child').slideUp(400, function()
					{				    	
						jQuery("#navigation li.nav-parent").removeClass("current_item");
				  	}); // fired on mouseout
				  	
			 	}
			}
		);
	});
	jQuery("#navigation li").hover(function() {
		
		jQuery(this).addClass("hover");
	},function(){
		jQuery(this).removeClass("hover");
	}
	);
	
}
function fadeContentOut()
{
	jQuery("#contentpane>div").fadeOut("slow");	
	jQuery(".hide-text").fadeOut("slow");
}
function fadeContentIn()
{
	jQuery("#contentpane>div").css('display','none');
	jQuery("#contentpane>div").fadeIn("slow");
	jQuery(".hide-text").fadeIn("slow");		
}



function getPath(href) {
	var parts = href.split('/');
	
	if(parts.length > 1 && parts[1].length==0 && parts[0].length>0 && parts[0].substr(parts[0].length-1)==':') {
		if(parts[0] != 'http:'){
			return null;
		}
		parts = parts.slice(2);
		parts[0] = '';
	} else if(href.indexOf('mailto:')== 0) {
		return null;
	}
	return parts.join('/'); 
}

function updateLinks(selector) {
	jQuery(selector + ' a[href]').click(function(e) {
		if(jQuery(this).hasClass('popup')){
//			PopupWindow(this.href);
			e.preventDefault();
		} else if(jQuery(this).hasClass('modal')){
//			popupMovie(this);
			e.preventDefault();
		} else if(jQuery(this).attr('rel') == 'lightbox'){
//			myLightbox.start(this);
			e.preventDefault();
		} else if(typeof(this.target) != "string" || this.target.length == 0) {
			var path = getPath(this.href);
			if((/\?show\=newsletter$/i).test(path))
			{
				e.preventDefault();
				if (jQuery("#openCloseIdentifier").is(":hidden"))
		        {
		        	/*jQuery("#slideNewsletter").animate({
		                marginTop: "-141px"
		                }, 500 );*/
					jQuery("#slideNewsletter").hide("slow");
		            
		        	jQuery("#openCloseIdentifier").show();
		        }
		        else
		        {
		        	/*jQuery("#slideNewsletter").animate({
		                marginTop: "0px"
		                }, 500 );*/
		        	jQuery("#slideNewsletter").show("slow");
		           
		        	jQuery("#openCloseIdentifier").hide();
		        }
				
			}
			else if(path && !(/\.(jpg|gif|png|html|htm|jpeg|pdf)$/i).test(path)){
				SWFAddress.setValue(path);
				e.preventDefault();
			}
		}
	});
	
}

function updateScroll()
{
//	var scrollHeight = jQuery('#contentpane').height() - jQuery('#contentpane>div>h1+div').position().top + 60;
//	jQuery('#contentpane').css('height', scrollHeight);
	jQuery('#pane1').jScrollPane({showArrows:true});
}




function updatePanel() {
	updateLinks("#navigation");
	updateLinks("#contentpane");
	updateScroll();
	//temp.scroll(
	//scrollPane.getViewport().setViewPosition(new Point(0,0));
	
	//call submit form
	jQuery("#contactfrm").submit(function(e){		
		if(validateContactForm(this)){
			
			submitForm("#contactfrm");
			
			
		}// else alert('invalid post');
		return false;
	});	
	
	//call submit career form
	//validateCareersForm
	jQuery("#careersfrm").submit(function(e){
		
		if(validateCareersForm(this)){
			submitForm("#careersfrm");
			//return true;
		}
		return false;
	});
	
	//call submit newsletter form
	//validateCareersForm
	jQuery("#frmsubscribe").submit(function(e){
		
		if(validateNewsletterForm(this)){
			submitFormNewsletter("#frmsubscribe");			
			
		}
		return false;
	});
}

function updateTitle(data)
{
	
	var beginTag = '<title>';
	var endTag = '</title>'
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			end = data.indexOf(endTag, begin);
			if(end > 0){
				SWFAddress.setTitle(data.substr(begin, end-begin).replace('&amp;', '&'));
				SWFAddress.setTitle(data.substr(begin, end-begin).replace('&#8211;', '-'));				
			}
		}
	}
}

function updateNewsletter(data)
{

	var beginTag = '<div class="formmessage">';
	var endTag = '</div>';
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			var end = data.indexOf(endTag, begin);
			if(end > 0){
				jQuery(".formmessage").html(data.substr(begin, end-begin));
				jQuery('.formmessage').fadeIn();
				jQuery(".formmessage p.error").append('<a href="#" class="backform"> Back to form</a>');
				jQuery('.formshow').fadeOut('slow', function(){
					
					jQuery("#slideNewsletter").animate({"height":"20px"},400);	
				});
				
				
				jQuery('.backform').click(function(e){
					jQuery('.formshow').fadeIn();
					jQuery('.formmessage').fadeOut();
					
					e.preventDefault();
				});
				
				return true;
			}
		}
	}
	
}


function updateContent(data)
{
	var beginTag = '<!-- CONTENT-BEGIN -->';
	var endTag = '<!-- CONTENT-END -->';
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			var end = data.indexOf(endTag, begin);
			if(end > 0){
//				jQuery("#nav").remove();
				jQuery("#wrapper #contentpane>div").remove();
				jQuery("#contentpane").append(data.substr(begin, end-begin));
				fadeContentIn();
				updatePanel();
				
				//change style file upload http://pixelmatrixdesign.com/uniform/
				//jQuery("input#ecr_cv").uniform();
				createUploader();
				return true;
			}
		}
	}
}
function updateRotateImages(data)
{
/*

	var beginTag = '<!-- ROTATE-IMAGES-BEGIN -->';
	var endTag = '<!-- ROTATE-IMAGES-END -->';
	if(typeof(data) == 'string')
	{
		var begin = data.indexOf(beginTag,0);
		if(begin >0)
		{
			begin += beginTag.length;
			var end = data.indexOf(endTag, begin);
			if(end > 0){
				
				
				
				//jQuery("#rotate-image").css('display', 'none');
				jQuery("#rotate-image:first").css('display', 'none');
				jQuery("#rotate-image").html(data.substr(begin, end-begin)).fadeIn(1500);
				var pageId = jQuery("#page_id").val();
				
				//jQuery("#rotate-image").fadeIn();
				
				
var pageId = jQuery("#page_id").val();
alert(pageId);
				//jsonImage(true);
				clearInterval($intervalid);
				return true;
			}
		}
	}
	*/
	
	var pageId = jQuery("#page_id").attr('title');
	/*
	var categoryId = jQuery("#category_id").attr('title');
	if(categoryId != '') {
		pageId = categoryId;
	}
	*/
	
	//input value to array
	valuesArrayImage = [];
	var i = 0;
	jQuery.each(valuesArray, function(key, value) {
		jQuery.each(value, function(id, item){			
			if(pageId == id) {				
				valuesArrayImage[i] = item;
								
				i +=1;
			}
		});
    			
	});
	
	
	imageRotatorImage = 0;
				//jsonImage(true);
				clearInterval($intervalid);
	if(valuesArrayImage.length > 1) {
//		rotateOneImage();				
//		$intervalid = setInterval( "rotateImage()", 10000/2 );
		rotateImage();		
	}
	else {
		clearInterval($intervalid);
		rotateOneImage();
	}
			
}


function updateNavigation(path)
{


	
	jQuery("#navigation li").removeClass('current_page_item');
	jQuery("#navigation li").removeClass('current_page_ancestor');
	jQuery("#navigation li").removeClass('current_page_parent');
	jQuery("#navigation li").removeClass('current_post_item');
	jQuery("#navigation a[href='" +path+ "']").each(function(index, element){
		
		
		
		jQuery(element).parents("li").each(function(index, item){
			if(index == 0){
				jQuery(item).addClass('current_page_item');
				jQuery(element).addClass(jQuery(element).parents("li").attr('class'));
				//jQuery(item + ' > a').addClass(jQuery(item).attr('class'));
				//jQuery(item + ' a:first-child').addClass(jQuery(item).attr('class'));
			} else {
				if(index == 1) {
					jQuery(item).addClass('current_page_parent');
					jQuery(item).find('a').addClass(jQuery(item).attr('class'));
					//jQuery(element).addClass(jQuery(element).parents("li").attr('class'));
				}
				jQuery(item).addClass('current_page_ancestor');
				
			}
		});
	});

	//Check in post detail
	var category_id = jQuery('#category_id').attr('title');
	if(category_id != '') {
		jQuery("#navigation li.page-item-"+category_id).addClass('current_post_item');
	}
	
	
	
}



function loadPath(path) {
	jQuery("#navigation ul ul").hide("slow");
	fadeContentOut();
	
	jQuery('#image-pause').css('display','inline');
	jQuery('#image-play').css('display','none');
	
	jQuery("#loading").show();
	jQuery.ajax({
		url: path,
		success: function(data) {
			updateTitle(data);
			updateContent(data);
			updateRotateImages(data);
//			updateContactSpotlight(data);
			updateNavigation(path);			
			jQuery("#loading").hide();
		}
	});
}

function handleChange(event)
{
	
	var path = event.path;
	if(window['ajaxpath'] != path)
	{
		window['ajaxpath'] = path;
		loadPath(path);
	}
}

function handleExternalChange(event)
{
}
function submitForm(selector)
{
	fadeContentOut();
	jQuery.post(window['ajaxpath'],
		jQuery(selector).serialize(),
		function(data) {			
			updateContent(data);			
		});
}
function submitFormNewsletter(selector)
{
	//fadeContentOut();
	jQuery.post(window['ajaxpath'],
		jQuery(selector).serialize(),
		function(data) {	
			
			updateNewsletter(data);			
		});	
}


function scaleImage(image, width, height)
{
	if(image.src)
	{
		jQuery(image).css('min-height', '');
		jQuery(image).css('min-width', '');
		jQuery(image).css('height', 'auto');
		jQuery(image).css('width', 'auto');
		
		if(jQuery(image).width() / jQuery(image).height() < width/height) {
			jQuery(image).css('height', Math.ceil(jQuery(image).height() * width / jQuery(image).width()) + 'px');
			jQuery(image).css('width', width + 'px');
		} else {
			jQuery(image).css('width', Math.ceil(jQuery(image).width()  * height / jQuery(image).height()) + 'px');
			jQuery(image).css('height', height + 'px');
		}
	}
}

/////////////
/* Rotator */
/////////////
var imageRotatorImage = 0;
var valuesArrayImage = [];
var pageId = '';
var $intervalid = '';
//Display this function when json finish


function loadImage(src,status) {
	var height = jQuery('#rotate-image').height();
	var width = jQuery('#rotate-image').width();
	var image = jQuery("<img alt=\"\">");
	image.load(function(){
		jQuery(this).fadeIn(2000, function(){
			if(status == "oneimage") {
				jQuery('#rotate-image img').each(function(index){			
					if(index > 0) { 
						jQuery(this).fadeOut(2000, function(){
							jQuery(this).remove();
						});
					}
				});
			}
			if(status == "lastimage") {
				if(jQuery('#rotate-image img').length > 1) {
			        jQuery('#rotate-image img:last').fadeOut(4000, function(){
						jQuery(this).remove();
					});
		        }
			}
		});
	});

	image.hide();
	jQuery('#rotate-image').prepend(image);
	image.attr('src',src);
}


function initialImage(){
	if(jQuery("#rotate-image").length == 1){
		imageRotatorImage = 0;
		
		jQuery("#loading").hide();
		loadImage(valuesArrayImage[0],"");
		$intervalid = setInterval( "rotateImage()", 10000/2 );
	}
}

function rotateImagePrevious(){
	
	clearInterval( $intervalid );
	$intervalid = setInterval( "rotateImage()", 10000/2 );

	var imageLen = jQuery('#rotate-image img').length;
	if(imageLen > 2) {
		jQuery('#rotate-image img').each(function(index){			
			if(index > 1) { 
				jQuery(this).remove();
			}
		});
	}	
	
	imageRotatorImage = ((imageRotatorImage + valuesArrayImage.length -1)%(valuesArrayImage.length));
	
	var img = new Image();
	jQuery(img).load(function () {			
		
		
		
	}).error(function () {
		
	}).attr('src', valuesArrayImage[imageRotatorImage])
	.each(function(){
		if(this.complete) {
			
			loadImage(valuesArrayImage[imageRotatorImage],"lastimage");
			
			
	        

		}
	});
	
	
	

	// Current Image 
	
	
}
var status = true;
var backgrounds = [];
function rotateImage(){
	
	clearInterval( $intervalid );
	$intervalid = setInterval( "rotateImage()", 10000/2 );
	
	if(valuesArrayImage.length > 1){
		var imageLen = jQuery('#rotate-image img').length;
		
		if(imageLen > 2) {			
			jQuery('#rotate-image img').each(function(index){			
				if(index < (imageLen-2)) { 
					jQuery(this).remove();
				}
			});
		
		}
		
		
		
		var img = new Image();
		jQuery(img).load(function () {			
			
			
			
		}).error(function () {
			
		}).attr('src', valuesArrayImage[imageRotatorImage])
		.each(function(){
			if(this.complete) {
				
				loadImage(valuesArrayImage[imageRotatorImage],"lastimage");
				
				
		        imageRotatorImage = (imageRotatorImage+1)%(valuesArrayImage.length);

			}
		});
		
		
		
	}
}
function rotateOneImage() {
	var imageLen = jQuery('#rotate-image img').length;
	
	var image = jQuery("<img alt=\"\">");
	jQuery('#rotate-image').prepend(image);
	
	
	var img = new Image();
	jQuery(img).load(function () {
//		image.attr('src',valuesArrayImage[0]);
//		jQuery('#rotate-image img:first').fadeIn(2000);
		loadImage(valuesArrayImage[0],"oneimage");
		
		imageRotatorImage = 0;
		
	}).error(function () {
		
	}).attr('src', valuesArrayImage[0]);
	
	
		
}

function resizeImages() {
	var height = jQuery('#rotate-image').height();
	var width = jQuery('#rotate-image').width();
	jQuery('#rotate-image img').each(function(id,element) {
		scaleImage(element, width, height);
	});
}
/////////////////////
var valuesArray = {};
var imagePath = '';
function jsonImage(change) {
	/* Rotate */
	
	jQuery("#loading").show();
	
	var pageId = jQuery("#page_id").attr('title');
	/*
	var categoryId = jQuery("#category_id").attr('title');
	if(categoryId != '') {
		pageId = categoryId;
	}
	*/
	
	
	//Display screensize
	//alert(changeBGScreenSize());
	var screenSize = changeBGScreenSize();
	
	valuesArrayImage = [];
	
	jQuery.getJSON(
		'/wp-content/themes/cribb/json-requestimage.php?sreensize='+screenSize,
		function(data){
			
			jQuery.each(data.userdata, function(i,user){
				var row = {};
				row[user.id] = user.rotated_image;
				valuesArray[i] = row;
				
			});
			
			
			var i=0;
			jQuery.each(valuesArray, function(key, value) {
				jQuery.each(value, function(id, item){					
					if(pageId == id) {
						valuesArrayImage[i] = item;				
						i +=1;
					}
				});
      			
			});
			
			
			
			//call initialImage function to display in front end from javascript
			if(jQuery('.hide-text').css('display') == 'none') {
				if(valuesArrayImage.length <= 1) {
					jQuery('.play-control').fadeOut();
				} else {
					jQuery('.play-control').fadeIn();
				}
			}	
			
			
			initialImage();
			if(change)rotateImage();
		}
	);
	
	
}

function changeBGScreenSize()
{
	var screen_width=window.screen.width;
	var screen_height=window.screen.height;
	
	
	
	var screen_sizes=new Array();
	screen_sizes[0]=1920;
	screen_sizes[1]=1680;
	screen_sizes[2]=1280;
	screen_sizes[3]=1024;
	screen_sizes=screen_sizes.sort();
	
	//set the bg image size
	for(i=0; i<screen_sizes.length ;i++ ){		
		if(screen_sizes[i]==screen_width) {
			break;
		}
		
		if(screen_sizes[i]>screen_width) {
			screen_width=screen_sizes[i];
			break;
		}
	}
	
	
	return screen_width;
}



function createUploader() {   
	  
	if(jQuery('#file-uploader').length) {
	    var uploader = new qq.FileUploader({    	
	        element: document.getElementById('file-uploader'),
	        action: '/uploader.php',
	        debug: true
	    });           
    }
}

jQuery(document).ready(function(){
	

	// Set url for Sign up for our Newsletter
	jQuery("#topMenuAction").attr("href","?show=newsletter");
	
	if(window.location.pathname != '/')
	{
		window.location.replace('/#' + window.location.pathname);
		
	}
	else
	{
		window['ajaxpath'] = '/';
		updateLinks('#top-link');
		updateLinks('#footer');
		jQuery(window).resize(function() {
//			resizeImages();
			updateScroll();
		});
//		resizeImages()
		updatePanel();
		showHideContent();
		topMenuAction();
		
		jQuery("#navigation li a[href]").each(function(index, element){
			var path = getPath(this.href);
			
			if(path) {
				element.href = "" +path+ "";
			}
		})
		//show text
		jQuery(".show-text").css({"height":"0","overflow":"hidden","padding":"0"});
		
		// hide text
		jQuery(".click-show-text").css({"display":"none","opacity":"0","background":"#1da2f9"});
		//Navigation();
		mainMenu();
		
		jQuery(document).ajaxError(function() {
  			fadeContentIn();
		});	
		
		
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
		SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, handleExternalChange);
	}
	
	jQuery("#rotate-image:first").fadeIn();	
	jsonImage(false);
	
	
	jQuery('.page-item-15, .contactus-top').click(function(e){
		jQuery('.click-show-text').fadeOut();
		jQuery('.hide-text').fadeIn();
		jQuery("#contentpane").show("slow");
		jQuery(".play-control").fadeOut();
		jQuery('#navigation').slideUp('slow', function(){
			jQuery('.contact-spotlight').slideDown();
		});
		
	});
	
	
	jQuery('.contact-spotlight h4').click(function(e){
		//jQuery('.contact-content').slideUp();		
		//jQuery('.contact-content').slideDown('slow');
		jQuery('.contact-spotlight').slideUp(function(){
			jQuery('#navigation').slideDown('slow');
			jQuery('.show-text').fadeOut();
		});
		
	});
	
	
	jQuery('#logo').click(function(e){
		clearInterval($intervalid);		
		jQuery('.contact-spotlight').slideUp('slow');
		jQuery('#navigation').slideDown('slow');
		
		jQuery('.show-text').fadeOut();
		jQuery("#contentpane").show("slow");
		
		
		jQuery('.hide-text').fadeIn("slow");
		jQuery(".play-control").fadeOut();
		
		jQuery('#image-pause').css('display','inline');
		jQuery('#image-play').css('display','none');
		
		
		
	});
	
	//Menu Footer 
	jQuery('.menu-footer').click(function(e){
		jQuery('.contact-spotlight').slideUp('slow');
		jQuery('#navigation').slideDown('slow');
		jQuery('.hide-text').fadeIn();
		jQuery("#contentpane").show("slow");
		//jQuery('.show-text').css({'height' : '0','padding' : '0'});
		jQuery('.show-text').fadeOut();
		jQuery(".play-control").fadeOut();
	});
	
	
	//pause
	jQuery('#image-pause').click(function(e){
		jQuery(this).css('display','none');
		jQuery('#image-play').css('display','inline');
		clearInterval($intervalid);
		e.preventDefault();
	});
	
	jQuery('#image-play').click(function(e){
		jQuery(this).css('display','none');
		jQuery('#image-pause').css('display','inline');
		$intervalid = setInterval( "rotateImage()", 10000/2 );
		e.preventDefault();
	});
	/*
	jQuery('#pause').toggle(
		function (e) {			
			jQuery('#pause img').toggle();
			clearInterval($intervalid);			
		},
		function(){
			jQuery('#pause img').toggle();
			$intervalid = setInterval( "rotateImage()", 10000/2 );			
		});
	*/
	//next 
	jQuery('#next').click(function(e) {
		clearInterval($intervalid);
		rotateImage();
		
		if(jQuery('#image-pause').css('display') == 'none') {
			clearInterval($intervalid);			
		}	
		e.preventDefault();
	});
	//previous
	jQuery('#previous').click(function(e) {
		
		clearInterval($intervalid);
		rotateImagePrevious();
		if(jQuery('#image-pause').css('display') == 'none') {
			//$intervalid = setInterval( "rotateImage()", 10000/2 );
			clearInterval($intervalid);
		}
		e.preventDefault();
	});	
	
	
	
});


