/**
 * @author Will
 */

var form_visible = false;
var strOff = "-off";
var strOver = "-on";
var currenttarget = "";
var list_target = 0;
var list_target2 = 0;
var image_target = 0;
var image_target2 = 0;
var timer;
var currentlength = 0;

function urlencode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
 
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };
 
    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();
 
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

function toggle_form(){
	
	if(form_visible){
		new Effect.Move('addresses',{x:0,y:0,mode:'absolute',duration:0.5});
		new Effect.Move('enquiry_form',{x:1002,y:0,mode:'absolute',duration:0.5});
		form_visible = false;
	} else {
		new Effect.Move('addresses',{x:-1002,y:0,mode:'absolute',duration:0.5});
		new Effect.Move('enquiry_form',{x:0,y:0,mode:'absolute',duration:0.5});
		form_visible = true;
	}
}

function swap_img(target){
	if(target!=""){
	var strImg = target.src;
   if (strImg.indexOf(strOver) != -1){ 
   var newImg = new Image();
	  target.src = strImg.replace(strOver,strOff);
   }else{
	  target.src = strImg.replace(strOff,strOver);
   }
}
}

function changeRollover(target){
	if(target != ""){
		var obj = document.getElementById(target);
		var strImg = obj.src;
		if(strImg.indexOf(strOver) == -1){
			newImg = strImg.replace(strOff,strOver);
			obj.src = newImg
		}
		
		obj.onmouseover = "";
		obj.onmouseout = "";
		obj.parentNode.parentNode.style.borderColor = "#baccdf";
	}
}
function hidecats(index2){
$('category_list').hide();
$('project_text').show();
		
if(cat_id != index2 && cat_id != "")
{
	$('images2_' + cat_id).hide();
}

$('images2_' + index2).show();

so_init(index2);

}


function rotate_pics2(){
	
	
//	currentlength	
/*	
	$('image2_' + cat_id + "_" + image_target).fade({duration:1.6,transition: Effect.Transitions.linear});
	var next = (image_target+1 == list.length) ? 0 : image_target+1;
	$('image2_' + cat_id + "_" + next).appear({duration:1.6,transition: Effect.Transitions.linear});
	image_target = next;
	rotate_list();
	timer = setTimeout('rotate_images()',5000);
	*/
}

function swap_services(index){
	
	var vals = services[index];
	var ul = new Element('ul');
	
	for(var i = 0;i<vals.length;i++){
		var li = (new Element('li')).update(vals[i]['service']);
		ul.appendChild(li);
		
	}
	$('services_heading').update(service_types[index]['service_type']);
	$('services_list').update(ul);
	
}

function swap_projects(index){
	
	if(cat_id != index && cat_id != "")
	{
		$('images_' + cat_id).hide();
	}
	if($('category_' + cat_id))
		$('category_' + cat_id).removeClassName('selected');
	
	clearTimeout(timer);
	cat_id = index;
	var vals = projects[index];
	var ul = new Element('ul');
	var regexp = new RegExp("/[ \t]/")
	
	for(var i = 0;i<vals.length;i++){
		var li = (new Element('li'));
		var a = new Element('a').update(vals[i]['project_name']);
		a.id = "project_" + i;
		a.href = "/" + company + "/projects/" + urlencode(categories[index]['category']) + "/" + vals[i]['prID'] + "/" + vals[i]['project_name'].replace(regexp,'') + ".html";
		
		if(!Object.isUndefined(project_id) && vals[i]['prID']==project_id){
			list_target = i;
			image_target=i;
			a.addClassName('selected');
		}
		
		li.appendChild(a);
		ul.appendChild(li);
		
	}
	$('projects_heading').update(categories[index]['category']+" Projects");
	$('projects').update(ul);
	$('images_' + index).show();
	if($('category_' + index))
		$('category_' + index).addClassName('selected');
	
	list = vals;
	$('image_' + cat_id + "_" + image_target).show();
	
	if(list.length > 1)
	{
		timer = setTimeout('rotate_images()',2000);
	}
	
}

function swap_projects_greatyarmouth(index){
	
	if(cat_id != index && cat_id != "")
	{
		$('images_' + cat_id).hide();
	}
	if($('category_' + cat_id))
		$('category_' + cat_id).removeClassName('selected');
	
	clearTimeout(timer);
	cat_id = index;
	var vals = projects[index];
	var ul = new Element('ul');
	var regexp = new RegExp("/[ \t]/")
	
	for(var i = 0;i<vals.length;i++){
		var li = (new Element('li'));
		var a = new Element('a').update(vals[i]['project_name']);
		a.id = "project_" + i;
		a.href = "/" + company + "/projects/" + urlencode(categories[index]['category']) + "/" + vals[i]['prID'] + "/" + vals[i]['project_name'].replace(regexp,'') + ".html";
		
		if(!Object.isUndefined(project_id) && vals[i]['prID']==project_id){
			list_target = i;
			image_target=i;
			a.addClassName('selected');
		}
		
		li.appendChild(a);
		ul.appendChild(li);
		
	}
	$('projects_heading').update(categories[index]['category']);

	$('projects').update(ul);
	$('images_' + index).show();
	if($('category_' + index))
		$('category_' + index).addClassName('selected');
	
	list = vals;
	$('image_' + cat_id + "_" + image_target).show();
	
	if(list.length > 1)
	{
		timer = setTimeout('rotate_images()',2000);
	}
	
}


function rotate_list(){
	
	new Effect.Morph('project_' + list_target,{
									style:"color:#fff",
									duration:1.6
									});
	var next = (list_target+1 == list.length) ? 0 : list_target+1;
	new Effect.Morph('project_' + next,{
		style:"color:#000",
		duration:1.6
		});
	list_target = next;

}

function rotate_images(){
	$('image_' + cat_id + "_" + image_target).fade({duration:1.6,transition: Effect.Transitions.linear});
	//$('image_' + cat_id + "_" + image_target).hide();
	var next = (image_target+1 == list.length) ? 0 : image_target+1;
	
	//alert('image_' + cat_id + "_" + image_target + "***" + 'image_' + cat_id + "_" + next);
	
	//$('image_' + cat_id + "_" + next).show();
	$('image_' + cat_id + "_" + next).appear({duration:1.6,transition: Effect.Transitions.linear});
	image_target = next;

	rotate_list();
	timer = setTimeout('rotate_images()',4000);
	
}


function return_images(){
	return images.toJSON(); 

}

function return_tags(){
	return tags.toJSON(); 

}

function return_folder(){
	return folder;
}

function return_company(){
	return company;
}

function switch_company(company){
	window.location.href = "index.php?action=switch-company&company=" + company;
}

function edit_contact(contact){

	myLightWindow.activateWindow({
		href:'edit-contact.php?contact=' + contact,
		height:430,
		width:475,
		type:"external",
		title:'Edit Contact Details'
	});
	
}

function delete_contact(contact){
	
	var url = "delete-contact.php?contact="+contact;
	var decision = confirm('Are you sure you want to delete this contact?');
	
	if(decision == true){
		new Ajax.Request(url,{
						method:'get',
						onSuccess: function(transport){
							$('contact_' + contact).remove();
							num_contacts--;
						}
					});
	}
}

function upload_img(){
	
	if(!Object.isArray(img_array) || img_array.length<6){
		myLightWindow.activateWindow({
			href:'upload-project-image.php?folder=' + $F('folder-input'),
			height:145,
			width:475,
			type:"external",
			title:"Upload Image"
		});
	} else {
		alert("You are only allowed 6 images per project.");
	}
	
}

function update_folder(project_name){
	
	var regexp = /[ \t]/g
	$('folder-input').value = project_name.replace(regexp,'');
		
}
