var viewportwidth;
var viewportwidth;
var viewportheight;
var layout_pos;
var print_polaroids = 0;

function findPos(obj){
	var curleft = curtop = 0;

	if (obj.offsetParent){
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;

		} while (obj = obj.offsetParent);
	}

	return [curleft,curtop];
}

function get_viewport_dimensions(){

	if(typeof window.innerWidth != 'undefined'){
	viewportwidth = window.innerWidth,
	viewportheight = window.innerHeight
	}
	 
	else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
	viewportwidth = document.documentElement.clientWidth,
	viewportheight = document.documentElement.clientHeight
	}
	 
	else{
	viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}

}

function position_elements(){
get_viewport_dimensions();
var start_pos = (((viewportwidth - 960) / 2) - 960) - (960 * total_transitions);
$("#home_slider_container").css("left", start_pos+"px");
var home_links = ((viewportwidth - 733) / 2);
$("#home_links").css("left", home_links+"px");
}

function isInt(x) {
var y=parseInt(x);
if (isNaN(y)) return false;
return x==y && x.toString()==y.toString();
}

$(document).ready(function(){
/*
$("#nav ul ul").css("left", "auto");
$("#nav ul ul").hide();

$("#nav ul>li").hover(function(){

$("ul", this).fadeIn();

},

function(){

$("ul", this).fadeOut();

});
*/

$("#show_search_popup").click(function(){
$("#search_popup").fadeIn();
});

if(document.getElementById("search_popup")){

var box = document.getElementById("search_popup");
var divWidth = 344;
var divHeight = 700;

	var scrollingLeft = Math.round((document.documentElement.clientWidth/2)-(box.style.width/2)) - (divWidth / 2);
	
	box.style.left = scrollingLeft+'px';
	box.style.top = '50px';

}

$('select#find_talent').selectmenu();

$("#featured_col .featured_casting_item .casting_item_hover p").hide();

$("#featured_col .featured_casting_item").hover(

function(){
$(".casting_item_hover p", this).slideDown("fast");
},

function(){
$(".casting_item_hover p", this).slideUp("fast");
}

);

if(document.getElementById("overlay")){

var overlay_pos = findPos(document.getElementById("model_list_container"));
var content_pos = findPos(document.getElementById("content"));

var overlay_top = (overlay_pos[1] - content_pos[1]) + 10;

var overlay_height = $("#model_list_container").height();
$("#overlay").css("height", overlay_height + 'px');
$("#overlay").css("top", overlay_top + 'px');

$("#close_popup").live("click", function(){
$("#overlay").fadeOut("fast");
$("#casting_details_popup").fadeOut("fast");
});

// $(".model_list_model a").removeAttr("href");

/*
var box = document.getElementById("casting_details_popup");
var divWidth = 840;
var divHeight = 490;

	var scrollingLeft = Math.round((document.documentElement.clientWidth/2)-(box.style.width/2)) - (divWidth / 2);
	var scrollingTop = Math.round((document.documentElement.clientHeight/2)-(box.style.height/2)+document.documentElement.scrollTop) - (divHeight / 2);
	
	box.style.left = scrollingLeft+'px';
	box.style.top = scrollingTop+'px';
*/

}

var pane = $('#model_images_container');

pane.jScrollPane(
{
	horizontalDragMaxWidth: 20,
	animateScroll: true
});

var api = pane.data('jsp');

$("#left_button").click(function(){
	api.scrollToX(0);
});

$("#right_button").click(function(){
	var scrollToVal = parseInt($("#model_images").css("width")) - 754;

	api.scrollToX(scrollToVal);
});

$("#start_button").click(function(){
// prev button
var current_pos = Math.ceil(api.getContentPositionX());

var current_width_calc = 0;
var next_image_index;

$('#model_images img').each(function(index){
current_width_calc = current_width_calc + ($(this).width() + 8);

	if(current_width_calc >= current_pos){
	current_width_calc = current_width_calc - ($(this).width() + 8);
	return false;
	}
	
});

var scrollToVal = current_width_calc;
api.scrollToX(scrollToVal);
});

$("#end_button").click(function(){
// next button

var current_pos = Math.ceil(api.getContentPositionX());

var current_width_calc = 0;
var next_image_index;

$('#model_images img').each(function(index){
current_width_calc = current_width_calc + $(this).width();

	if(current_width_calc > (current_pos + 762)){
	return false;
	}
	else
	{
	current_width_calc = current_width_calc + 8;
	}
	
});


var scrollToVal = current_width_calc - 754;
api.scrollToX(scrollToVal);

});

$("#polaroids").click(function(){

api.scrollToX(0, false);

if($(this).attr("rel") == 'polaroids'){

print_polaroids = 1;

$("#model_images").html(model_polaroid_images_content);
$("#model_images").css("width", model_polaroid_images_width+"px");

if(parseInt(model_polaroid_images_width) > 754){
$("#image_nav").slideDown();
$(".jspHorizontalBar").show();
api.reinitialise();
}

else{
$(".jspHorizontalBar").hide();
$("#image_nav").slideUp();
}

$(this).attr("rel", "main");
$(this).css("background-image", "url(/images/portfolio.png)");

}

else{

print_polaroids = 0;

$("#model_images").html(model_images_content);
$("#model_images").css("width", model_images_width+"px");

if(parseInt(model_images_width) > 754){
$("#image_nav").slideDown();
$(".jspHorizontalBar").show();
api.reinitialise();
}

else{
$(".jspHorizontalBar").hide();
$("#image_nav").slideUp();
}

$(this).attr("rel", "polaroids");
$(this).css("background-image", "url(/images/polaroids.png)");

}

});

// home page
$("#home_links").css({ opacity: 0 });
$("#home_links").css("display", "block");
$("#home_links").animate({opacity:1}, 1200);

get_viewport_dimensions();
var start_pos = ((viewportwidth - 960) / 2) - 960;
$("#home_slider_container").css("left", start_pos+"px");
$("#home_slider_container").css({ opacity: 0 });
$("#home_slider_container").css("display", "block");
$("#home_slider_container").delay(2000).animate({opacity:1}, 1200);

var home_links = ((viewportwidth - 733) / 2);
$("#home_links").css("left", home_links+"px");

t=setTimeout("timedSlider()", 4000);

$(window).bind('resize', function() { position_elements(); });

// home rollovers
$("#models_home").hover(function(){$("#main_home_image").attr("src","/images/models_and_casting_model_hover.png");}, function(){$("#main_home_image").attr("src","/images/models_and_casting.png");});
$("#casting_home").hover(function(){ $("#main_home_image").attr("src","/images/models_and_casting_casting_hover.png");}, function(){$("#main_home_image").attr("src","/images/models_and_casting.png");});

// category content expander

$("#expand_cat_content").click(function(){

	if($("#cat_content_expand").css("right") == '-220px'){
	$("#cat_content_expand").animate({right: '0'});
	$("#expand_cat_content").html('More <img src="/images/cat_plus.gif" alt="" />');
	}

	else{
	$("#cat_content_expand").animate({right: '-220px'});
	$("#expand_cat_content").html('Less <img src="/images/cat_minus.gif" alt="" />');
	}

});

});

function print_model(product_id, category_id){

window.open('/print.php?id='+product_id+'&cid='+category_id+'&polaroid='+print_polaroids, 'Print', 'status = 1, height = 560, width = 630, resizable = 0, scrollbars = yes, top=150, left=150' );

}

function print_casting(product_id, category_id){

window.open('/print_casting.php?id='+product_id+'&cid='+category_id, 'Print', 'status = 1, height = 560, width = 630, resizable = 0, scrollbars = yes, top=150, left=150' );

}

var current_image = 0;
var total_transitions = 0;

function timedSlider(){

var current_left = parseInt($("#home_slider_container").css("left"));
var new_left = current_left - 960;

	$("#home_slider_container").animate({left:new_left+'px'}, 1000, "swing", function(){

	var new_image = $("#home_slider_container img:eq("+current_image+")").attr("src");
	$("#home_slider_container").append('<img src="'+new_image+'" alt="" />');
	var new_width = parseInt($("#home_slider_container").css("width")) + 960;
	$("#home_slider_container").css("width", new_width+"px")
	current_image++;
	current_image = ($("#home_slider_container img").length == current_image) ? 0 : current_image ;

	total_transitions++;
	t=setTimeout("timedSlider()", 6000);

	});

}

// ajax

var xmlhttp

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

// popup for casting models

function casting_popup(){
$("#overlay").fadeIn("fast");
$("#casting_details_popup").fadeIn("fast");
}

function casting_popup_ajax(model_id, product, cat2){
/*
// build string

var div = 'casting_details_popup';
var url = '/functions/ajax/casting_popup.php?id='+model_id+'&product='+product+'&cat2='+cat2;

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null){
  alert ("Your browser does not support XMLHTTP!");
  return;
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4)
  {
  document.getElementById(div).innerHTML=xmlhttp.responseText;
  casting_popup();
  }
};

xmlhttp.open("GET",url,true);
xmlhttp.send(null);
*/
}

function validate_registration(layout){

var textfields = new Array('first_name', 'last_name', 'company_name', 'position');

var email = document.getElementById('email');
var confirm_email = document.getElementById('confirm_email');
var password = document.getElementById('password');
var confirm_password = document.getElementById('confirm_password');
var terms = document.getElementById('terms');

var valid = true
var error_msg = '';

// check text fields

	for(x in textfields){
	
		if(document.getElementById(textfields[x]).value.length == 0){
		document.getElementById(textfields[x]).style.border = '1px solid #cc0000';
		valid = false;
		}
		else{
		document.getElementById(textfields[x]).style.border = '1px solid #cccccc';
		}
	
	}
	
// check email

var emailValidationRule = new RegExp(/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/);

if(!emailValidationRule.test(email.value)){
email.style.border = '1px solid #cc0000';
valid = false;
}
else{
email.style.border = '1px solid #cccccc';
}

if(email.value != confirm_email.value){
confirm_email.style.border = '1px solid #cc0000';
valid = false;
}
else{
confirm_email.style.border = '1px solid #cccccc';
}

// check password

if(password.value.length < 6 || password.value.length > 16){
password.style.border = '1px solid #cc0000';
valid = false;
}
else{
password.style.border = '1px solid #cccccc';
}

if(password.value != confirm_password.value){
confirm_password.style.border = '1px solid #cc0000';
valid = false;
}
else{
confirm_password.style.border = '1px solid #cccccc';
}

// check terms

if(!terms.checked){
var error_msg = '<p>Please confirm that you agree to the <a href="/'+layout+'/terms.htm" target="_blank">terms &amp; conditions</a> and <a href="/'+layout+'/privacy.htm" target="_blank">privacy policy</a>.</p>';
valid = false;
}

// submit form or popup error

	if(!valid){
	show_popup('<h2>Registration error</h2><p>Some required fields were not completed.</p>'+error_msg+'<a onclick="document.getElementById(\'popup\').style.display = \'none\';" class="button" id="ok_button">OK</a>');
	}
	else{
	document.forms['registration_form'].submit();
	}

}

function show_popup(content){

// build popup

var divWidth = 288;
var divHeight = 300;

var divID = 'popup';
var box;

if(document.getElementById(divID)){
box = document.getElementById(divID);
}

else{
box = document.createElement("div");
box.setAttribute("id",divID);
document.body.appendChild(box);
}

	var scrollingLeft = Math.round((document.documentElement.clientWidth/2)-(box.style.width/2)) - (divWidth / 2);
	var scrollingTop = Math.round((document.documentElement.clientHeight/2)-(box.style.height/2)+document.documentElement.scrollTop) - (divHeight / 2);

	box.innerHTML = '<div id="popup_top">'+content+'</div><div id="popup_bottom"></div>';
	box.style.position = 'absolute';
	box.style.top = scrollingTop+'px';
	box.style.left = scrollingLeft+'px';
	box.style.zIndex = '100';
	box.style.display = 'block';

}

function add_to_my_boss(product_id, product_type, cat3, cat2, product){

// build string

var url = '/functions/ajax/my_boss.php?product_id='+product_id+'&product_type='+product_type+'&cat3='+cat3+'&cat2='+cat2+'&product='+product;

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null){
  alert ("Your browser does not support XMLHTTP!");
  return;
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4)
  {
  // popup confirmation
  
  show_popup('<h2>My Boss</h2><p>This person has been successfully added to My Boss. <a href="/'+product_type+'/my-boss.htm">Click here to go there now.</a></p><a onclick="document.getElementById(\'popup\').style.display = \'none\';" class="button" id="ok_button">OK</a>')
  
  }
};

xmlhttp.open("GET",url,true);
xmlhttp.send(null);

}

function show_male(){

	$(".female_attribute").hide();
	$(".male_attribute").show();

}

function show_female(){

	$(".female_attribute").show();
	$(".male_attribute").hide();

}
