if(! top.BAQA_PREVIEW){
	top.BAQA_PREVIEW = false;
}

function initBaqa(){
	top.DocumentLoaded = true;
	if(!top.BAQA_SEND_PREVIEW){
		createBaqaIcons();
		createMovingObj();
		addBaqaIconToImages();
		document.body.onclick=hideBaqaIcon;
		document.body.onmousemove = function(){
			if(!top.mouseOverMatImage){
				hideBaqaIcon();
			}
		}
	}
	
	if(!top.BAQA_PREVIEW){
		var list = getCookie('BaqaContent');
		if(!list) list='';
		updateBaqaWidget(list);
	}
}

function addBaqaIconToImages(){
	var images = document.images;
	var i=0;
	var mid = 0;
	var jpgFolder = window.location.protocol+'//'+window.location.host+'/jpg/';
	for(i=0;i<images.length;i++){
		if(images[i].src && images[i].src.indexOf(jpgFolder)!=-1)	{
			mid = parseInt(images[i].src.replace(jpgFolder,''));
			if(mid){
				images[i].setAttribute('id','jpg'+mid);
				images[i].setAttribute('mid',mid);
				images[i].onmouseover = showBaqaIcon;
				images[i].onmouseout = setMouseOut;
				/*if(!document.all){
					images[i].onmouseout =mouseOutMatImage;
				}*/
			}
		}
	}
}

function setMouseOut(){
	top.mouseOverMatImage = false;
}

function emptyBaqa(){
	setCookie('BaqaContent','');
	window.location='baqa.php';
}

function inBaqa(mid){
	var list = getCookie('BaqaContent');
	var isInBaqa = false;
	if(list){
		if(list==mid){
			isInBaqa = true;
		}else{
			var listArray = list.split(',');
			for(i=0;i<listArray.length;i++){
				if(listArray[i]==mid){
					isInBaqa = true;
					break;
				}
			}
		}
	}
	return isInBaqa;
}

function mouseOutMatImage(){
	setTimeout('hideBaqaIcon()',50);
}

function showBaqaIcon(ref){
	if(!top.DocumentLoaded) return;
	if(ref==undefined || ref.nodeType!=1)ref=this;
	var pos = findPos(ref);
	var mid = ref.getAttribute('mid');
	if(!top.BAQA_PREVIEW){
		var BaqaLogo = document.getElementById('BaqaLogo');
		var BaqaLogoPos = findPos(BaqaLogo);
	}
	top.mid = mid;
	top.mouseOverMatImage = true;
	var posOffset = 0;
	if(!document.all){
		posOffset=1;
	}
	if(inBaqa(mid)){
		top.BaqaIconDel.style.display='block';
		top.BaqaIconDel.style.top = (pos[1]+5)+'px';
		top.BaqaIconDel.style.left = (pos[0]+posOffset)+'px';
		top.BaqaIconDel.style.width = ref.offsetWidth + "px";
		top.BaqaIconDel.onclick = delMatFromBaqa;
		if(!top.BAQA_PREVIEW){
			top.BaqaImg.src = ref.src;
			top.BaqaImg.width = 1;
			top.BaqaImg.height = 1;
			top.BaqaImg.style.top= (BaqaLogoPos[1]+25)+'px';
			top.BaqaImg.style.left= (BaqaLogoPos[0]+90)+'px';
			top.BaqaImg.setAttribute('tox',pos[0]+'px');
			top.BaqaImg.setAttribute('toy', pos[1]+'px');
			top.BaqaImg.setAttribute('tow', ref.width);
			top.BaqaImg.setAttribute('toh', ref.height);
		}
	}else if(!top.BAQA_PREVIEW){
		top.BaqaIcon.style.display='block';
		top.BaqaIcon.style.top= (pos[1]+5)+'px';
		top.BaqaIcon.style.left= (pos[0]+posOffset)+'px';
		top.BaqaIcon.style.width = ref.offsetWidth+"px";
		top.BaqaIcon.onclick = addMatToBaqa;
		if(!top.BAQA_PREVIEW){
			top.BaqaImg.src = ref.src;
			top.BaqaImg.width = ref.width;
			top.BaqaImg.height = ref.height;
			top.BaqaImg.style.top= pos[1]+'px';
			top.BaqaImg.style.left= pos[0]+'px';
			top.BaqaImg.setAttribute('tox', (BaqaLogoPos[0]+90)+'px');
			top.BaqaImg.setAttribute('toy', (BaqaLogoPos[1]+25)+'px');
			top.BaqaImg.setAttribute('tow', 1);
			top.BaqaImg.setAttribute('toh', 1);
		}
	}
}

function hideBaqaIcon(){
	top.BaqaIcon.style.display='none';
	top.BaqaIconDel.style.display='none';
}

function addMatToBaqa(){
	if(top.mid){
		var list = getCookie('BaqaContent');
		if(!list) list='';
		if(list){
			list+=","+top.mid;
		}else{
			list = 	top.mid;
		}
		dt=new Date();
		dt.setDate(dt.getDate()+30);
		setCookie('BaqaContent', list, dt);
		updateBaqaWidget(list);
		showAndMoveMovingObj();
	}
	hideBaqaIcon();
}

function delMatFromBaqa(){
	if(top.mid){
		var oldList = getCookie('BaqaContent');
		var listArr = [];
		if(oldList){
			if(oldList.indexOf(',')!=-1){
				var oldListArr = oldList.split(',');
				for(i=0;i<oldListArr.length;i++){
					if(oldListArr[i]!=top.mid){
						listArr[listArr.length]=oldListArr[i];
					}
				}
			}else if(oldList!=top.mid){
				listArr=[oldList];
			}
		}
		var dt=new Date();
		var list = listArr.join(',');
		dt.setDate(dt.getDate()+30);
		setCookie('BaqaContent', list, dt);
		if(top.BAQA_PREVIEW){
			window.location='baqa.php';
		}else{
			showAndMoveMovingObj();
			updateBaqaWidget(list);
			hideBaqaIcon();
		}
	}
}

function updateBaqaWidget(list){
	var baqaContentCount = document.getElementById('BaqaContentCount');
	if(baqaContentCount){
		var count = 0;
		list = new String(list);
		if(!list){
			count = 0;
		}else if(list.indexOf(',')!=-1){
			var listArr = list.split(',');
			count=listArr.length;
		}else if(parseInt(list)){
			count=1;
		}
		
		baqaContentCount.innerHTML = count;
	}
}

function createBaqaIcons(){
	var icon = document.createElement('span');
	
	icon.style.position = 'absolute';
	icon.style.width='32px';
	icon.style.height='20px';
	icon.style.fontSize='1px';
	icon.style.display='none';
	icon.style.cursor='pointer';
	icon.style.textAlign = "center";
	icon.style.backgroundImage='url(images/BaqaIconBG.jpg)';
	icon.id = 'BaqaIcon';
	icon.innerHTML = '<img src="images/BaqaIconAdd.jpg" align="center"/>';
	icon.setAttribute('onmouseover',"mouseOverBaqaIcon(event);");
	icon.onmouseout=function(){
		top.BaqaIconVisible = false;
		setMouseOut();
	}
	icon.onmouseover = icon.onmousemove = function(){
		top.mouseOverMatImage = true;
	};
	icon.title = "أضف هذه المادة للباقة الدعوية";
	document.body.appendChild(icon);
	top.BaqaIcon = icon;
	
	var iconDel = document.createElement('span');
	iconDel.style.position = 'absolute';
	iconDel.style.width='32px';
	iconDel.style.height='20px';
	iconDel.style.fontSize='1px';
	iconDel.style.display='none';
	iconDel.style.cursor='pointer';
	iconDel.style.backgroundImage='url(images/BaqaIconBG.jpg)';
	iconDel.id = 'BaqaIconDel';
	iconDel.style.textAlign = "center";
	iconDel.innerHTML = '<img src="images/BaqaIconRemove.jpg" align="center"/>';
	iconDel.setAttribute('onmouseover',"mouseOverBaqaIcon(event);");
	iconDel.onmouseout=function(){
		top.BaqaIconVisible = false;
		setMouseOut();
	}
	iconDel.onmouseover = iconDel.onmousemove = function(){
		top.mouseOverMatImage = true;
	};
	iconDel.title = "احذف هذه المادة من الباقة الدعوية";
	document.body.appendChild(iconDel);
	top.BaqaIconDel = iconDel;
	
	/*icon.onmouseover=function(){
		top.BaqaIconVisible = true	;
	}*/
}

function createMovingObj(){
	var img = document.createElement('img');
	img.style.position = 'absolute';
	img.style.display='none';
	img.style.opacity = 0.5;
	img.style.filter = 'alpha(opacity=50)';
	img.style.border='1ps solid #000000';
	img.id = 'BaqaImg';
	document.body.appendChild(img);
	top.BaqaImg = img;
}

function showAndMoveMovingObj(){
	var movingObj = top.BaqaImg.cloneNode(false);
	movingObj.id = 'BaqaMovingNow';
	document.body.appendChild(movingObj);
	movingObj.style.display='block';
	slideMovingObj();
}

function slideMovingObj() {
	var obj = document.getElementById('BaqaMovingNow');
	var fromx  = parseInt(obj.style.left);
	var tox = parseInt(obj.getAttribute('tox'));
	var fromy  = parseInt(obj.style.top);
	var toy = parseInt(obj.getAttribute('toy'));
	var fromw  = parseInt(obj.width);
	var tow = parseInt(obj.getAttribute('tow'));
	var fromh  = parseInt(obj.height);
	var toh = parseInt(obj.getAttribute('toh'));
	
	var stepx = parseInt((tox - fromx)/4);
	var stepy = parseInt((toy - fromy)/4);
	var stepw = parseInt((tow - fromw)/4);
	var steph = parseInt((toh - fromh)/4);
	var workx = Math.floor(Math.abs(stepx))!=0;
	var worky = Math.floor(Math.abs(stepy))!=0;
	var workw = Math.floor(Math.abs(stepw))!=0;
	var workh = Math.floor(Math.abs(steph))!=0;
	if (workx || worky || workw || workh) {
		if(workx){
			fromx += stepx;
			obj.style.left = fromx;
		}
		if(worky){
			fromy += stepy;
			obj.style.top = fromy;
		}
		if(workw){
			fromw += stepw;
			obj.width = fromw;
		}
		if(workh){
			fromh += steph;
			obj.height = fromh;
		}
		setTimeout("slideMovingObj()", 30);
	}else{
		obj.style.display='none';
		document.body.removeChild(obj);
	}
}

function mouseOverBaqaIcon(event){
	top.BaqaIconVisible = true	;
	stopPropagation(event);
}



function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function setCookie(name, value, expire, path){
  document.cookie = name + "=" + escape(value) +
                    ((!expire) ? "" : ("; expires=" + expire.toGMTString())) +
                    "; path=" + ((!path) ? "/" : path);
}

function getCookie(name){
  var value=null, search=name+"=";
  if (document.cookie.length > 0) {
    var offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      var end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      value = unescape(document.cookie.substring(offset, end));
    }
  }
  return value;
}

function stopPropagation(evt){
  if (evt && evt.stopPropagation) evt.stopPropagation();
  else if (window.event) window.event.cancelBubble = true;
}

function validateBaqa(){
	var email = document.getElementById('email').value;
	var name = document.getElementById('name').value;
	var email1 = document.getElementById('email1').value;
	var email2 = document.getElementById('email2').value;
	var email3 = document.getElementById('email3').value;
	var email4 = document.getElementById('email4').value;
	var email5 = document.getElementById('email5').value;
	var email6 = document.getElementById('email6').value;
	var success = true;
	var msd = '';
	
	if(!isEmail(trim(email))){
		msd+="الرجاء إدخال بريدك بشكل صحيح\n";
		success=false;
	}
	
	if(!trim(name)){
		msd+="الرجاء إدخال اسمك\n";
		success=false;
	}
	
	if(!trim(email1) && !trim(email2) && !trim(email3) && !trim(email4) && !trim(email5) && !trim(email6)){
		msd+="الرجاء إدخال بريد صديق واحد على الأقل.\n";
		success=false;
	}else{
		if(trim(email1) && !isEmail(trim(email1))){
			msd+="الرجاء إدخال بريد صديقك الأول بشكل صحيح\n";
			success=false;
		}
		if(trim(email2) && !isEmail(trim(email2))){
			msd+="الرجاء إدخال بريد صديقك الثاني بشكل صحيح\n";
			success=false;
		}
		if(trim(email3) && !isEmail(trim(email3))){
			msd+="الرجاء إدخال بريد صديقك الثالث بشكل صحيح\n";
			success=false;
		}
		if(trim(email4) && !isEmail(trim(email4))){
			msd+="الرجاء إدخال بريد صديقك الرابع بشكل صحيح\n";
			success=false;
		}
		if(trim(email5) && !isEmail(trim(email5))){
			msd+="الرجاء إدخال بريد صديقك الخامس بشكل صحيح\n";
			success=false;
		}
		if(trim(email6) && !isEmail(trim(email6))){
			msd+="الرجاء إدخال بريد صديقك السادس بشكل صحيح\n";
			success=false;
		}
	}
	if(!success)alert(msd);
	return success;
}

window.onload = initBaqa;